문제📖
풀이🙏
- bfs를 이용하여 graph의 최우측하단의
graph[n][m]
을 출력한다.
코드💻
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 and ny>=0 and nx<n and ny<m and 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))