-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path5-11.py
More file actions
36 lines (24 loc) · 661 Bytes
/
5-11.py
File metadata and controls
36 lines (24 loc) · 661 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
36
from collections import deque
n,m = map(int,input().split())
graph = []
for i in range(n):
graph.append(list(map(int,input())))
dx = [-1,1,0,0]
dy = [0,0,-1,1]
def bfs(x,y):
queue = deque()
queue.append((x,y))
while queue:
x,y = queue.popleft()
for i in range(4):
nx = x + dx[i]
ny = y + dy[i]
if nx< 0 or nx>=n or ny<0 or ny>=m:
continue
if graph[nx][ny] == 0:
continue
if graph[nx][ny] == 1:
graph[nx][ny] = graph[x][y]+1
queue.append((nx,ny))
return graph[n-1][m-1]
print(bfs(0,0))