프로그래머스 1844 게임 맵 최단거리 풀이 ( 파이썬 )
programmers.co.kr/learn/courses/30/lessons/1844
코딩테스트 연습 - 게임 맵 최단거리
[[1,0,1,1,1],[1,0,1,0,1],[1,0,1,1,1],[1,1,1,0,1],[0,0,0,0,1]] 11 [[1,0,1,1,1],[1,0,1,0,1],[1,0,1,1,1],[1,1,1,0,0],[0,0,0,0,1]] -1
programmers.co.kr
기본적인 bfs 문제였다!
from collections import deque
def bfs(arr,x,y):
q=deque()
dx=[0,1,0,-1]
dy=[1,0,-1,0]
q.append((x,y))
while q:
cx,cy=q.popleft()
for i in range(4):
nx=cx+dx[i]
ny=cy+dy[i]
if nx<0 or ny<0 or nx>=len(arr) or ny>=len(arr[0]):
continue
if arr[nx][ny]==0:
continue
if arr[nx][ny]==1:
arr[nx][ny]=arr[cx][cy]+1
q.append((nx,ny))
return arr
def solution(maps):
answer = 0
bfs(maps,0,0)
# print(maps)
if maps[len(maps)-1][len(maps[0])-1]==1:
return -1
else:
return maps[len(maps)-1][len(maps[0])-1]
return answer
maps=[[1,0,1,1,1],[1,0,1,0,1],[1,0,1,1,1],[1,1,1,0,0],[0,0,0,0,1]]
print(solution(maps))
[ programmers ] 약수의 개수와 덧셈 77884번 ( python ) (0) | 2021.08.25 |
---|---|
[ programmers ] 베스트앨범 42579번 ( python ) (0) | 2021.08.21 |
[ programmers ] 경주로 건설 67259번 ( python ) (0) | 2021.08.17 |
[ programmers ] 괄호 회전하기 76502번 ( python ) (0) | 2021.05.16 |
[ programmers ] [3차] n 진수 게임 17687번 ( python ) (0) | 2021.05.02 |