2178 미로 탐색 [Python]

지구온난화·2023년 8월 17일
0

백준 실버

목록 보기
25/25

참고click

import sys
from collections import deque
input = sys.stdin.readline

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 0 <= nx < N and 0 <= ny < M and graph[nx][ny] == 1:
                queue.append((nx,ny))
                graph[nx][ny] = graph[x][y] + 1
    
    return graph[N-1][M-1]



N, M = map(int, input().split())

graph = []

for i in range(N):
    graph.append(list(map(int, input().rstrip())))


dx = [-1,1,0,0]
dy = [0,0,-1,1]

print(bfs(0,0))

0개의 댓글