-
Notifications
You must be signed in to change notification settings - Fork 0
/
1261.py
36 lines (27 loc) · 827 Bytes
/
1261.py
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
import sys
input = sys.stdin.readline
dx = [-1, 1, 0, 0]
dy = [0, 0, 1, -1]
M, N = map(int, input().split())
arr = [list(map(int, list(input())[:-1])) for _ in range(N)]
visited = [[N*M]* M for _ in range(N)]
que = deque()
que.append((0,0))
visited[0][0] = 0
while que:
x, y = que.popleft()
time = visited[x][y]
if x == N-1 and y == M -1:
print(time)
break
for i in range(4):
nx = x + dx[i]
ny = y + dy[i]
if 0<= nx < N and 0<= ny < M:
if arr[nx][ny] == 0 and visited[nx][ny] > time:
visited[nx][ny] = time
que.appendleft((nx,ny))
elif arr[nx][ny] == 1 and visited[nx][ny] > time + 1:
visited[nx][ny] = time + 1
que.append((nx,ny))