#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 |
Incorrect |
0 ms |
348 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 |
348 KB |
Output isn't correct |
7 |
Incorrect |
37 ms |
11100 KB |
Output isn't correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
10 |
Incorrect |
0 ms |
344 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 |
Correct |
1 ms |
344 KB |
Output is correct |
14 |
Correct |
0 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 |
0 ms |
348 KB |
Output isn't correct |
21 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
22 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
23 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
24 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
25 |
Incorrect |
1 ms |
348 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 |
0 ms |
348 KB |
Output isn't correct |
31 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
32 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
33 |
Incorrect |
7 ms |
2652 KB |
Output isn't correct |
34 |
Incorrect |
7 ms |
2652 KB |
Output isn't correct |
35 |
Incorrect |
7 ms |
2652 KB |
Output isn't correct |
36 |
Incorrect |
10 ms |
3164 KB |
Output isn't correct |
37 |
Incorrect |
11 ms |
3164 KB |
Output isn't correct |
38 |
Incorrect |
9 ms |
3164 KB |
Output isn't correct |
39 |
Incorrect |
13 ms |
3928 KB |
Output isn't correct |
40 |
Incorrect |
12 ms |
3932 KB |
Output isn't correct |
41 |
Incorrect |
12 ms |
3960 KB |
Output isn't correct |
42 |
Incorrect |
15 ms |
4700 KB |
Output isn't correct |
43 |
Incorrect |
15 ms |
4808 KB |
Output isn't correct |
44 |
Incorrect |
14 ms |
4700 KB |
Output isn't correct |
45 |
Incorrect |
18 ms |
5676 KB |
Output isn't correct |
46 |
Incorrect |
20 ms |
5668 KB |
Output isn't correct |
47 |
Incorrect |
18 ms |
5664 KB |
Output isn't correct |
48 |
Incorrect |
22 ms |
6488 KB |
Output isn't correct |
49 |
Incorrect |
21 ms |
6492 KB |
Output isn't correct |
50 |
Incorrect |
21 ms |
6492 KB |
Output isn't correct |
51 |
Incorrect |
26 ms |
7516 KB |
Output isn't correct |
52 |
Incorrect |
25 ms |
7768 KB |
Output isn't correct |
53 |
Incorrect |
25 ms |
7516 KB |
Output isn't correct |
54 |
Incorrect |
28 ms |
8540 KB |
Output isn't correct |
55 |
Incorrect |
29 ms |
8540 KB |
Output isn't correct |
56 |
Incorrect |
29 ms |
8588 KB |
Output isn't correct |
57 |
Incorrect |
33 ms |
9812 KB |
Output isn't correct |
58 |
Incorrect |
43 ms |
9736 KB |
Output isn't correct |
59 |
Incorrect |
36 ms |
9820 KB |
Output isn't correct |
60 |
Incorrect |
42 ms |
11096 KB |
Output isn't correct |
61 |
Incorrect |
37 ms |
11096 KB |
Output isn't correct |
62 |
Incorrect |
39 ms |
11100 KB |
Output isn't correct |
63 |
Incorrect |
38 ms |
11092 KB |
Output isn't correct |
64 |
Incorrect |
40 ms |
11096 KB |
Output isn't correct |
65 |
Incorrect |
39 ms |
11100 KB |
Output isn't correct |
66 |
Incorrect |
39 ms |
11164 KB |
Output isn't correct |
67 |
Correct |
39 ms |
11088 KB |
Output is correct |
68 |
Incorrect |
43 ms |
10972 KB |
Output isn't correct |
69 |
Incorrect |
43 ms |
11172 KB |
Output isn't correct |
70 |
Incorrect |
43 ms |
11092 KB |
Output isn't correct |
71 |
Incorrect |
40 ms |
11092 KB |
Output isn't correct |
72 |
Incorrect |
43 ms |
11088 KB |
Output isn't correct |
73 |
Correct |
41 ms |
11088 KB |
Output is correct |
74 |
Incorrect |
42 ms |
11088 KB |
Output isn't correct |
75 |
Incorrect |
41 ms |
11096 KB |
Output isn't correct |
76 |
Incorrect |
41 ms |
11092 KB |
Output isn't correct |
77 |
Incorrect |
45 ms |
11092 KB |
Output isn't correct |
78 |
Correct |
42 ms |
11176 KB |
Output is correct |
79 |
Incorrect |
40 ms |
11096 KB |
Output isn't correct |
80 |
Incorrect |
40 ms |
11092 KB |
Output isn't correct |
81 |
Incorrect |
41 ms |
11096 KB |
Output isn't correct |
82 |
Incorrect |
41 ms |
11096 KB |
Output isn't correct |
83 |
Incorrect |
41 ms |
11100 KB |
Output isn't correct |
84 |
Incorrect |
42 ms |
10984 KB |
Output isn't correct |
85 |
Incorrect |
39 ms |
11100 KB |
Output isn't correct |
86 |
Incorrect |
48 ms |
11100 KB |
Output isn't correct |
87 |
Incorrect |
48 ms |
11100 KB |
Output isn't correct |
88 |
Incorrect |
39 ms |
11100 KB |
Output isn't correct |
89 |
Incorrect |
39 ms |
11100 KB |
Output isn't correct |
90 |
Incorrect |
41 ms |
11184 KB |
Output isn't correct |
91 |
Incorrect |
42 ms |
11100 KB |
Output isn't correct |
92 |
Incorrect |
40 ms |
11192 KB |
Output isn't correct |