#include <bits/stdc++.h>
using namespace std;
#define pi pair<int, int>
#define f first
#define s second
int N, S;
const int MN = 800;
char grid[MN][MN] = {{}};
vector<pi> hives;
pi M; pi D;
int dx[4] = {1, -1, 0, 0};
int dy[4] = {0, 0, 1, -1};
void movebees(queue<pair<pi,int>> (&b), char (&v)[MN][MN], int t){
if(b.empty()) return;
int start = b.front().s;
for(int i = 0; i < t; i++){
while(!b.empty() && b.front().s <= start + i){
pair<pi, int> x = b.front(); b.pop();
pi p = x.f; int index = x.s;
for(int d = 0; d < 4; d++){
int nx = p.f + dx[d]; int ny = p.s + dy[d];
if(nx < 0 || nx >= N || ny < 0 || ny >= N) continue;
if(v[nx][ny] != 'B' && (grid[nx][ny] == 'G' || grid[nx][ny] == 'M')){
v[nx][ny] = 'B';
b.push({{nx, ny}, index+1});
}
}
}
}
}
bool movemecho(queue<pair<pi, int>> (&m), char (&v)[MN][MN]){
if(m.empty()) return false;
int start = m.front().s;
for(int i = 0; i < S; i++){
while(!m.empty() && m.front().s <= start + i){
pair<pi, int> x = m.front(); m.pop();
pi p = x.f; int index = x.s;
if(v[p.f][p.s] == 'B') continue; //invalidate positions that are now occupied by bees
for(int d = 0; d < 4; d++){
int nx = p.f + dx[d]; int ny = p.s + dy[d];
if(nx < 0 || nx >= N || ny < 0 || ny >= N) continue;
if(!v[nx][ny] && grid[nx][ny] != 'T'){
v[nx][ny] = 'M';
if(grid[nx][ny] == 'D') return true;
m.push({{nx, ny}, index+1});
}
}
}
}
return false;
}
bool works(int t){ //O(N^2)
char visited[MN][MN] = {{}};
queue<pair<pi, int>> mecho; mecho.push({M,0}); visited[M.f][M.s] = 'M';
queue<pair<pi, int>> bees;
for(pi p : hives){
bees.push({p,0});
visited[p.first][p.second] = 'B';
}
movebees(bees, visited, t);
for(int i = 0; i < N*N; i++){
//first, move mecho S steps
if(mecho.empty()) break;
bool success = movemecho(mecho, visited);
if(success) return true;
//then, move bees 1 step
movebees(bees, visited, 1);
}
return false;
}
int main(){
cin.tie(0)->sync_with_stdio(0);
cin >> N >> S;
for(int i = 0; i < N; i++){
for(int j = 0; j < N; j++){
cin >> grid[i][j];
if(grid[i][j] == 'M') M = {i, j};
if(grid[i][j] == 'D') D = {i, j};
if(grid[i][j] == 'H') hives.push_back({i,j});
}
}
int lo = -1; int hi = N*N; //amount of head start to bees
while(lo < hi){ // in total, O(N^2 log N)
int mid = lo + (hi - lo + 1) / 2;
if(works(mid)){
lo = mid;
} else {
hi = mid - 1;
}
}
cout << lo;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
836 KB |
Output is correct |
2 |
Correct |
1 ms |
852 KB |
Output is correct |
3 |
Correct |
1 ms |
852 KB |
Output is correct |
4 |
Correct |
1 ms |
852 KB |
Output is correct |
5 |
Correct |
1 ms |
852 KB |
Output is correct |
6 |
Correct |
1 ms |
852 KB |
Output is correct |
7 |
Correct |
259 ms |
2344 KB |
Output is correct |
8 |
Correct |
1 ms |
852 KB |
Output is correct |
9 |
Correct |
1 ms |
840 KB |
Output is correct |
10 |
Correct |
1 ms |
852 KB |
Output is correct |
11 |
Correct |
1 ms |
852 KB |
Output is correct |
12 |
Correct |
3 ms |
980 KB |
Output is correct |
13 |
Correct |
2 ms |
980 KB |
Output is correct |
14 |
Correct |
3 ms |
972 KB |
Output is correct |
15 |
Correct |
1 ms |
852 KB |
Output is correct |
16 |
Correct |
1 ms |
936 KB |
Output is correct |
17 |
Correct |
1 ms |
852 KB |
Output is correct |
18 |
Correct |
1 ms |
852 KB |
Output is correct |
19 |
Correct |
1 ms |
968 KB |
Output is correct |
20 |
Correct |
1 ms |
968 KB |
Output is correct |
21 |
Correct |
1 ms |
968 KB |
Output is correct |
22 |
Correct |
1 ms |
980 KB |
Output is correct |
23 |
Correct |
1 ms |
980 KB |
Output is correct |
24 |
Correct |
1 ms |
980 KB |
Output is correct |
25 |
Correct |
2 ms |
964 KB |
Output is correct |
26 |
Correct |
2 ms |
980 KB |
Output is correct |
27 |
Correct |
2 ms |
976 KB |
Output is correct |
28 |
Correct |
2 ms |
980 KB |
Output is correct |
29 |
Correct |
2 ms |
980 KB |
Output is correct |
30 |
Correct |
2 ms |
980 KB |
Output is correct |
31 |
Correct |
2 ms |
972 KB |
Output is correct |
32 |
Correct |
2 ms |
980 KB |
Output is correct |
33 |
Correct |
23 ms |
1236 KB |
Output is correct |
34 |
Correct |
21 ms |
1236 KB |
Output is correct |
35 |
Correct |
33 ms |
1328 KB |
Output is correct |
36 |
Correct |
32 ms |
1364 KB |
Output is correct |
37 |
Correct |
31 ms |
1364 KB |
Output is correct |
38 |
Correct |
39 ms |
1408 KB |
Output is correct |
39 |
Correct |
39 ms |
1492 KB |
Output is correct |
40 |
Correct |
37 ms |
1488 KB |
Output is correct |
41 |
Correct |
54 ms |
1492 KB |
Output is correct |
42 |
Correct |
53 ms |
1476 KB |
Output is correct |
43 |
Correct |
44 ms |
1568 KB |
Output is correct |
44 |
Correct |
65 ms |
1492 KB |
Output is correct |
45 |
Correct |
72 ms |
1772 KB |
Output is correct |
46 |
Correct |
54 ms |
1660 KB |
Output is correct |
47 |
Correct |
77 ms |
1620 KB |
Output is correct |
48 |
Correct |
69 ms |
1876 KB |
Output is correct |
49 |
Correct |
65 ms |
1868 KB |
Output is correct |
50 |
Correct |
95 ms |
1760 KB |
Output is correct |
51 |
Correct |
108 ms |
1852 KB |
Output is correct |
52 |
Correct |
81 ms |
1876 KB |
Output is correct |
53 |
Correct |
115 ms |
1872 KB |
Output is correct |
54 |
Correct |
98 ms |
1876 KB |
Output is correct |
55 |
Correct |
125 ms |
1964 KB |
Output is correct |
56 |
Correct |
139 ms |
1876 KB |
Output is correct |
57 |
Correct |
112 ms |
2072 KB |
Output is correct |
58 |
Correct |
103 ms |
2004 KB |
Output is correct |
59 |
Correct |
166 ms |
2088 KB |
Output is correct |
60 |
Correct |
134 ms |
2172 KB |
Output is correct |
61 |
Correct |
117 ms |
2176 KB |
Output is correct |
62 |
Correct |
176 ms |
2212 KB |
Output is correct |
63 |
Correct |
264 ms |
2196 KB |
Output is correct |
64 |
Correct |
283 ms |
2192 KB |
Output is correct |
65 |
Correct |
268 ms |
2196 KB |
Output is correct |
66 |
Correct |
285 ms |
2240 KB |
Output is correct |
67 |
Correct |
266 ms |
2188 KB |
Output is correct |
68 |
Correct |
290 ms |
2252 KB |
Output is correct |
69 |
Correct |
238 ms |
2232 KB |
Output is correct |
70 |
Correct |
248 ms |
2232 KB |
Output is correct |
71 |
Correct |
303 ms |
2252 KB |
Output is correct |
72 |
Correct |
299 ms |
2352 KB |
Output is correct |
73 |
Correct |
226 ms |
2596 KB |
Output is correct |
74 |
Correct |
286 ms |
2752 KB |
Output is correct |
75 |
Correct |
284 ms |
2644 KB |
Output is correct |
76 |
Correct |
281 ms |
2656 KB |
Output is correct |
77 |
Correct |
292 ms |
2632 KB |
Output is correct |
78 |
Correct |
290 ms |
2568 KB |
Output is correct |
79 |
Correct |
273 ms |
2700 KB |
Output is correct |
80 |
Correct |
314 ms |
2560 KB |
Output is correct |
81 |
Correct |
291 ms |
2548 KB |
Output is correct |
82 |
Correct |
290 ms |
2576 KB |
Output is correct |
83 |
Correct |
297 ms |
2516 KB |
Output is correct |
84 |
Correct |
304 ms |
2508 KB |
Output is correct |
85 |
Correct |
291 ms |
2516 KB |
Output is correct |
86 |
Correct |
297 ms |
2580 KB |
Output is correct |
87 |
Correct |
287 ms |
2516 KB |
Output is correct |
88 |
Correct |
277 ms |
2428 KB |
Output is correct |
89 |
Correct |
266 ms |
2432 KB |
Output is correct |
90 |
Correct |
275 ms |
2340 KB |
Output is correct |
91 |
Correct |
269 ms |
2336 KB |
Output is correct |
92 |
Correct |
306 ms |
2436 KB |
Output is correct |