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()
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 |
51 ms |
18512 KB |
Output is correct |
2 |
Correct |
46 ms |
18700 KB |
Output is correct |
3 |
Correct |
51 ms |
18632 KB |
Output is correct |
4 |
Correct |
47 ms |
18728 KB |
Output is correct |
5 |
Correct |
54 ms |
18696 KB |
Output is correct |
6 |
Correct |
61 ms |
18764 KB |
Output is correct |
7 |
Runtime error |
296 ms |
65536 KB |
Execution killed with signal 9 |
8 |
Correct |
71 ms |
19276 KB |
Output is correct |
9 |
Correct |
58 ms |
19256 KB |
Output is correct |
10 |
Correct |
64 ms |
19272 KB |
Output is correct |
11 |
Correct |
61 ms |
19200 KB |
Output is correct |
12 |
Incorrect |
82 ms |
20668 KB |
Output isn't correct |
13 |
Incorrect |
84 ms |
22180 KB |
Output isn't correct |
14 |
Correct |
102 ms |
22168 KB |
Output is correct |
15 |
Correct |
54 ms |
19360 KB |
Output is correct |
16 |
Correct |
50 ms |
19376 KB |
Output is correct |
17 |
Correct |
58 ms |
19652 KB |
Output is correct |
18 |
Correct |
74 ms |
20352 KB |
Output is correct |
19 |
Correct |
63 ms |
19752 KB |
Output is correct |
20 |
Correct |
72 ms |
19944 KB |
Output is correct |
21 |
Correct |
90 ms |
21124 KB |
Output is correct |
22 |
Correct |
95 ms |
22040 KB |
Output is correct |
23 |
Correct |
77 ms |
20752 KB |
Output is correct |
24 |
Correct |
92 ms |
21444 KB |
Output is correct |
25 |
Correct |
81 ms |
21000 KB |
Output is correct |
26 |
Correct |
97 ms |
21788 KB |
Output is correct |
27 |
Correct |
85 ms |
21200 KB |
Output is correct |
28 |
Correct |
117 ms |
21672 KB |
Output is correct |
29 |
Correct |
88 ms |
20920 KB |
Output is correct |
30 |
Correct |
110 ms |
22572 KB |
Output is correct |
31 |
Correct |
86 ms |
20828 KB |
Output is correct |
32 |
Correct |
106 ms |
22020 KB |
Output is correct |
33 |
Correct |
161 ms |
30072 KB |
Output is correct |
34 |
Correct |
222 ms |
30684 KB |
Output is correct |
35 |
Correct |
459 ms |
36536 KB |
Output is correct |
36 |
Correct |
199 ms |
32392 KB |
Output is correct |
37 |
Correct |
290 ms |
34548 KB |
Output is correct |
38 |
Correct |
545 ms |
42540 KB |
Output is correct |
39 |
Correct |
227 ms |
34904 KB |
Output is correct |
40 |
Correct |
257 ms |
36468 KB |
Output is correct |
41 |
Correct |
685 ms |
47232 KB |
Output is correct |
42 |
Correct |
222 ms |
38172 KB |
Output is correct |
43 |
Correct |
293 ms |
41264 KB |
Output is correct |
44 |
Correct |
811 ms |
54456 KB |
Output is correct |
45 |
Correct |
270 ms |
42080 KB |
Output is correct |
46 |
Correct |
318 ms |
44320 KB |
Output is correct |
47 |
Execution timed out |
1016 ms |
59752 KB |
Time limit exceeded |
48 |
Correct |
289 ms |
46688 KB |
Output is correct |
49 |
Correct |
321 ms |
50496 KB |
Output is correct |
50 |
Execution timed out |
1056 ms |
65536 KB |
Time limit exceeded |
51 |
Correct |
315 ms |
51512 KB |
Output is correct |
52 |
Correct |
427 ms |
52164 KB |
Output is correct |
53 |
Runtime error |
705 ms |
65536 KB |
Execution killed with signal 9 |
54 |
Correct |
385 ms |
54708 KB |
Output is correct |
55 |
Correct |
422 ms |
58776 KB |
Output is correct |
56 |
Runtime error |
402 ms |
65536 KB |
Execution killed with signal 9 |
57 |
Correct |
399 ms |
64732 KB |
Output is correct |
58 |
Correct |
489 ms |
64636 KB |
Output is correct |
59 |
Runtime error |
301 ms |
65536 KB |
Execution killed with signal 9 |
60 |
Correct |
408 ms |
65536 KB |
Output is correct |
61 |
Runtime error |
277 ms |
65536 KB |
Execution killed with signal 9 |
62 |
Runtime error |
302 ms |
65536 KB |
Execution killed with signal 9 |
63 |
Runtime error |
286 ms |
65536 KB |
Execution killed with signal 9 |
64 |
Runtime error |
310 ms |
65536 KB |
Execution killed with signal 9 |
65 |
Runtime error |
292 ms |
65536 KB |
Execution killed with signal 9 |
66 |
Runtime error |
243 ms |
65536 KB |
Execution killed with signal 9 |
67 |
Runtime error |
315 ms |
65536 KB |
Execution killed with signal 9 |
68 |
Runtime error |
221 ms |
65536 KB |
Execution killed with signal 9 |
69 |
Runtime error |
238 ms |
65536 KB |
Execution killed with signal 9 |
70 |
Runtime error |
217 ms |
65536 KB |
Execution killed with signal 9 |
71 |
Runtime error |
222 ms |
65536 KB |
Execution killed with signal 9 |
72 |
Runtime error |
246 ms |
65536 KB |
Execution killed with signal 9 |
73 |
Runtime error |
192 ms |
65536 KB |
Execution killed with signal 9 |
74 |
Runtime error |
211 ms |
65536 KB |
Execution killed with signal 9 |
75 |
Runtime error |
231 ms |
65536 KB |
Execution killed with signal 9 |
76 |
Runtime error |
196 ms |
65536 KB |
Execution killed with signal 9 |
77 |
Runtime error |
212 ms |
65536 KB |
Execution killed with signal 9 |
78 |
Runtime error |
238 ms |
65536 KB |
Execution killed with signal 9 |
79 |
Runtime error |
274 ms |
65536 KB |
Execution killed with signal 9 |
80 |
Runtime error |
254 ms |
65536 KB |
Execution killed with signal 9 |
81 |
Runtime error |
240 ms |
65536 KB |
Execution killed with signal 9 |
82 |
Runtime error |
265 ms |
65536 KB |
Execution killed with signal 9 |
83 |
Runtime error |
234 ms |
65536 KB |
Execution killed with signal 9 |
84 |
Runtime error |
251 ms |
65536 KB |
Execution killed with signal 9 |
85 |
Runtime error |
256 ms |
65536 KB |
Execution killed with signal 9 |
86 |
Runtime error |
229 ms |
65536 KB |
Execution killed with signal 9 |
87 |
Runtime error |
261 ms |
65536 KB |
Execution killed with signal 9 |
88 |
Runtime error |
220 ms |
65536 KB |
Execution killed with signal 9 |
89 |
Runtime error |
236 ms |
65536 KB |
Execution killed with signal 9 |
90 |
Runtime error |
224 ms |
65536 KB |
Execution killed with signal 9 |
91 |
Runtime error |
260 ms |
65536 KB |
Execution killed with signal 9 |
92 |
Runtime error |
219 ms |
65536 KB |
Execution killed with signal 9 |