#include<bits/stdc++.h>
using namespace std;
using i64 = long long;
#define int i64
#define vi vector<int>
#define vvi vector<vi>
#define vb vector<bool>
#define sz(x) (int)(x).size()
#define pii pair<int,int>
#define fi first
#define se second
const int MAXN = 800;
const int dx[4] = {-1, 0, 1, 0};
const int dy[4] = {0, -1, 0, 1};
int n, s;
vector<string> forest(MAXN);
queue<array<int, 3>> hives;
pii start;
inline bool inside(int x, int y) {return x >= 0 && x < n && y >= 0 && y < n;}
inline bool check(int time){
vector<string> t_forest = forest;
queue<array<int, 3>> bees = hives;
queue<array<int, 3>> frontier;
vector<vb> vis(vector<vb>(n, vb(n)));
int minutes = 0;
while(!bees.empty()){
array<int, 3> b = bees.front();
if(b[2] == time)
break;
bees.pop();
for(int i = 0; i < 4; i++){
int new_x = b[0] + dx[i];
int new_y = b[1] + dy[i];
if(inside(new_x, new_y) && (t_forest[new_x][new_y] == 'G' || t_forest[new_x][new_y] == 'M')){
bees.push({new_x, new_y, b[2] + 1});
t_forest[new_x][new_y] = 'H';
}
}
if(t_forest[start.fi][start.se] == 'H')
return false;
}
frontier.push({start.fi, start.se, 0});
vis[start.fi][start.se] = true;
while(!frontier.empty()){
minutes++;
while(frontier.front()[2] < s * minutes && !frontier.empty()){
array<int, 3> c = frontier.front();
frontier.pop();
if(t_forest[c[0]][c[1]] == 'H') continue;
for(int j = 0; j < 4; j++){
int new_x = c[0] + dx[j];
int new_y = c[1] + dy[j];
if(inside(new_x, new_y) && (t_forest[new_x][new_y] == 'G' || t_forest[new_x][new_y] == 'D') && !vis[new_x][new_y]){
if(t_forest[new_x][new_y] == 'D'){
return true;
}
frontier.push({new_x, new_y, c[2] + 1});
vis[new_x][new_y] = true;
}
}
}
int size = sz(bees);
while(size--){
array<int, 3> b = bees.front();
bees.pop();
for(int i = 0; i < 4; i++){
int new_x = b[0] + dx[i];
int new_y = b[1] + dy[i];
if(inside(new_x, new_y) && (t_forest[new_x][new_y] == 'G' || t_forest[new_x][new_y] == 'M')){
bees.push({new_x, new_y, b[2] + 1});
t_forest[new_x][new_y] = 'H';
}
}
}
}
return false;
}
int binary_search(){
int le = 0, ri = 2 * n;
while(le <= ri){
int mid = le + ((ri - le) >> 1);
if(check(mid)){
le = mid + 1;
}
else{
ri = mid - 1;
}
}
return le - 1;
}
signed main(){
ios_base::sync_with_stdio(false); cin.tie(0);
cin >> n >> s;
for(int i = 0; i < n; i++){
cin >> forest[i];
for(int j = 0; j < n; j++){
if(forest[i][j] == 'H')
hives.push({i, j, 0});
else if(forest[i][j] == 'M')
start = {i, j};
}
}
cout << binary_search();
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
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 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
143 ms |
2328 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
344 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Incorrect |
1 ms |
344 KB |
Output isn't correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
596 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 |
1 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 |
0 ms |
348 KB |
Output isn't correct |
23 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
24 |
Incorrect |
1 ms |
344 KB |
Output isn't correct |
25 |
Incorrect |
0 ms |
344 KB |
Output isn't correct |
26 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
27 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
28 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
29 |
Incorrect |
0 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 |
1 ms |
348 KB |
Output isn't correct |
33 |
Incorrect |
1 ms |
860 KB |
Output isn't correct |
34 |
Incorrect |
2 ms |
860 KB |
Output isn't correct |
35 |
Correct |
15 ms |
892 KB |
Output is correct |
36 |
Incorrect |
1 ms |
860 KB |
Output isn't correct |
37 |
Incorrect |
2 ms |
860 KB |
Output isn't correct |
38 |
Correct |
21 ms |
1072 KB |
Output is correct |
39 |
Incorrect |
2 ms |
1116 KB |
Output isn't correct |
40 |
Incorrect |
2 ms |
1116 KB |
Output isn't correct |
41 |
Correct |
28 ms |
1156 KB |
Output is correct |
42 |
Incorrect |
2 ms |
1116 KB |
Output isn't correct |
43 |
Incorrect |
2 ms |
1116 KB |
Output isn't correct |
44 |
Correct |
35 ms |
1112 KB |
Output is correct |
45 |
Incorrect |
2 ms |
1368 KB |
Output isn't correct |
46 |
Incorrect |
3 ms |
1368 KB |
Output isn't correct |
47 |
Correct |
41 ms |
1368 KB |
Output is correct |
48 |
Incorrect |
3 ms |
2092 KB |
Output isn't correct |
49 |
Incorrect |
3 ms |
1368 KB |
Output isn't correct |
50 |
Correct |
51 ms |
1532 KB |
Output is correct |
51 |
Incorrect |
3 ms |
1632 KB |
Output isn't correct |
52 |
Incorrect |
3 ms |
1628 KB |
Output isn't correct |
53 |
Correct |
75 ms |
1792 KB |
Output is correct |
54 |
Incorrect |
3 ms |
1880 KB |
Output isn't correct |
55 |
Incorrect |
4 ms |
1884 KB |
Output isn't correct |
56 |
Correct |
73 ms |
1948 KB |
Output is correct |
57 |
Incorrect |
4 ms |
2080 KB |
Output isn't correct |
58 |
Incorrect |
4 ms |
2140 KB |
Output isn't correct |
59 |
Correct |
80 ms |
2156 KB |
Output is correct |
60 |
Incorrect |
4 ms |
2392 KB |
Output isn't correct |
61 |
Incorrect |
4 ms |
2140 KB |
Output isn't correct |
62 |
Correct |
99 ms |
2360 KB |
Output is correct |
63 |
Correct |
114 ms |
2332 KB |
Output is correct |
64 |
Correct |
118 ms |
2088 KB |
Output is correct |
65 |
Correct |
133 ms |
2136 KB |
Output is correct |
66 |
Correct |
120 ms |
2320 KB |
Output is correct |
67 |
Correct |
120 ms |
2356 KB |
Output is correct |
68 |
Correct |
93 ms |
2460 KB |
Output is correct |
69 |
Correct |
73 ms |
2392 KB |
Output is correct |
70 |
Correct |
88 ms |
2396 KB |
Output is correct |
71 |
Correct |
91 ms |
2652 KB |
Output is correct |
72 |
Correct |
114 ms |
2444 KB |
Output is correct |
73 |
Correct |
32 ms |
3180 KB |
Output is correct |
74 |
Correct |
139 ms |
3224 KB |
Output is correct |
75 |
Correct |
135 ms |
3288 KB |
Output is correct |
76 |
Correct |
147 ms |
3328 KB |
Output is correct |
77 |
Correct |
133 ms |
3252 KB |
Output is correct |
78 |
Correct |
135 ms |
3120 KB |
Output is correct |
79 |
Correct |
138 ms |
3188 KB |
Output is correct |
80 |
Correct |
138 ms |
3072 KB |
Output is correct |
81 |
Correct |
145 ms |
3108 KB |
Output is correct |
82 |
Correct |
140 ms |
3120 KB |
Output is correct |
83 |
Correct |
186 ms |
2908 KB |
Output is correct |
84 |
Correct |
152 ms |
2828 KB |
Output is correct |
85 |
Correct |
140 ms |
2996 KB |
Output is correct |
86 |
Correct |
146 ms |
3012 KB |
Output is correct |
87 |
Correct |
146 ms |
3028 KB |
Output is correct |
88 |
Correct |
145 ms |
2864 KB |
Output is correct |
89 |
Correct |
137 ms |
2652 KB |
Output is correct |
90 |
Correct |
146 ms |
2648 KB |
Output is correct |
91 |
Correct |
151 ms |
3296 KB |
Output is correct |
92 |
Correct |
139 ms |
2648 KB |
Output is correct |