#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}) ;
}
}
}
if(mn[Home.first][Home.second] == 1e9)
return 0 ;
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 |
Incorrect |
0 ms |
344 KB |
Output isn't correct |
2 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
3 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
4 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
5 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
6 |
Incorrect |
0 ms |
428 KB |
Output isn't correct |
7 |
Incorrect |
291 ms |
5980 KB |
Output isn't correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
10 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
11 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
12 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
13 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
16 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
17 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
18 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
19 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
20 |
Incorrect |
1 ms |
472 KB |
Output isn't correct |
21 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
22 |
Incorrect |
1 ms |
600 KB |
Output isn't correct |
23 |
Incorrect |
1 ms |
344 KB |
Output isn't correct |
24 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
25 |
Incorrect |
1 ms |
344 KB |
Output isn't correct |
26 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
27 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
28 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
29 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
30 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
31 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
32 |
Incorrect |
2 ms |
348 KB |
Output isn't correct |
33 |
Incorrect |
29 ms |
1428 KB |
Output isn't correct |
34 |
Incorrect |
33 ms |
1624 KB |
Output isn't correct |
35 |
Incorrect |
38 ms |
1628 KB |
Output isn't correct |
36 |
Incorrect |
39 ms |
1884 KB |
Output isn't correct |
37 |
Incorrect |
44 ms |
1884 KB |
Output isn't correct |
38 |
Incorrect |
55 ms |
1880 KB |
Output isn't correct |
39 |
Incorrect |
59 ms |
2140 KB |
Output isn't correct |
40 |
Incorrect |
55 ms |
2136 KB |
Output isn't correct |
41 |
Incorrect |
67 ms |
2364 KB |
Output isn't correct |
42 |
Incorrect |
61 ms |
2648 KB |
Output isn't correct |
43 |
Incorrect |
70 ms |
2652 KB |
Output isn't correct |
44 |
Incorrect |
91 ms |
2652 KB |
Output isn't correct |
45 |
Incorrect |
81 ms |
3164 KB |
Output isn't correct |
46 |
Incorrect |
87 ms |
3076 KB |
Output isn't correct |
47 |
Incorrect |
106 ms |
3160 KB |
Output isn't correct |
48 |
Incorrect |
98 ms |
3672 KB |
Output isn't correct |
49 |
Incorrect |
105 ms |
3672 KB |
Output isn't correct |
50 |
Incorrect |
125 ms |
3676 KB |
Output isn't correct |
51 |
Incorrect |
107 ms |
4184 KB |
Output isn't correct |
52 |
Incorrect |
130 ms |
4184 KB |
Output isn't correct |
53 |
Incorrect |
146 ms |
4188 KB |
Output isn't correct |
54 |
Incorrect |
123 ms |
4700 KB |
Output isn't correct |
55 |
Incorrect |
142 ms |
4696 KB |
Output isn't correct |
56 |
Incorrect |
173 ms |
4696 KB |
Output isn't correct |
57 |
Incorrect |
150 ms |
5212 KB |
Output isn't correct |
58 |
Incorrect |
172 ms |
5208 KB |
Output isn't correct |
59 |
Incorrect |
221 ms |
5208 KB |
Output isn't correct |
60 |
Incorrect |
171 ms |
6060 KB |
Output isn't correct |
61 |
Incorrect |
194 ms |
5972 KB |
Output isn't correct |
62 |
Incorrect |
261 ms |
5972 KB |
Output isn't correct |
63 |
Incorrect |
314 ms |
5972 KB |
Output isn't correct |
64 |
Incorrect |
394 ms |
5872 KB |
Output isn't correct |
65 |
Incorrect |
404 ms |
6056 KB |
Output isn't correct |
66 |
Incorrect |
359 ms |
5972 KB |
Output isn't correct |
67 |
Correct |
318 ms |
6056 KB |
Output is correct |
68 |
Incorrect |
304 ms |
5976 KB |
Output isn't correct |
69 |
Incorrect |
316 ms |
5996 KB |
Output isn't correct |
70 |
Incorrect |
298 ms |
5888 KB |
Output isn't correct |
71 |
Incorrect |
311 ms |
6084 KB |
Output isn't correct |
72 |
Incorrect |
318 ms |
5968 KB |
Output isn't correct |
73 |
Incorrect |
276 ms |
6340 KB |
Output isn't correct |
74 |
Incorrect |
338 ms |
6336 KB |
Output isn't correct |
75 |
Incorrect |
307 ms |
6224 KB |
Output isn't correct |
76 |
Incorrect |
303 ms |
6332 KB |
Output isn't correct |
77 |
Incorrect |
308 ms |
6228 KB |
Output isn't correct |
78 |
Correct |
293 ms |
6480 KB |
Output is correct |
79 |
Incorrect |
314 ms |
6224 KB |
Output isn't correct |
80 |
Incorrect |
294 ms |
6228 KB |
Output isn't correct |
81 |
Incorrect |
283 ms |
6304 KB |
Output isn't correct |
82 |
Incorrect |
296 ms |
6232 KB |
Output isn't correct |
83 |
Incorrect |
329 ms |
6112 KB |
Output isn't correct |
84 |
Incorrect |
333 ms |
6224 KB |
Output isn't correct |
85 |
Incorrect |
298 ms |
6280 KB |
Output isn't correct |
86 |
Incorrect |
316 ms |
6228 KB |
Output isn't correct |
87 |
Incorrect |
330 ms |
6264 KB |
Output isn't correct |
88 |
Incorrect |
317 ms |
6224 KB |
Output isn't correct |
89 |
Incorrect |
332 ms |
6236 KB |
Output isn't correct |
90 |
Incorrect |
313 ms |
6012 KB |
Output isn't correct |
91 |
Incorrect |
320 ms |
6100 KB |
Output isn't correct |
92 |
Incorrect |
310 ms |
6220 KB |
Output isn't correct |