https://www.acmicpc.net/problem/2178
# 미로 탐색
import sys
from collections import deque
input = sys.stdin.readline
N, M = map(int,input().rstrip().split())
graph = []
for _ in range(N): # graph = [list(map(int, input())) for _ in range(n)]으로도 해결 가능
graph.append(list(map(int, input().rstrip())))
# 너비 우선 탐색
def bfs(x, y):
# 이동할 네 가지 방향 정의 (상, 하, 좌, 우)
dx = [-1, 1, 0, 0]
dy = [0, 0, -1, 1]
# deque 생성
queue = deque()
queue.append((x, y))
while queue:
x, y = queue.popleft()
# 현재 위치에서 4가지 방향으로 위치 확인
for i in range(4):
nx = x + dx[i]
ny = y + dy[i]
# 위치가 벗어나면 안되기 때문에 조건 추가
if nx < 0 or nx >= N or ny < 0 or ny >= M:
continue
# 벽이므로 진행 불가
if graph[nx][ny] == 0:
continue
# 벽이 아니므로 이동 // 1이상인 경우에는 한번이상 지나간 경우
if 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))
마지막에는 경로에는 지나온 횟수가 저장된다.
0 = 벽, 1 = 지나가지않은 길, 1이상 = 한번이상 방문한 길
참고
https://jokerldg.github.io/algorithm/2021/03/24/maze.html