[프로그래머스] 게임 맵 최단거리

 

문제

from collections import deque

def bfs(maps, start_x, start_y, dx, dy):
    n = len(maps)
    m = len(maps[0])
    visited = [[False]*m for _ in range(n)]
    visited[start_x][start_y] = True
    d = deque()
    d.append([start_x, start_y])
    while d:
        x,y = d.popleft()
        for i in range(4):
            nx = x+dx[i]
            ny = y+dy[i]
            if 0 <= nx < n and 0 <= ny < m and not visited[nx][ny] and maps[nx][ny] == 1:
                d.append((nx, ny))
                visited[nx][ny] = True
                maps[nx][ny] += maps[x][y]
def solution(maps):
    dx = [-1, 1, 0, 0]
    dy = [0, 0, -1, 1]
    bfs(maps, 0, 0, dx, dy)
    
    return maps[-1][-1] if maps[-1][-1] != 1 else -1