문제링크: 게임 맵 최단거리
✍🏻 Information
| content | |
|---|---|
| 언어 | python |
| 난이도 | ⭐️⭐️ |
| 풀이시간 | 10분 |
| 제출횟수 | 1 |
| 인터넷검색유무 | no |
🍒 My Code
from collections import deque
def solution(maps):
#BFS
#초기화
q = deque()
n,m = len(maps),len(maps[0])
dx = [-1,1,0,0]
dy = [0,0,-1,1]
dismap = [[0]*m for i in range(n)]
checkmap = [[False]*m for i in range(n)]
#(0,0)에서 출발
q.append((0,0))
dismap[0][0]=1
checkmap[0][0]=True
#순회
while q:
x,y=q.popleft()
for i in range(4):
nextx,nexty = x+dx[i],y+dy[i]
if 0<=nextx<n and 0<=nexty<m:
if maps[nextx][nexty]==1 and checkmap[nextx][nexty]==False:
dismap[nextx][nexty]=dismap[x][y]+1
checkmap[nextx][nexty]=True
q.append((nextx,nexty))
return dismap[n-1][m-1] if dismap[n-1][m-1]!=0 else -1
💡 What I learned