-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path3055.py
More file actions
62 lines (48 loc) · 1.58 KB
/
3055.py
File metadata and controls
62 lines (48 loc) · 1.58 KB
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
56
57
58
59
60
61
62
# 탈출
import sys
from collections import deque
sys.setrecursionlimit(10*6)
input = sys.stdin.readline
R, C = map(int, input().split())
forestMap = []
visited = [[False]*C for _ in range(R)]
for _ in range(R):
forestMap.append(list(input().strip()))
queue = deque()
river = deque()
dx = [1, -1, 0, 0]
dy = [0, 0, 1, -1]
escapeCave = [-1, -1]
def sol():
for i in range(R):
for j in range(C):
if forestMap[i][j] == 'S':
forestMap[i][j] == '.'
queue.append([i, j, 0])
if forestMap[i][j] == '*':
river.append([i, j])
while queue:
countRiver = len(river)
for _ in range(countRiver):
rx, ry = river.popleft()
for k in range(4):
nrx = rx + dx[k]
nry = ry + dy[k]
if 0 <= nrx < R and 0 <= nry < C and (forestMap[nrx][nry] == '.'):
visited[nrx][nry] = True
forestMap[nrx][nry] = '*'
river.append([nrx, nry])
countQueue = len(queue)
for _ in range(countQueue):
x, y, moveCount = queue.popleft()
if (forestMap[x][y] == 'D'):
return moveCount
for k in range(4):
nx = x + dx[k]
ny = y + dy[k]
if 0 <= nx < R and 0 <= ny < C and visited[nx][ny] == False and (forestMap[nx][ny] == '.' or forestMap[nx][ny] == 'D'):
visited[nx][ny] = True
queue.append([nx, ny, moveCount+1])
return 'KAKTUS'
result = sol()
print(result)