#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] != 'T') ;
}
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 ;
}
}
}
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}) ;
}
}
}
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 ;
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}) ;
}
}
}
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 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
2 |
Incorrect |
1 ms |
344 KB |
Output isn't correct |
3 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
4 |
Incorrect |
0 ms |
344 KB |
Output isn't correct |
5 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
6 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
7 |
Incorrect |
310 ms |
11076 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 |
1 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 |
0 ms |
348 KB |
Output isn't correct |
21 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
22 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
23 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
24 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
25 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
26 |
Incorrect |
1 ms |
344 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 |
576 KB |
Output isn't correct |
30 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
31 |
Incorrect |
2 ms |
348 KB |
Output isn't correct |
32 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
33 |
Incorrect |
30 ms |
2652 KB |
Output isn't correct |
34 |
Incorrect |
31 ms |
2652 KB |
Output isn't correct |
35 |
Incorrect |
39 ms |
2648 KB |
Output isn't correct |
36 |
Incorrect |
42 ms |
3164 KB |
Output isn't correct |
37 |
Incorrect |
52 ms |
3164 KB |
Output isn't correct |
38 |
Incorrect |
53 ms |
3164 KB |
Output isn't correct |
39 |
Incorrect |
54 ms |
3932 KB |
Output isn't correct |
40 |
Incorrect |
54 ms |
3932 KB |
Output isn't correct |
41 |
Incorrect |
76 ms |
3988 KB |
Output isn't correct |
42 |
Incorrect |
64 ms |
4696 KB |
Output isn't correct |
43 |
Incorrect |
69 ms |
4696 KB |
Output isn't correct |
44 |
Incorrect |
83 ms |
4700 KB |
Output isn't correct |
45 |
Incorrect |
83 ms |
5468 KB |
Output isn't correct |
46 |
Incorrect |
85 ms |
5644 KB |
Output isn't correct |
47 |
Incorrect |
104 ms |
5468 KB |
Output isn't correct |
48 |
Incorrect |
105 ms |
6440 KB |
Output isn't correct |
49 |
Incorrect |
98 ms |
6488 KB |
Output isn't correct |
50 |
Incorrect |
124 ms |
6488 KB |
Output isn't correct |
51 |
Incorrect |
114 ms |
7516 KB |
Output isn't correct |
52 |
Incorrect |
115 ms |
7516 KB |
Output isn't correct |
53 |
Incorrect |
163 ms |
7504 KB |
Output isn't correct |
54 |
Incorrect |
132 ms |
8532 KB |
Output isn't correct |
55 |
Incorrect |
139 ms |
8528 KB |
Output isn't correct |
56 |
Incorrect |
176 ms |
8528 KB |
Output isn't correct |
57 |
Incorrect |
160 ms |
10072 KB |
Output isn't correct |
58 |
Incorrect |
162 ms |
9896 KB |
Output isn't correct |
59 |
Incorrect |
214 ms |
10064 KB |
Output isn't correct |
60 |
Incorrect |
182 ms |
11156 KB |
Output isn't correct |
61 |
Incorrect |
203 ms |
11088 KB |
Output isn't correct |
62 |
Incorrect |
235 ms |
11092 KB |
Output isn't correct |
63 |
Incorrect |
329 ms |
10964 KB |
Output isn't correct |
64 |
Incorrect |
400 ms |
11088 KB |
Output isn't correct |
65 |
Incorrect |
389 ms |
11092 KB |
Output isn't correct |
66 |
Incorrect |
366 ms |
10976 KB |
Output isn't correct |
67 |
Correct |
335 ms |
11092 KB |
Output is correct |
68 |
Incorrect |
327 ms |
11092 KB |
Output isn't correct |
69 |
Incorrect |
329 ms |
11092 KB |
Output isn't correct |
70 |
Incorrect |
318 ms |
11188 KB |
Output isn't correct |
71 |
Incorrect |
319 ms |
11088 KB |
Output isn't correct |
72 |
Incorrect |
343 ms |
11088 KB |
Output isn't correct |
73 |
Incorrect |
285 ms |
11232 KB |
Output isn't correct |
74 |
Incorrect |
328 ms |
11348 KB |
Output isn't correct |
75 |
Incorrect |
304 ms |
11440 KB |
Output isn't correct |
76 |
Incorrect |
298 ms |
11344 KB |
Output isn't correct |
77 |
Incorrect |
316 ms |
11440 KB |
Output isn't correct |
78 |
Correct |
303 ms |
11604 KB |
Output is correct |
79 |
Incorrect |
327 ms |
11412 KB |
Output isn't correct |
80 |
Incorrect |
301 ms |
11412 KB |
Output isn't correct |
81 |
Incorrect |
295 ms |
11252 KB |
Output isn't correct |
82 |
Incorrect |
299 ms |
11348 KB |
Output isn't correct |
83 |
Incorrect |
302 ms |
11376 KB |
Output isn't correct |
84 |
Incorrect |
337 ms |
11352 KB |
Output isn't correct |
85 |
Incorrect |
328 ms |
11372 KB |
Output isn't correct |
86 |
Incorrect |
302 ms |
11192 KB |
Output isn't correct |
87 |
Incorrect |
320 ms |
11344 KB |
Output isn't correct |
88 |
Incorrect |
300 ms |
11324 KB |
Output isn't correct |
89 |
Incorrect |
337 ms |
11260 KB |
Output isn't correct |
90 |
Incorrect |
300 ms |
11140 KB |
Output isn't correct |
91 |
Incorrect |
314 ms |
11092 KB |
Output isn't correct |
92 |
Incorrect |
303 ms |
11088 KB |
Output isn't correct |