문제
2178번 미로탐색
코드
import sys
from collections import deque
input = sys.stdin.readline
N, M = map(int, input().split())
def bfs(s_x, s_y):
q = deque()
visited[s_x][s_y] = 1
q.append((s_x, s_y))
dx = [-1, 1, 0, 0]
dy = [0, 0, -1, 1]
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:
if not visited[nx][ny] and graph[nx][ny] >= 1:
visited[nx][ny] = 1
graph[nx][ny] = graph[x][y] + 1
q.append((nx, ny))
graph = []
visited = [[0 for _ in range(M)] for _ in range(N)]
for _ in range(N):
graph.append(list(map(int, input().rstrip())))
bfs(0, 0)
print(graph[N-1][M-1])