#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/2; 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/2; //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 |
852 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 |
236 ms |
1724 KB |
Output is correct |
8 |
Correct |
1 ms |
852 KB |
Output is correct |
9 |
Correct |
1 ms |
852 KB |
Output is correct |
10 |
Correct |
2 ms |
852 KB |
Output is correct |
11 |
Correct |
1 ms |
852 KB |
Output is correct |
12 |
Correct |
1 ms |
980 KB |
Output is correct |
13 |
Correct |
2 ms |
1000 KB |
Output is correct |
14 |
Correct |
2 ms |
980 KB |
Output is correct |
15 |
Correct |
1 ms |
852 KB |
Output is correct |
16 |
Correct |
1 ms |
852 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 |
852 KB |
Output is correct |
20 |
Correct |
1 ms |
852 KB |
Output is correct |
21 |
Correct |
1 ms |
852 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 |
1 ms |
980 KB |
Output is correct |
26 |
Correct |
2 ms |
980 KB |
Output is correct |
27 |
Correct |
1 ms |
980 KB |
Output is correct |
28 |
Correct |
1 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 |
992 KB |
Output is correct |
32 |
Correct |
3 ms |
980 KB |
Output is correct |
33 |
Correct |
22 ms |
1220 KB |
Output is correct |
34 |
Correct |
19 ms |
1108 KB |
Output is correct |
35 |
Correct |
33 ms |
1108 KB |
Output is correct |
36 |
Correct |
27 ms |
1252 KB |
Output is correct |
37 |
Correct |
30 ms |
1268 KB |
Output is correct |
38 |
Correct |
46 ms |
1236 KB |
Output is correct |
39 |
Correct |
38 ms |
1236 KB |
Output is correct |
40 |
Correct |
34 ms |
1284 KB |
Output is correct |
41 |
Correct |
59 ms |
1236 KB |
Output is correct |
42 |
Correct |
46 ms |
1328 KB |
Output is correct |
43 |
Correct |
46 ms |
1316 KB |
Output is correct |
44 |
Correct |
78 ms |
1324 KB |
Output is correct |
45 |
Correct |
65 ms |
1360 KB |
Output is correct |
46 |
Correct |
70 ms |
1376 KB |
Output is correct |
47 |
Correct |
99 ms |
1384 KB |
Output is correct |
48 |
Correct |
76 ms |
1396 KB |
Output is correct |
49 |
Correct |
72 ms |
1396 KB |
Output is correct |
50 |
Correct |
106 ms |
1408 KB |
Output is correct |
51 |
Correct |
85 ms |
1436 KB |
Output is correct |
52 |
Correct |
81 ms |
1428 KB |
Output is correct |
53 |
Correct |
139 ms |
1452 KB |
Output is correct |
54 |
Correct |
116 ms |
1492 KB |
Output is correct |
55 |
Correct |
87 ms |
1492 KB |
Output is correct |
56 |
Correct |
183 ms |
1504 KB |
Output is correct |
57 |
Correct |
105 ms |
1516 KB |
Output is correct |
58 |
Correct |
99 ms |
1492 KB |
Output is correct |
59 |
Correct |
143 ms |
1540 KB |
Output is correct |
60 |
Correct |
118 ms |
1488 KB |
Output is correct |
61 |
Correct |
109 ms |
1612 KB |
Output is correct |
62 |
Correct |
166 ms |
1580 KB |
Output is correct |
63 |
Correct |
239 ms |
1556 KB |
Output is correct |
64 |
Correct |
269 ms |
1580 KB |
Output is correct |
65 |
Correct |
244 ms |
1560 KB |
Output is correct |
66 |
Correct |
269 ms |
1564 KB |
Output is correct |
67 |
Correct |
248 ms |
1564 KB |
Output is correct |
68 |
Correct |
255 ms |
1596 KB |
Output is correct |
69 |
Correct |
225 ms |
1612 KB |
Output is correct |
70 |
Correct |
249 ms |
1620 KB |
Output is correct |
71 |
Correct |
272 ms |
1620 KB |
Output is correct |
72 |
Correct |
303 ms |
1604 KB |
Output is correct |
73 |
Correct |
196 ms |
2016 KB |
Output is correct |
74 |
Correct |
253 ms |
2004 KB |
Output is correct |
75 |
Correct |
289 ms |
2004 KB |
Output is correct |
76 |
Correct |
306 ms |
2004 KB |
Output is correct |
77 |
Correct |
257 ms |
2004 KB |
Output is correct |
78 |
Correct |
259 ms |
1948 KB |
Output is correct |
79 |
Correct |
266 ms |
1948 KB |
Output is correct |
80 |
Correct |
263 ms |
1948 KB |
Output is correct |
81 |
Correct |
263 ms |
1944 KB |
Output is correct |
82 |
Correct |
271 ms |
1960 KB |
Output is correct |
83 |
Correct |
269 ms |
1888 KB |
Output is correct |
84 |
Correct |
294 ms |
1888 KB |
Output is correct |
85 |
Correct |
284 ms |
1908 KB |
Output is correct |
86 |
Correct |
270 ms |
2036 KB |
Output is correct |
87 |
Correct |
310 ms |
1880 KB |
Output is correct |
88 |
Correct |
256 ms |
1804 KB |
Output is correct |
89 |
Correct |
262 ms |
1804 KB |
Output is correct |
90 |
Correct |
256 ms |
1808 KB |
Output is correct |
91 |
Correct |
257 ms |
1816 KB |
Output is correct |
92 |
Correct |
261 ms |
1812 KB |
Output is correct |