from collections import *
n,s = map(int, input().split())
grid = [list(input()) for i in range(n)]
hives = []
start = []
end = []
for i in range(n):
for j in range(n):
if grid[i][j] == "M":
start = [i,j]
elif grid[i][j] == "D":
end = [i,j]
elif grid[i][j] == "H":
hives.append([i,j])
grid[i][j] = 0
def bfsinit():
d = deque()
for i in hives:
d.append(i+[0])
while len(d):
node = d.popleft()
for a,b in [(0,1), (0,-1), (1, 0), (-1, 0)]:
a += node[0]
b += node[1]
if 0<=a<n and 0<=b<n:
if grid[a][b] == "G" or grid[a][b] == "M":
d.append([a,b,node[2]+1])
grid[a][b] = node[2]+1
visited = [[False for i in range(n)] for i in range(n)]
def test(time):
d = deque()
if grid[start[0]][start[1]] <= time:
return False
for i in range(n):
for j in range(n):
visited[i][j] = False
d.append(start+[0])
while len(d):
node = d.popleft()
if node[0] == end[0] and node[1]==end[1]:
return True
for a,b in [(0,1), (0,-1), (1, 0), (-1, 0)]:
a += node[0]
b += node[1]
if 0<=a<n and 0<=b<n and not visited[a][b]:
if grid[a][b]=="T":
continue
if grid[a][b] == "D" or int(grid[a][b]) > time+(node[2]+1)//s:
d.append([a,b,node[2]+1])
visited[a][b] = True
return False
bfsinit()
lo = -1
hi = n*n
while lo<hi:
mid = lo+(hi-lo+1)//2
if test(mid):
lo = mid
else:
hi = mid-1
print(lo)
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
39 ms |
18604 KB |
Output is correct |
2 |
Correct |
41 ms |
18684 KB |
Output is correct |
3 |
Correct |
40 ms |
18516 KB |
Output is correct |
4 |
Correct |
42 ms |
18652 KB |
Output is correct |
5 |
Correct |
41 ms |
18752 KB |
Output is correct |
6 |
Correct |
44 ms |
18724 KB |
Output is correct |
7 |
Runtime error |
236 ms |
65536 KB |
Execution killed with signal 9 |
8 |
Correct |
43 ms |
18760 KB |
Output is correct |
9 |
Correct |
44 ms |
18728 KB |
Output is correct |
10 |
Correct |
45 ms |
18788 KB |
Output is correct |
11 |
Correct |
44 ms |
18760 KB |
Output is correct |
12 |
Correct |
62 ms |
20728 KB |
Output is correct |
13 |
Correct |
72 ms |
21568 KB |
Output is correct |
14 |
Correct |
80 ms |
21488 KB |
Output is correct |
15 |
Correct |
49 ms |
19376 KB |
Output is correct |
16 |
Correct |
53 ms |
19264 KB |
Output is correct |
17 |
Correct |
55 ms |
19656 KB |
Output is correct |
18 |
Correct |
57 ms |
19668 KB |
Output is correct |
19 |
Correct |
53 ms |
19620 KB |
Output is correct |
20 |
Correct |
58 ms |
19596 KB |
Output is correct |
21 |
Correct |
65 ms |
21076 KB |
Output is correct |
22 |
Correct |
75 ms |
21564 KB |
Output is correct |
23 |
Correct |
65 ms |
20440 KB |
Output is correct |
24 |
Correct |
74 ms |
21056 KB |
Output is correct |
25 |
Correct |
62 ms |
20904 KB |
Output is correct |
26 |
Correct |
85 ms |
21964 KB |
Output is correct |
27 |
Correct |
70 ms |
20972 KB |
Output is correct |
28 |
Correct |
81 ms |
21896 KB |
Output is correct |
29 |
Correct |
69 ms |
20804 KB |
Output is correct |
30 |
Correct |
83 ms |
21560 KB |
Output is correct |
31 |
Correct |
64 ms |
21020 KB |
Output is correct |
32 |
Correct |
82 ms |
21928 KB |
Output is correct |
33 |
Correct |
132 ms |
30144 KB |
Output is correct |
34 |
Correct |
161 ms |
31048 KB |
Output is correct |
35 |
Correct |
378 ms |
36800 KB |
Output is correct |
36 |
Correct |
138 ms |
32456 KB |
Output is correct |
37 |
Correct |
184 ms |
34848 KB |
Output is correct |
38 |
Correct |
418 ms |
42688 KB |
Output is correct |
39 |
Correct |
161 ms |
35228 KB |
Output is correct |
40 |
Correct |
199 ms |
37108 KB |
Output is correct |
41 |
Correct |
547 ms |
46808 KB |
Output is correct |
42 |
Correct |
182 ms |
37532 KB |
Output is correct |
43 |
Correct |
229 ms |
42148 KB |
Output is correct |
44 |
Correct |
670 ms |
53692 KB |
Output is correct |
45 |
Correct |
203 ms |
42100 KB |
Output is correct |
46 |
Correct |
243 ms |
43540 KB |
Output is correct |
47 |
Correct |
769 ms |
59952 KB |
Output is correct |
48 |
Correct |
225 ms |
46692 KB |
Output is correct |
49 |
Correct |
255 ms |
50140 KB |
Output is correct |
50 |
Runtime error |
788 ms |
65536 KB |
Execution killed with signal 9 |
51 |
Correct |
253 ms |
51516 KB |
Output is correct |
52 |
Correct |
296 ms |
52236 KB |
Output is correct |
53 |
Runtime error |
560 ms |
65536 KB |
Execution killed with signal 9 |
54 |
Correct |
282 ms |
54972 KB |
Output is correct |
55 |
Correct |
314 ms |
58256 KB |
Output is correct |
56 |
Runtime error |
386 ms |
65536 KB |
Execution killed with signal 9 |
57 |
Correct |
338 ms |
64696 KB |
Output is correct |
58 |
Correct |
376 ms |
64572 KB |
Output is correct |
59 |
Runtime error |
284 ms |
65536 KB |
Execution killed with signal 9 |
60 |
Correct |
341 ms |
65536 KB |
Output is correct |
61 |
Runtime error |
262 ms |
65536 KB |
Execution killed with signal 9 |
62 |
Runtime error |
250 ms |
65536 KB |
Execution killed with signal 9 |
63 |
Runtime error |
250 ms |
65536 KB |
Execution killed with signal 9 |
64 |
Runtime error |
272 ms |
65536 KB |
Execution killed with signal 9 |
65 |
Runtime error |
266 ms |
65536 KB |
Execution killed with signal 9 |
66 |
Runtime error |
243 ms |
65536 KB |
Execution killed with signal 9 |
67 |
Runtime error |
283 ms |
65536 KB |
Execution killed with signal 9 |
68 |
Runtime error |
209 ms |
65536 KB |
Execution killed with signal 9 |
69 |
Runtime error |
217 ms |
65536 KB |
Execution killed with signal 9 |
70 |
Runtime error |
212 ms |
65536 KB |
Execution killed with signal 9 |
71 |
Runtime error |
213 ms |
65536 KB |
Execution killed with signal 9 |
72 |
Runtime error |
214 ms |
65536 KB |
Execution killed with signal 9 |
73 |
Runtime error |
196 ms |
65536 KB |
Execution killed with signal 9 |
74 |
Runtime error |
181 ms |
65536 KB |
Execution killed with signal 9 |
75 |
Runtime error |
189 ms |
65536 KB |
Execution killed with signal 9 |
76 |
Runtime error |
189 ms |
65536 KB |
Execution killed with signal 9 |
77 |
Runtime error |
197 ms |
65536 KB |
Execution killed with signal 9 |
78 |
Runtime error |
246 ms |
65536 KB |
Execution killed with signal 9 |
79 |
Runtime error |
241 ms |
65536 KB |
Execution killed with signal 9 |
80 |
Runtime error |
234 ms |
65536 KB |
Execution killed with signal 9 |
81 |
Runtime error |
238 ms |
65536 KB |
Execution killed with signal 9 |
82 |
Runtime error |
238 ms |
65536 KB |
Execution killed with signal 9 |
83 |
Runtime error |
222 ms |
65536 KB |
Execution killed with signal 9 |
84 |
Runtime error |
245 ms |
65536 KB |
Execution killed with signal 9 |
85 |
Runtime error |
221 ms |
65536 KB |
Execution killed with signal 9 |
86 |
Runtime error |
222 ms |
65536 KB |
Execution killed with signal 9 |
87 |
Runtime error |
254 ms |
65536 KB |
Execution killed with signal 9 |
88 |
Runtime error |
221 ms |
65536 KB |
Execution killed with signal 9 |
89 |
Runtime error |
220 ms |
65536 KB |
Execution killed with signal 9 |
90 |
Runtime error |
228 ms |
65536 KB |
Execution killed with signal 9 |
91 |
Runtime error |
227 ms |
65536 KB |
Execution killed with signal 9 |
92 |
Runtime error |
235 ms |
65536 KB |
Execution killed with signal 9 |