https://programmers.co.kr/learn/courses/30/lessons/1844
✅ Solution
- 최단거리 구하는 문제므로 BFS를 이용한다.
✅ Code
from collections import deque
def solution(maps):
n, m = len(maps), len(maps[0])
dx = [-1, 1, 0, 0]
dy = [0, 0, -1, 1]
queue = deque()
queue.append((0, 0))
while queue:
x, y = queue.popleft()
for i in range(4):
nx = x + dx[i]
ny = y + dy[i]
if 0 <= nx < n and 0 <= ny < m:
if maps[nx][ny] == 1:
maps[nx][ny] = maps[x][y] + 1
queue.append((nx, ny))
return maps[n - 1][m - 1] if maps[n - 1][m - 1] > 1 else -1
'코딩테스트 > 프로그래머스' 카테고리의 다른 글
[프로그래머스] Level 2 - 다리를 지나는 트럭 (Python) (0) | 2022.06.22 |
---|---|
[프로그래머스] Level 2 - 위장 (Python) (0) | 2022.06.09 |
[프로그래머스] Level 2 - 배달 (Python) (0) | 2022.06.09 |
[프로그래머스] Level 2 - 괄호 회전하기 (Python) (0) | 2022.06.09 |
[프로그래머스] Level 2 - 짝지어 제거하기 (Python) (0) | 2022.06.04 |