Skip to content

Commit ad40ec0

Browse files
committed
pg - 150365
1 parent ec0020a commit ad40ec0

1 file changed

Lines changed: 38 additions & 0 deletions

File tree

python/implement/pg_150365

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# https://school.programmers.co.kr/learn/courses/30/lessons/150365
2+
# 미로 탈출 명령어
3+
4+
def solution(n, m, x, y, r, c, k):
5+
# 맨해튼 거리
6+
def reachable(y, x, now):
7+
distance = abs(y-r) + abs(x-c)
8+
# 남은 거리가 맨해튼 거리보다 크거나 같아야 함. 안 그러면 도달할 수 없는 거리
9+
# 남은 거리 - 맨해튼 거리가 짝수여야 함. 홀수일 경우는 도달할 수 없기 때문
10+
if now >= distance and (now-distance) % 2 == 0:
11+
return True
12+
return False
13+
14+
# 알파벳, 시작y, 시작x, 남은 거리
15+
stack = [("", x, y, k)]
16+
17+
while stack:
18+
alpha, nowY, nowX, nowDistance = stack.pop()
19+
20+
# 모두 돌았을 경우, 도착 위치라면
21+
if nowDistance == 0:
22+
if nowY == r and nowX == c:
23+
return alpha
24+
continue
25+
26+
# d, l, r, u 순으로 돌아야 하는데
27+
# d를 가장 마지막에 넣어 주어야 바로 탐색 가능하기 때문에 뒤집어서 넣음
28+
for (dy, dx, nowAlpha) in [(-1, 0, "u"), (0, 1, "r"), (0, -1, "l"), (1, 0, "d")]:
29+
ny = dy+nowY
30+
nx = dx+nowX
31+
na = alpha+nowAlpha
32+
33+
# 범위 안에 있고
34+
if 1 <= ny <= n and 1 <= nx <= m:
35+
# 남은 범위 내로 도착할 수 있는 경로일 때
36+
if reachable(ny, nx, nowDistance-1):
37+
stack.append((na, ny, nx, nowDistance-1))
38+

0 commit comments

Comments
 (0)