#include <bits/stdc++.h>
using namespace std ;
const int N = 800 ;
int dx[]{0 , 0 , 1 , -1} ;
int dy[]{1 , -1 , 0 , 0} ;
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 + 1][n + 1] ;
fill_n(&mn2[0][0] , (n + 1) * (n + 1) , 1e9) ;
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 ;
}
}
}
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]) {
mn2[x][y] = mn2[t.first][t.second] + 1 ;
q2.push({x, y}) ;
}
}
}
int mn[n + 1][n + 1] ;
fill_n(&mn[0][0] , (n + 1) * (n + 1) , 1e9 ) ;
queue <pair <int , int>> q ;
mn[Honey.first][Honey.second] = 0 ;
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(val(mn[t.first][t.second] + 1 , mn2[x][y] - time) && inside(x , y) && mn[t.first][t.second] + 1 < mn[x][y]) {
mn[x][y] = mn[t.first][t.second] + 1 ;
q.push({x , y}) ;
}
}
}
for(int i = 0 ; i < 4 ; i++){
int x = Home.first + dx[i] ;
int y = Home.second + dy[i] ;
if(inside(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 + 1 ;
while(l <= r) {
int m = (l + r) / 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 |
1 ms |
348 KB |
Output is correct |
5 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
304 ms |
6164 KB |
Output is correct |
8 |
Correct |
1 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 |
0 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 |
2 ms |
344 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
344 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 |
600 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
348 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 |
344 KB |
Output is correct |
32 |
Correct |
1 ms |
348 KB |
Output is correct |
33 |
Correct |
22 ms |
1624 KB |
Output is correct |
34 |
Correct |
21 ms |
1628 KB |
Output is correct |
35 |
Correct |
46 ms |
1628 KB |
Output is correct |
36 |
Correct |
29 ms |
2008 KB |
Output is correct |
37 |
Correct |
29 ms |
1884 KB |
Output is correct |
38 |
Correct |
56 ms |
1888 KB |
Output is correct |
39 |
Correct |
37 ms |
2136 KB |
Output is correct |
40 |
Correct |
38 ms |
2360 KB |
Output is correct |
41 |
Correct |
76 ms |
2140 KB |
Output is correct |
42 |
Correct |
46 ms |
2648 KB |
Output is correct |
43 |
Correct |
47 ms |
2652 KB |
Output is correct |
44 |
Correct |
91 ms |
2772 KB |
Output is correct |
45 |
Correct |
61 ms |
3216 KB |
Output is correct |
46 |
Correct |
58 ms |
3212 KB |
Output is correct |
47 |
Correct |
128 ms |
3160 KB |
Output is correct |
48 |
Correct |
68 ms |
3672 KB |
Output is correct |
49 |
Correct |
71 ms |
3676 KB |
Output is correct |
50 |
Correct |
140 ms |
3600 KB |
Output is correct |
51 |
Correct |
84 ms |
4236 KB |
Output is correct |
52 |
Correct |
136 ms |
4124 KB |
Output is correct |
53 |
Correct |
166 ms |
4188 KB |
Output is correct |
54 |
Correct |
100 ms |
4824 KB |
Output is correct |
55 |
Correct |
89 ms |
4700 KB |
Output is correct |
56 |
Correct |
213 ms |
4960 KB |
Output is correct |
57 |
Correct |
120 ms |
5212 KB |
Output is correct |
58 |
Correct |
110 ms |
5200 KB |
Output is correct |
59 |
Correct |
311 ms |
5272 KB |
Output is correct |
60 |
Correct |
125 ms |
5888 KB |
Output is correct |
61 |
Correct |
115 ms |
5980 KB |
Output is correct |
62 |
Correct |
248 ms |
5980 KB |
Output is correct |
63 |
Correct |
344 ms |
5872 KB |
Output is correct |
64 |
Correct |
461 ms |
5972 KB |
Output is correct |
65 |
Correct |
362 ms |
5968 KB |
Output is correct |
66 |
Correct |
333 ms |
5972 KB |
Output is correct |
67 |
Correct |
322 ms |
6056 KB |
Output is correct |
68 |
Correct |
303 ms |
5968 KB |
Output is correct |
69 |
Correct |
331 ms |
5972 KB |
Output is correct |
70 |
Correct |
298 ms |
6088 KB |
Output is correct |
71 |
Correct |
320 ms |
5968 KB |
Output is correct |
72 |
Correct |
289 ms |
5872 KB |
Output is correct |
73 |
Correct |
279 ms |
6336 KB |
Output is correct |
74 |
Correct |
290 ms |
6228 KB |
Output is correct |
75 |
Correct |
315 ms |
6236 KB |
Output is correct |
76 |
Correct |
298 ms |
6148 KB |
Output is correct |
77 |
Correct |
309 ms |
6228 KB |
Output is correct |
78 |
Correct |
315 ms |
6228 KB |
Output is correct |
79 |
Correct |
269 ms |
6228 KB |
Output is correct |
80 |
Correct |
284 ms |
6224 KB |
Output is correct |
81 |
Correct |
281 ms |
6312 KB |
Output is correct |
82 |
Correct |
280 ms |
6224 KB |
Output is correct |
83 |
Correct |
292 ms |
6224 KB |
Output is correct |
84 |
Correct |
282 ms |
6268 KB |
Output is correct |
85 |
Correct |
308 ms |
6116 KB |
Output is correct |
86 |
Correct |
297 ms |
6080 KB |
Output is correct |
87 |
Correct |
303 ms |
6232 KB |
Output is correct |
88 |
Correct |
288 ms |
6228 KB |
Output is correct |
89 |
Correct |
295 ms |
5972 KB |
Output is correct |
90 |
Correct |
294 ms |
6224 KB |
Output is correct |
91 |
Correct |
291 ms |
5972 KB |
Output is correct |
92 |
Correct |
320 ms |
6228 KB |
Output is correct |