lhb8106 / CodingTest

코테의 고수가 되어보자 👀
0 stars 0 forks source link

[PG] 게임 맵 최단거리 #18

Open lhb8106 opened 1 year ago

lhb8106 commented 1 year ago

게임 맵 최단거리

lhb8106 commented 1 year ago
from collections import deque
def solution(maps):
    answer = 0

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

    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 nx < 0 or ny < 0 or nx >= len(maps) or ny >= len(maps[0]) :
                    continue
                if maps[nx][ny] == 0:
                    continue
                if maps[nx][ny] == 1:
                    maps[nx][ny] = maps[x][y] + 1
                    queue.append((nx, ny))

        return maps[len(maps)-1][len(maps[0])-1]

    answer = bfs(0,0)
    return -1 if answer == 1 else answer