알고리즘/소스코드
PROGRAMMERS 1844: 게임 맵 최단거리
cjw.git
2021. 4. 2. 23:16
Link : programmers.co.kr/learn/courses/30/lessons/1844
Python
더보기
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
from queue import Queue
# R D L U
direction = [(1, 0), (0, 1), (-1, 0), (0, -1)]
def ableMove(maps, x, y):
if x < 0 or y < 0 or x >= len(maps[0]) or y >= len(maps):
return False
if maps[y][x] == 0:
return False
return True
def solution(maps):
que = Queue()
que.put((0, 0, 0))
while que.qsize():
x, y, d = que.get()
if maps[y][x] != 0:
maps[y][x] = 0
if x == len(maps[0]) - 1 and y == len(maps) - 1:
return d + 1
for i in direction:
if ableMove(maps, x + i[0], y + i[1]):
que.put((x + i[0], y + i[1], d + 1))
return -1
|
cs |
FeedBack
cjw.git@gmail.com