-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2178.py
More file actions
28 lines (26 loc) · 673 Bytes
/
2178.py
File metadata and controls
28 lines (26 loc) · 673 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
import sys
import queue
n, m = map(int, sys.stdin.readline().split())
maze = []
dist = [[0]*m for _ in range(n)]
visited = [[False]*m for _ in range(n)]
for i in range(n):
maze.append(list(map(int, sys.stdin.readline().rstrip())))
q=queue.Queue()
q.put((0, 0))
dist[0][0]=1
visited[0][0] = True
while q:
i, j=q.get()
if i==n-1 and j==m-1:
break
dx=[-1, 0, 1, 0]
dy=[0, -1, 0, 1]
for k in range(4):
nx=i+dx[k]
ny=j+dy[k]
if 0 <= nx < n and 0 <= ny < m and maze[nx][ny] and not visited[nx][ny]:
visited[nx][ny]=True
q.put((nx, ny))
dist[nx][ny]=dist[i][j]+1
print(dist[n-1][m-1])