#include <bits/stdc++.h>
using namespace std ;
const int N = 801 ;
int dx[] = {-1, 0, 1, 0};
int dy[] = {0, -1, 0, 1};
int n , s ;
pair <int , int> Honey , Home ;
char a[N][N] ;
bool inside(int x , int y) {
return (x >= 0 && y >= 0 && x < n && y < n && (a[x][y] == 'G' || a[x][y] == 'M')) ;
}
bool val(int x , int y) {
return x / s < y ;
}
int ok(int time) {
int mn2[n + 4][n + 4] ;
int vis2[n + 4][n + 4] ;
fill_n(&mn2[0][0] , (n + 4) * (n + 4) , 1e9) ;
fill_n(&vis2[0][0] , (n + 4) * (n + 4) , 0) ;
queue<pair <int , int>> q2 ;
for(int i = 0 ; i < n ; i++) {
for(int j = 0 ; j < n ; j++) {
if(a[i][j] == 'H') {
q2.push({i, j}) ;
mn2[i][j] = 0 ;
vis2[i][j] = 1 ;
}
}
}
while(q2.size()) {
pair <int , int> t = q2.front() ;
q2.pop() ;
for(int i = 0 ; i < 4 ; i++) {
int x = t.first + dx[i] ;
int y = t.second + dy[i] ;
if(inside(x , y) && mn2[t.first][t.second] + 1 < mn2[x][y] && !vis2[x][y]) {
mn2[x][y] = mn2[t.first][t.second] + 1 ;
q2.push({x, y}) ;
vis2[x][y] = 1 ;
}
}
}
int mn[n + 4][n + 4] ;
int vis[n + 4][n + 4] ;
fill_n(&mn[0][0] , (n + 4) * (n + 4) , 1e9 ) ;
fill_n(&vis[0][0] , (n + 4) * (n + 4) , 0) ;
queue <pair <int , int>> q ;
mn[Honey.first][Honey.second] = 0 ;
vis[Honey.first][Honey.second] = 1 ;
if(mn2[Honey.first][Honey.second] > time)
q.push(Honey) ;
while(!q.empty()) {
pair <int , int> t = q.front() ;
q.pop() ;
for(int i = 0 ; i < 4 ; i++) {
int x = t.first + dx[i] ;
int y = t.second + dy[i] ;
if(inside(x , y) && val(mn[t.first][t.second] + 1 , mn2[x][y] - time) && mn[t.first][t.second] + 1 < mn[x][y] && !vis[x][y]) {
mn[x][y] = mn[t.first][t.second] + 1 ;
q.push({x , y}) ;
vis[x][y] = 1 ;
}
}
}
for(int i = 0 ; i < 4 ; i++){
int x = Home.first + dx[i] ;
int y = Home.second + dy[i] ;
if(inside(x , y) && vis[x][y] && val(mn[x][y] , mn2[x][y] - time))
return true ;
}
return false ;
}
int main() {
cin >> n >> s ;
for(int i = 0 ; i < n ; i++) {
for(int j = 0 ; j < n ; j++) {
cin >> a[i][j] ;
if(a[i][j] == 'M')
Honey = {i , j} ;
if(a[i][j] == 'D')
Home = {i , j} ;
}
}
int l = 0 , r = n * n + 4 ;
while(l <= r) {
int m = l + (r - l) / 2 ;
if(ok(m))
l = m + 1 ;
else
r = m - 1 ;
}
cout << l - 1 << endl ;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
334 ms |
11276 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
560 KB |
Output is correct |
29 |
Correct |
1 ms |
348 KB |
Output is correct |
30 |
Correct |
1 ms |
348 KB |
Output is correct |
31 |
Correct |
1 ms |
348 KB |
Output is correct |
32 |
Correct |
1 ms |
348 KB |
Output is correct |
33 |
Correct |
25 ms |
2652 KB |
Output is correct |
34 |
Correct |
24 ms |
2652 KB |
Output is correct |
35 |
Correct |
45 ms |
2652 KB |
Output is correct |
36 |
Correct |
34 ms |
3164 KB |
Output is correct |
37 |
Correct |
30 ms |
3164 KB |
Output is correct |
38 |
Correct |
62 ms |
3304 KB |
Output is correct |
39 |
Correct |
46 ms |
3928 KB |
Output is correct |
40 |
Correct |
38 ms |
3928 KB |
Output is correct |
41 |
Correct |
82 ms |
3940 KB |
Output is correct |
42 |
Correct |
55 ms |
4696 KB |
Output is correct |
43 |
Correct |
51 ms |
4696 KB |
Output is correct |
44 |
Correct |
96 ms |
4596 KB |
Output is correct |
45 |
Correct |
67 ms |
5468 KB |
Output is correct |
46 |
Correct |
59 ms |
5468 KB |
Output is correct |
47 |
Correct |
112 ms |
5652 KB |
Output is correct |
48 |
Correct |
79 ms |
6488 KB |
Output is correct |
49 |
Correct |
66 ms |
6488 KB |
Output is correct |
50 |
Correct |
135 ms |
6604 KB |
Output is correct |
51 |
Correct |
106 ms |
7512 KB |
Output is correct |
52 |
Correct |
84 ms |
7512 KB |
Output is correct |
53 |
Correct |
171 ms |
7516 KB |
Output is correct |
54 |
Correct |
111 ms |
8536 KB |
Output is correct |
55 |
Correct |
94 ms |
8532 KB |
Output is correct |
56 |
Correct |
205 ms |
8532 KB |
Output is correct |
57 |
Correct |
126 ms |
9808 KB |
Output is correct |
58 |
Correct |
110 ms |
9808 KB |
Output is correct |
59 |
Correct |
229 ms |
9812 KB |
Output is correct |
60 |
Correct |
149 ms |
11088 KB |
Output is correct |
61 |
Correct |
124 ms |
11088 KB |
Output is correct |
62 |
Correct |
292 ms |
11092 KB |
Output is correct |
63 |
Correct |
350 ms |
11156 KB |
Output is correct |
64 |
Correct |
432 ms |
10980 KB |
Output is correct |
65 |
Correct |
380 ms |
11092 KB |
Output is correct |
66 |
Correct |
353 ms |
11092 KB |
Output is correct |
67 |
Correct |
352 ms |
11092 KB |
Output is correct |
68 |
Correct |
341 ms |
11012 KB |
Output is correct |
69 |
Correct |
375 ms |
11088 KB |
Output is correct |
70 |
Correct |
333 ms |
11092 KB |
Output is correct |
71 |
Correct |
358 ms |
11092 KB |
Output is correct |
72 |
Correct |
326 ms |
11188 KB |
Output is correct |
73 |
Correct |
303 ms |
11344 KB |
Output is correct |
74 |
Correct |
312 ms |
11344 KB |
Output is correct |
75 |
Correct |
345 ms |
11348 KB |
Output is correct |
76 |
Correct |
334 ms |
11252 KB |
Output is correct |
77 |
Correct |
325 ms |
11440 KB |
Output is correct |
78 |
Correct |
323 ms |
11392 KB |
Output is correct |
79 |
Correct |
304 ms |
11248 KB |
Output is correct |
80 |
Correct |
315 ms |
11344 KB |
Output is correct |
81 |
Correct |
313 ms |
11228 KB |
Output is correct |
82 |
Correct |
333 ms |
11416 KB |
Output is correct |
83 |
Correct |
330 ms |
11192 KB |
Output is correct |
84 |
Correct |
335 ms |
11348 KB |
Output is correct |
85 |
Correct |
363 ms |
11344 KB |
Output is correct |
86 |
Correct |
325 ms |
11372 KB |
Output is correct |
87 |
Correct |
329 ms |
11344 KB |
Output is correct |
88 |
Correct |
377 ms |
11088 KB |
Output is correct |
89 |
Correct |
327 ms |
11088 KB |
Output is correct |
90 |
Correct |
335 ms |
11324 KB |
Output is correct |
91 |
Correct |
335 ms |
11324 KB |
Output is correct |
92 |
Correct |
366 ms |
11100 KB |
Output is correct |