diff --git "a/cmkim/BaekJoon/\353\260\261\354\244\200_1504.py" "b/cmkim/BaekJoon/\353\260\261\354\244\200_1504.py" new file mode 100644 index 0000000..bca8253 --- /dev/null +++ "b/cmkim/BaekJoon/\353\260\261\354\244\200_1504.py" @@ -0,0 +1,40 @@ +import sys +import heapq +input = sys.stdin.readline +INF = 1e9 + +n, e = map(int, input().split()) + +graph = [[] for i in range(n + 1)] + + +arr = [[] for _ in range(n+1)] +for _ in range(e): + a, b, c = map(int, input().split()) + arr[a].append((b, c)) # a부터 b까지의 거리가 c이다. + arr[b].append((a, c)) +v1, v2 = map(int, input().split()) + +def dijkstra(start): + distance = [INF] * (n + 1) + q = [] + heapq.heappush(q, (0, start)) + distance[start] = 0 + while q: + dist, now = heapq.heappop(q) + if distance[now] < dist: + continue + for i in arr[now]: + cost = dist + i[1] + if cost < distance[i[0]]: + distance[i[0]] = cost + heapq.heappush(q, (cost, i[0])) + return distance + +first = dijkstra(1) +second = dijkstra(v1) +third = dijkstra(v2) +if min(first[v1]+second[v2]+third[n], first[v2]+third[v1]+second[n]) x or x >= n or 0 > y or y >= m: #탈출 + if 0 > x or x >= n or 0 > y or y >= m: # 탈출 return True - if arr[x][y] == 'true':# 탈출 확정된 곳 + if arr[x][y] == 'true': # 탈출 확정된 곳 return True - elif arr[x][y] == 'false':# 탈출 불가능 확정된 곳 + elif arr[x][y] == 'false': # 탈출 불가능 확정된 곳 return False if visited[x][y]: @@ -27,7 +29,7 @@ def dfs(x, y): n, m = map(int, input().split()) arr = [] -visited = [[0]*m for _ in range(n)] +visited = [[0] * m for _ in range(n)] for i in range(n): arr.append(list(input())) @@ -44,4 +46,3 @@ def dfs(x, y): count += 1 print(count) - diff --git "a/cmkim/BaekJoon/\353\260\261\354\244\200_2206.py" "b/cmkim/BaekJoon/\353\260\261\354\244\200_2206.py" new file mode 100644 index 0000000..0812a71 --- /dev/null +++ "b/cmkim/BaekJoon/\353\260\261\354\244\200_2206.py" @@ -0,0 +1,48 @@ +import sys +from collections import deque + +input = sys.stdin.readline +dx = [-1, 1, 0, 0] +dy = [0, 0, -1, 1] +n, m = map(int, input().split()) +arr = [] # 맵 정보 +arr.append([1 for _ in range(m + 1)]) +visited = [[[0] * (m + 1) for _ in range(n + 1)] for __ in range(2)] + +for i in range(1, n + 1): + arr.append(list(map(int, input().strip()))) + arr[i].insert(0, 1) + + +def bfs(): + q = deque() + q.append((1, 1, 1, 0)) # x, y, cost, 벽부순지에 대한 여부 + visited[0][1][1] = 1 + visited[1][1][1] = 1 + while q: + x, y, c, flag = q.popleft() + # visited[0][x][y] = c + # visited[1][x][y] = c <- 이렇게 두개의 배열다 초기화 시키는 방법은 왜 시간초과가 날까? + # if x == n and y == m: + # print(visited[flag][x][y], flag) + for i in range(4): + nx = x + dx[i] + ny = y + dy[i] + if 0 < nx <= n and 0 < ny <= m and arr[nx][ny] == 0 and not visited[flag][nx][ny]: + q.append((nx, ny, c + 1, flag)) + visited[flag][nx][ny] = visited[flag][x][y] + 1 + if 0 < nx <= n and 0 < ny <= m and arr[nx][ny] == 1 and flag == 0: + q.append((nx, ny, c + 1, 1)) + visited[flag+1][nx][ny] = visited[flag][x][y] + 1 + + +bfs() + +result = 0 +for i in range(2): + if visited[i][n][m]: + result = visited[i][n][m] +if result: + print(result) +else: + print(-1) diff --git "a/cmkim/BaekJoon/\353\260\261\354\244\200_4386.py" "b/cmkim/BaekJoon/\353\260\261\354\244\200_4386.py" index 967ee88..db042e2 100644 --- "a/cmkim/BaekJoon/\353\260\261\354\244\200_4386.py" +++ "b/cmkim/BaekJoon/\353\260\261\354\244\200_4386.py" @@ -1,4 +1,5 @@ import sys, math, heapq + n = int(input()) arr = [] @@ -6,8 +7,10 @@ sum = 0 cost = 1e9 + def dist(x1, y1, x2, y2): - return math.sqrt((x1 - x2)**2 + (y1 - y2)**2) + return math.sqrt((x1 - x2) ** 2 + (y1 - y2) ** 2) + for i in range(n): x, y = map(float, input().split()) @@ -33,16 +36,4 @@ def dist(x1, y1, x2, y2): if i not in connected: heapq.heappush(q, (dist(arr[node][0], arr[node][1], arr[i][0], arr[i][1]), i)) -print("%.2f"%(sum)) - - - - - - - - - - - - +print("%.2f" %(sum)) diff --git "a/cmkim/BaekJoon/\353\260\261\354\244\200_5547.py" "b/cmkim/BaekJoon/\353\260\261\354\244\200_5547.py" index bd666a0..725b449 100644 --- "a/cmkim/BaekJoon/\353\260\261\354\244\200_5547.py" +++ "b/cmkim/BaekJoon/\353\260\261\354\244\200_5547.py" @@ -1,22 +1,24 @@ import sys from collections import deque + input = sys.stdin.readline -odd = [(-1, 1), (0, 1), (1, 1), (1, 0), (0, -1), (-1, 0)] #행, 렬 기준으로함 정각부터 정시계방향으로 순환 +odd = [(-1, 1), (0, 1), (1, 1), (1, 0), (0, -1), (-1, 0)] # 행, 렬 기준으로함 정각부터 정시계방향으로 순환 even = [(-1, 0), (0, 1), (1, 0), (1, -1), (0, -1), (-1, -1)] + def bfs(x, y): q = deque([(x, y)]) count = 0 while q: x, y = q.popleft() visited[x][y] = 1 - dir = even if x%2 == 0 else odd + dir = even if x % 2 == 0 else odd for i in range(6): dx, dy = dir[i][0], dir[i][1] nx, ny = x + dx, y + dy - if 0 <= nx < h+2 and 0 <= ny < w+2: + if 0 <= nx < h + 2 and 0 <= ny < w + 2: if arr[nx][ny] == 1: count += 1 elif arr[nx][ny] == 0 and not visited[nx][ny]: @@ -25,13 +27,12 @@ def bfs(x, y): return count - -w, h = map(int, input().split()) # 열, 행 순서로 입력 -arr = [[0] * (w+2)] -visited = [[0] * (w+2) for _ in range(h+2)] +w, h = map(int, input().split()) # 열, 행 순서로 입력 +arr = [[0] * (w + 2)] +visited = [[0] * (w + 2) for _ in range(h + 2)] for i in range(h): - arr.append([0]+list(map(int, input().split())) + [0]) -arr.append([0 for _ in range(w+2)]) + arr.append([0] + list(map(int, input().split())) + [0]) +arr.append([0 for _ in range(w + 2)]) # for i in range(h+2): # print(arr[i])