Skip to content

Commit

Permalink
게임 맵 최단거리 문제 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
ZeVicTech committed Jan 5, 2023
1 parent 557aeeb commit 42e13c8
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions 깊이or너비 우선 탐색/게임_맵_최단거리.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
from collections import deque

def solution(maps):
dir = [(1,0),(0,1),(-1,0),(0,-1)]

n = len(maps)
m = len(maps[0])

queue = deque()
queue.append((0,0))

while queue:
row,colum = queue.popleft()
for x,y in dir:
if row+x >= 0 and row+x < n and colum + y >= 0 and colum +y < m:
if maps[row+x][colum+y] == 1:
queue.append((row+x, colum+y))
maps[row+x][colum+y] = maps[row][colum] + 1

if maps[n-1][m-1] == 1:
answer = -1
else:
answer = maps[n-1][m-1]

return answer

0 comments on commit 42e13c8

Please sign in to comment.