728x90
나의 풀이
from collections import deque
def solution(maps):
dx, dy = [1, 0, -1, 0], [0, 1, 0, -1]
n, m = len(maps), len(maps[0])
depth = [[0] * m for _ in range(n)]
queue = deque([(0, 0)])
answer = -1
depth[0][0] = 1
while queue:
x, y = queue.popleft()
if (x, y) == (n - 1, m - 1):
answer = depth[x][y]
break
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] and not depth[nx][ny]:
queue.append((nx, ny))
depth[nx][ny] = depth[x][y] + 1
return answer
반응형
'Coding Test > Programmers' 카테고리의 다른 글
[프로그래머스/파이썬] 순위 검색 (0) | 2022.06.15 |
---|---|
[프로그래머스/파이썬] 예상 대진표 (0) | 2022.06.13 |
[프로그래머스/파이썬] 빛의 경로 사이클 (0) | 2022.06.13 |
[프로그래머스/파이썬] 튜플 (0) | 2022.06.13 |
[프로그래머스/파이썬] 수식 최대화 (0) | 2022.06.13 |