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

Junyoung Park·2022년 6월 30일
0

코딩테스트

목록 보기
470/631
post-thumbnail

1. 문제 설명

게임 맵 최단거리

2. 문제 분석

일반적인 BFS 문제.

3. 나의 풓이

from collections import deque

def solution(maps):
    dx = [0, 0, 1, -1]
    dy = [1, -1, 0, 0]
    n = len(maps)
    m = len(maps[0])
    
    def BFS():
        visited = [[False for _ in range(m)] for _ in range(n)]
        visited[0][0] = True
        queue = deque()
        queue.append([0, 0, 1])
        
        while queue:
            cur_row, cur_col, cur_cost = queue.popleft()
            
            if cur_row == n-1 and cur_col == m-1:
                return cur_cost
            
            for x, y in zip(dx, dy):
                next_row, next_col = cur_row + y, cur_col + x
                if next_row < 0 or next_col < 0 or next_row >= n or next_col >= m:
                    continue
                    
                if not visited[next_row][next_col] and maps[next_row][next_col] == 1:
                    visited[next_row][next_col] = True
                    queue.append([next_row, next_col, cur_cost + 1])
        
        return -1
        
    answer = BFS()
    return answer
profile
JUST DO IT

0개의 댓글