백준 알고리즘 2178번
파이썬(BFS사용)
from collections import deque
n,m = map(int,input().split())
s=[]
for _ in range(n):
s.append(list(map(int,input())))
dx = [-1,1,0,0]
dy = [0,0,-1,1]
def bfs(x,y):
q = deque()
q.append((x,y))
while q:
x,y = q.popleft()
for i in range(4):
nx = x+dx[i]
ny = y+dy[i]
if 0<= nx < n and 0<= ny <m
and s[nx][ny]==1:
q.append((nx,ny))
s[nx][ny] = s[x][y]+1
return s[n-1][m-1]
print(bfs(0,0))
댓글남기기