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
from collections import deque
def isSafe(x, y):
if 0 <= x < N and 0 <= y < M:
return True
def BFS():
while q:
x, y = q.popleft()
visited[x][y] = 1
for i in range(4):
cx = x + dx[i]
cy = y + dy[i]
if isSafe(cx, cy) and arr[cx][cy] == 1 and visited[cx][cy] == 0:
q.append([cx, cy])
arr[cx][cy] = arr[x][y] + 1
dx = [-1, 0, 1, 0]
dy = [0, 1, 0, -1]
N, M = map(int, sys.stdin.readline().split())
arr = [list(map(int, sys.stdin.readline().rstrip())) for _ in range(N)]
visited = [[0]*(M) for _ in range(N)]
q = deque([[0, 0]])
BFS()
print(arr[N-1][M-1])
# pop을 사용하는데 리스트형태의 자료인 경우
# x, y = [1, 2]의 모습으로도 추출이 가능
|