[깊이/너비 우선 탐색(DFS/BFS)] 게임 맵 최단거리

박고은·2023년 6월 11일
0

from collections import deque

def solution(maps):
    n, m = len([i[0] for i in maps]), len(maps[0])
    direction = [(1, 0), (-1, 0), (0, -1), (0, 1)]
    
    q = deque()
    q.append((0, 0))
    
    while q:
        x, y = q.popleft()
        for d in direction:
            x2 , y2 = x+d[0], y+d[1]
            if 0<=x2<n and 0<=y2<m and maps[x2][y2]==1:
                q.append((x2, y2))
                maps[x2][y2] = maps[x][y] + 1
                
    if maps[n-1][m-1]==1: return -1
    return maps[n-1][m-1]
    

0개의 댓글