#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 = n * 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 |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
344 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
249 ms |
2452 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
600 KB |
Output is correct |
10 |
Correct |
1 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 |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
344 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
344 KB |
Output is correct |
23 |
Correct |
1 ms |
348 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 |
348 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 |
348 KB |
Output is correct |
32 |
Correct |
1 ms |
348 KB |
Output is correct |
33 |
Correct |
18 ms |
860 KB |
Output is correct |
34 |
Correct |
16 ms |
604 KB |
Output is correct |
35 |
Correct |
21 ms |
820 KB |
Output is correct |
36 |
Correct |
26 ms |
860 KB |
Output is correct |
37 |
Correct |
22 ms |
860 KB |
Output is correct |
38 |
Correct |
26 ms |
860 KB |
Output is correct |
39 |
Correct |
29 ms |
860 KB |
Output is correct |
40 |
Correct |
27 ms |
856 KB |
Output is correct |
41 |
Correct |
35 ms |
860 KB |
Output is correct |
42 |
Correct |
38 ms |
860 KB |
Output is correct |
43 |
Correct |
34 ms |
860 KB |
Output is correct |
44 |
Correct |
45 ms |
1092 KB |
Output is correct |
45 |
Correct |
48 ms |
1116 KB |
Output is correct |
46 |
Correct |
41 ms |
1116 KB |
Output is correct |
47 |
Correct |
53 ms |
1188 KB |
Output is correct |
48 |
Correct |
54 ms |
1116 KB |
Output is correct |
49 |
Correct |
56 ms |
1332 KB |
Output is correct |
50 |
Correct |
62 ms |
1336 KB |
Output is correct |
51 |
Correct |
66 ms |
1444 KB |
Output is correct |
52 |
Correct |
60 ms |
1368 KB |
Output is correct |
53 |
Correct |
74 ms |
1368 KB |
Output is correct |
54 |
Correct |
78 ms |
1368 KB |
Output is correct |
55 |
Correct |
69 ms |
1596 KB |
Output is correct |
56 |
Correct |
90 ms |
1628 KB |
Output is correct |
57 |
Correct |
89 ms |
1756 KB |
Output is correct |
58 |
Correct |
79 ms |
1624 KB |
Output is correct |
59 |
Correct |
120 ms |
1628 KB |
Output is correct |
60 |
Correct |
106 ms |
1920 KB |
Output is correct |
61 |
Correct |
91 ms |
1916 KB |
Output is correct |
62 |
Correct |
122 ms |
2136 KB |
Output is correct |
63 |
Correct |
185 ms |
1936 KB |
Output is correct |
64 |
Correct |
236 ms |
1884 KB |
Output is correct |
65 |
Correct |
206 ms |
1932 KB |
Output is correct |
66 |
Correct |
203 ms |
1936 KB |
Output is correct |
67 |
Correct |
199 ms |
1936 KB |
Output is correct |
68 |
Correct |
179 ms |
2136 KB |
Output is correct |
69 |
Correct |
160 ms |
2020 KB |
Output is correct |
70 |
Correct |
154 ms |
1880 KB |
Output is correct |
71 |
Correct |
179 ms |
2012 KB |
Output is correct |
72 |
Correct |
194 ms |
1884 KB |
Output is correct |
73 |
Correct |
21 ms |
2752 KB |
Output is correct |
74 |
Correct |
233 ms |
2896 KB |
Output is correct |
75 |
Correct |
262 ms |
2820 KB |
Output is correct |
76 |
Correct |
246 ms |
2816 KB |
Output is correct |
77 |
Correct |
238 ms |
3080 KB |
Output is correct |
78 |
Correct |
238 ms |
2652 KB |
Output is correct |
79 |
Correct |
220 ms |
2648 KB |
Output is correct |
80 |
Correct |
244 ms |
2904 KB |
Output is correct |
81 |
Correct |
231 ms |
2712 KB |
Output is correct |
82 |
Correct |
236 ms |
2652 KB |
Output is correct |
83 |
Correct |
247 ms |
2540 KB |
Output is correct |
84 |
Correct |
251 ms |
2552 KB |
Output is correct |
85 |
Correct |
234 ms |
2744 KB |
Output is correct |
86 |
Correct |
236 ms |
2584 KB |
Output is correct |
87 |
Correct |
236 ms |
2572 KB |
Output is correct |
88 |
Correct |
256 ms |
2424 KB |
Output is correct |
89 |
Correct |
229 ms |
2444 KB |
Output is correct |
90 |
Correct |
239 ms |
2640 KB |
Output is correct |
91 |
Correct |
231 ms |
2392 KB |
Output is correct |
92 |
Correct |
247 ms |
2420 KB |
Output is correct |