-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path18188.py
55 lines (41 loc) · 1.38 KB
/
18188.py
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import sys
from collections import deque
def main():
input = sys.stdin.readline
dir = {'W': [-1, 0], 'S': [1, 0], 'A': [0, -1], 'D': [0, 1]}
h, w = map(int, input().split())
arr = [list(input().strip()) for _ in range(h)]
# 다오 제거
for i in range(h):
for j in range(w):
if arr[i][j] == 'D':
arr[i][j] = '.'
sx, sy = i, j
n = int(input())
move = [list(map(str, input().split())) for _ in range(n)]
def solve(sx, sy):
q = deque()
q.append([sx, sy, ""])
visit = [[0] * w for _ in range(h)]
visit[sx][sy] = 1
while q:
i, j, cmd = q.popleft()
if arr[i][j] == 'Z':
return ['YES', cmd]
if not (visit[i][j] - 1 < len(move)):
continue
k1, k2 = move[visit[i][j] - 1]
x, y = i + dir[k1][0], j + dir[k1][1]
if (0 <= x < h and 0 <= y < w) and arr[x][y] != '@':
visit[x][y] = visit[i][j] + 1
q.append([x, y, cmd + k1])
x, y = i + dir[k2][0], j + dir[k2][1]
if (0 <= x < h and 0 <= y < w) and arr[x][y] != '@':
visit[x][y] = visit[i][j] + 1
q.append([x, y, cmd + k2])
return ['NO']
ans = solve(sx, sy)
for pp in ans:
print(pp)
if __name__ == '__main__':
main()