알고리즘/프로그래머스문제풀이

[Python/프로그래머스]게임 맵 최단거리_(BFS)

개발자 덕구🐾 2022. 9. 22. 14:09
728x90

 

 

https://school.programmers.co.kr/learn/courses/30/lessons/1844

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

 

 

 

정말 쉽다.

그냥 BFS 

 

 

from collections import deque
dx = [0,1,0,-1]
dy = [1,0,-1,0]

def solution(maps):
    q = deque([[0,0]])
    n,m = len(maps), len(maps[0])
    
    while q :
        x,y = q.popleft()
        for i in range(4) :
            nx,ny= x + dx[i], y + dy[i]
            if 0<=nx<n and 0<=ny<m and maps[nx][ny] ==1 :
                maps[nx][ny] = maps[x][y] +1 
                q.append([nx,ny])  
                
    if maps[n-1][m-1] == 1 : return -1            
    else: return maps[n-1][m-1]

 

 

🐾 20220924

반응형