
😎BACKJOON>2178번: 미로탐색
📘 문제풀이
from collections import deque
n, m = map(int, input().split())
graph = [list(map(int, input())) for _ in range(n)]
visited = [[False] * m for _ in range(n)]
dx = [1, -1, 0, 0]
dy = [0, 0, 1, -1]
def bfs(cur_x, cur_y, graph, visited):
queue = deque()
queue.append((cur_x, cur_y))
visited[cur_x][cur_y] = True
while queue:
pop_x, pop_y = queue.popleft()
for i in range(4):
next_x = pop_x + dx[i]
next_y = pop_y + dy[i]
if next_x <= -1 or next_x >= n or next_y <= -1 or next_y >= m:
continue
if visited[next_x][next_y] != True and graph[next_x][next_y] != 0:
queue.append((next_x,next_y))
visited[next_x][next_y] = True
graph[next_x][next_y] = graph[pop_x][pop_y] + 1
bfs(0, 0, graph, visited)
print(graph[n-1][m-1])