


from collections import deque
def solution(maps):
# global visited
answer = 0
n=len(maps)
m=len(maps[0])
# visited=[[0]*m for _ in range(n)]
dx=[-1,1,0,0]
dy=[0,0,-1,1]
deq = deque()
deq.append((0,0))
# visited[0][0] = 1
while deq:
poped = deq.popleft()
nx,ny = poped[0],poped[1]
for i in range(4):
x = nx+dx[i]
y = ny+dy[i]
if x<0 or x>=n or y<0 or y>=m:
continue
if maps[x][y] == 0:
continue
if maps[x][y] == 1:
maps[x][y] = maps[nx][ny]+1
deq.append((x,y))
if maps[n-1][m-1] == 1:
answer = -1
else:
answer = maps[n-1][m-1]
return answer
가장빨리 도달하는 타일 개수 찾기 - bfs로 풀기
dx와 dy를 두고 문제 해결.
deque 활용법.
continue 조건 해결.
반응형
'알고리즘' 카테고리의 다른 글
[이것이 코딩테스트다 - 그리디] 곱하기 혹은 더하기 (0) | 2024.03.28 |
---|---|
[이것이 코딩테스트다 - 그리디] 모험가 길드 (0) | 2024.03.28 |
[프로그래머스 고득점 kit] (완전 탐색) - 피로도 (0) | 2024.03.07 |
[프로그래머스 고득점 Kit] (완전 탐색) 소수 찾기 (0) | 2024.03.06 |
[프로그래머스 고득점 Kit] (완전탐색) - 모의고사 (2) | 2024.03.06 |