-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2665.py
More file actions
35 lines (28 loc) · 766 Bytes
/
2665.py
File metadata and controls
35 lines (28 loc) · 766 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# 미로 만들기
import sys
import heapq
input = sys.stdin.readline
n = int(input())
mazeMap = []
visited = [[0]*n for _ in range(n)]
for _ in range(n):
mazeMap.append(list(map(int, input().strip())))
dx = [1, -1, 0, 0]
dy = [0, 0, 1, -1]
visited[0][0] = 0
queue = []
heapq.heappush(queue, (0, 0, 0))
while queue:
count, x, y = heapq.heappop(queue)
if x == n-1 and y == n-1:
print(count)
break
for k in range(4):
nx = x + dx[k]
ny = y + dy[k]
if 0 <= nx < n and 0 <= ny < n and visited[nx][ny] == 0:
visited[nx][ny] = 1
if mazeMap[nx][ny] == 0:
heapq.heappush(queue, (count + 1, nx, ny))
else:
heapq.heappush(queue, (count, nx, ny))