#include<bits/stdc++.h>
using namespace std;
#define vi vector<int>
#define ll long long
#define pii pair<int, int>
int n, s;
char grid[800][800];
queue<pair<int, int>> bees_start;
pair<int, int> mecho, home;
bool possible(int t){
// cout << "t = " << t << endl;
//maybe use set to avoid duplicates
map<pii, int> bee_vis;
map<pii, int> mecho_vis;
queue<pii> bees = bees_start;
queue<pii> frontier;
frontier.push(mecho);
//maybe store bee queues for every t if TLE...
int bees_grid[n][n];
memset(bees_grid, 0, sizeof(bees_grid));
while (t--){
// cout << "initial" << endl;
int sz = (int) bees.size();
for (int _ = 0; _ < sz; _++){
auto bee = bees.front();
bee_vis[bee] = 1;
bees.pop();
if (bee.first > 0 && grid[bee.first - 1][bee.second] == 'G' && !bee_vis[{bee.first - 1, bee.second}]) bees.push({bee.first - 1, bee.second}), bees_grid[bee.first - 1][bee.second] = 1;
if (bee.first < n - 1 && grid[bee.first + 1][bee.second] == 'G' && !bee_vis[{bee.first + 1, bee.second}]) bees.push({bee.first + 1, bee.second}), bees_grid[bee.first + 1][bee.second] = 1;
if (bee.second > 0 && grid[bee.first][bee.second - 1] == 'G' && !bee_vis[{bee.first, bee.second - 1}]) bees.push({bee.first, bee.second - 1}), bees_grid[bee.first][bee.second - 1] = 1;
if (bee.second < n - 1 && grid[bee.first][bee.second + 1] == 'G' && !bee_vis[{bee.first, bee.second + 1}]) bees.push({bee.first, bee.second + 1}), bees_grid[bee.first][bee.second + 1] = 1;
}
// for (int i = 0; i < n; i++){
// for (int j = 0; j < n; j++){
// cout << bees_grid[i][j] << " ";
// }
// cout << endl;
// }
// cout << endl << endl;
}
bool reached_home = false;
while (frontier.size()){
int size = (int) frontier.size();
for (int _ = 0; _ < size; _++){
// cout << frontier.size() << endl;
// cout << "iterating" << endl;
auto pos = frontier.front();
if (pos == home) return true;
frontier.pop();
if (pos.first < 0 || pos.second < 0 || pos.first > n - 1 || pos.second > n - 1 || grid[pos.first][pos.second] == 'T' || bees_grid[pos.first][pos.second]) continue;
// if (pos.second == 2 && pos.first == 2){
// cout << pos.first << " " << pos.second << endl;
// cout << bees_grid[pos.first][pos.second] << endl;
// }
mecho_vis[pos] = true;
queue<pair<pii, int>> tmp_frontier; //let him take pair.second s steps
tmp_frontier.push({pos, 0});
while (tmp_frontier.size()){
auto location = tmp_frontier.front().first;
int steps = tmp_frontier.front().second;
tmp_frontier.pop();
if (location == home) return true;
if (location.first < 0 || location.second < 0 || location.first > n - 1 || location.second > n - 1 || grid[location.first][location.second] == 'T' || bees_grid[location.first][location.second]) continue;
// if (pos.second == 2 && pos.first == 2)
// cout << location.first << " " << location.second << " with " << steps << " steps" << endl;
// mecho_vis[location] = 1;
frontier.push(location);
if (steps +1 <= s){
tmp_frontier.push({{location.first + 1, location.second}, steps + 1});
tmp_frontier.push({{location.first - 1, location.second}, steps + 1});
tmp_frontier.push({{location.first, location.second + 1}, steps + 1});
tmp_frontier.push({{location.first, location.second - 1}, steps + 1});
}
}
// for (int i = 0; i < n; i++){
// for (int j = 0; j < n; j++){
// cout << bees_grid[i][j] << " ";
// }
// cout << endl;
// }
// cout << endl << endl;
}
int sz = (int) bees.size();
for (int _ = 0; _ < sz; _++){
auto bee = bees.front();
bee_vis[bee] = 1;
bees.pop();
if (bee.first > 0 && grid[bee.first - 1][bee.second] == 'G' && !bee_vis[{bee.first - 1, bee.second}]) bees.push({bee.first - 1, bee.second}), bees_grid[bee.first - 1][bee.second] = 1;
if (bee.first < n - 1 && grid[bee.first + 1][bee.second] == 'G' && !bee_vis[{bee.first + 1, bee.second}]) bees.push({bee.first + 1, bee.second}), bees_grid[bee.first + 1][bee.second] = 1;
if (bee.second > 0 && grid[bee.first][bee.second - 1] == 'G' && !bee_vis[{bee.first, bee.second - 1}]) bees.push({bee.first, bee.second - 1}), bees_grid[bee.first][bee.second - 1] = 1;
if (bee.second < n - 1 && grid[bee.first][bee.second + 1] == 'G' && !bee_vis[{bee.first, bee.second + 1}]) bees.push({bee.first, bee.second + 1}), bees_grid[bee.first][bee.second + 1] = 1;
}
// for (int i = 0; i < n; i++){
// for (int j = 0; j < n; j++){
// cout << bees_grid[i][j] << " ";
// }
// cout << endl;
// }
// cout << endl << endl;
}
return reached_home;
}
int main(){
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') mecho = {i, j}, grid[i][j] = 'G';
if (grid[i][j] == 'D') home = {i, j};
if (grid[i][j] == 'H') bees_start.push({i, j});
}
}
int lo = 0, hi = 50;
while (lo < hi){
int mid = (lo + hi + 1) / 2;
if (possible(mid))
lo = mid;
else hi = mid - 1;
}
cout << lo << endl;
// cout << possible(2) << endl;
// cout << home.first << " " << home.second << endl;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
416 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
80 ms |
7944 KB |
Output is correct |
7 |
Execution timed out |
1062 ms |
50524 KB |
Time limit exceeded |
8 |
Incorrect |
2 ms |
348 KB |
Output isn't correct |
9 |
Correct |
111 ms |
38512 KB |
Output is correct |
10 |
Correct |
91 ms |
9700 KB |
Output is correct |
11 |
Correct |
13 ms |
3164 KB |
Output is correct |
12 |
Incorrect |
1 ms |
344 KB |
Output isn't correct |
13 |
Correct |
36 ms |
860 KB |
Output is correct |
14 |
Runtime error |
407 ms |
65536 KB |
Execution killed with signal 9 |
15 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
16 |
Runtime error |
214 ms |
65536 KB |
Execution killed with signal 9 |
17 |
Incorrect |
0 ms |
344 KB |
Output isn't correct |
18 |
Runtime error |
185 ms |
65536 KB |
Execution killed with signal 9 |
19 |
Incorrect |
0 ms |
344 KB |
Output isn't correct |
20 |
Runtime error |
203 ms |
65536 KB |
Execution killed with signal 9 |
21 |
Incorrect |
0 ms |
344 KB |
Output isn't correct |
22 |
Runtime error |
95 ms |
65536 KB |
Execution killed with signal 9 |
23 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
24 |
Runtime error |
98 ms |
65536 KB |
Execution killed with signal 9 |
25 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
26 |
Runtime error |
118 ms |
65536 KB |
Execution killed with signal 9 |
27 |
Incorrect |
1 ms |
344 KB |
Output isn't correct |
28 |
Runtime error |
102 ms |
65536 KB |
Execution killed with signal 9 |
29 |
Incorrect |
1 ms |
600 KB |
Output isn't correct |
30 |
Runtime error |
99 ms |
65536 KB |
Execution killed with signal 9 |
31 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
32 |
Runtime error |
101 ms |
65536 KB |
Execution killed with signal 9 |
33 |
Incorrect |
6 ms |
1116 KB |
Output isn't correct |
34 |
Runtime error |
103 ms |
65536 KB |
Execution killed with signal 9 |
35 |
Runtime error |
106 ms |
65536 KB |
Execution killed with signal 9 |
36 |
Incorrect |
6 ms |
1372 KB |
Output isn't correct |
37 |
Runtime error |
112 ms |
65536 KB |
Execution killed with signal 9 |
38 |
Runtime error |
106 ms |
65536 KB |
Execution killed with signal 9 |
39 |
Incorrect |
7 ms |
1368 KB |
Output isn't correct |
40 |
Runtime error |
113 ms |
65536 KB |
Execution killed with signal 9 |
41 |
Runtime error |
108 ms |
65536 KB |
Execution killed with signal 9 |
42 |
Incorrect |
9 ms |
1624 KB |
Output isn't correct |
43 |
Runtime error |
111 ms |
65536 KB |
Execution killed with signal 9 |
44 |
Runtime error |
111 ms |
65536 KB |
Execution killed with signal 9 |
45 |
Incorrect |
11 ms |
1884 KB |
Output isn't correct |
46 |
Runtime error |
106 ms |
65536 KB |
Execution killed with signal 9 |
47 |
Runtime error |
114 ms |
65536 KB |
Execution killed with signal 9 |
48 |
Incorrect |
13 ms |
2140 KB |
Output isn't correct |
49 |
Runtime error |
107 ms |
65536 KB |
Execution killed with signal 9 |
50 |
Runtime error |
114 ms |
65536 KB |
Execution killed with signal 9 |
51 |
Incorrect |
21 ms |
2472 KB |
Output isn't correct |
52 |
Runtime error |
110 ms |
65536 KB |
Execution killed with signal 9 |
53 |
Runtime error |
125 ms |
65536 KB |
Execution killed with signal 9 |
54 |
Incorrect |
17 ms |
2640 KB |
Output isn't correct |
55 |
Runtime error |
120 ms |
65536 KB |
Execution killed with signal 9 |
56 |
Runtime error |
117 ms |
65536 KB |
Execution killed with signal 9 |
57 |
Incorrect |
20 ms |
3076 KB |
Output isn't correct |
58 |
Runtime error |
113 ms |
65536 KB |
Execution killed with signal 9 |
59 |
Runtime error |
119 ms |
65536 KB |
Execution killed with signal 9 |
60 |
Incorrect |
22 ms |
3560 KB |
Output isn't correct |
61 |
Runtime error |
116 ms |
65536 KB |
Execution killed with signal 9 |
62 |
Runtime error |
123 ms |
65536 KB |
Execution killed with signal 9 |
63 |
Runtime error |
327 ms |
65536 KB |
Execution killed with signal 9 |
64 |
Runtime error |
119 ms |
65536 KB |
Execution killed with signal 9 |
65 |
Runtime error |
239 ms |
65536 KB |
Execution killed with signal 9 |
66 |
Runtime error |
245 ms |
65536 KB |
Execution killed with signal 9 |
67 |
Runtime error |
294 ms |
65536 KB |
Execution killed with signal 9 |
68 |
Runtime error |
137 ms |
65536 KB |
Execution killed with signal 9 |
69 |
Runtime error |
134 ms |
65536 KB |
Execution killed with signal 9 |
70 |
Runtime error |
177 ms |
65536 KB |
Execution killed with signal 9 |
71 |
Runtime error |
211 ms |
65536 KB |
Execution killed with signal 9 |
72 |
Runtime error |
130 ms |
65536 KB |
Execution killed with signal 9 |
73 |
Execution timed out |
1084 ms |
43756 KB |
Time limit exceeded |
74 |
Execution timed out |
1072 ms |
43488 KB |
Time limit exceeded |
75 |
Execution timed out |
1018 ms |
42132 KB |
Time limit exceeded |
76 |
Execution timed out |
1067 ms |
43460 KB |
Time limit exceeded |
77 |
Execution timed out |
1046 ms |
42716 KB |
Time limit exceeded |
78 |
Execution timed out |
1059 ms |
45040 KB |
Time limit exceeded |
79 |
Execution timed out |
1079 ms |
45508 KB |
Time limit exceeded |
80 |
Execution timed out |
1008 ms |
43812 KB |
Time limit exceeded |
81 |
Execution timed out |
1058 ms |
44340 KB |
Time limit exceeded |
82 |
Execution timed out |
1045 ms |
43532 KB |
Time limit exceeded |
83 |
Execution timed out |
1034 ms |
45872 KB |
Time limit exceeded |
84 |
Execution timed out |
1036 ms |
46404 KB |
Time limit exceeded |
85 |
Execution timed out |
1077 ms |
47756 KB |
Time limit exceeded |
86 |
Execution timed out |
1062 ms |
47308 KB |
Time limit exceeded |
87 |
Execution timed out |
1063 ms |
47300 KB |
Time limit exceeded |
88 |
Execution timed out |
1012 ms |
46468 KB |
Time limit exceeded |
89 |
Execution timed out |
1073 ms |
50476 KB |
Time limit exceeded |
90 |
Execution timed out |
1056 ms |
49084 KB |
Time limit exceeded |
91 |
Execution timed out |
1054 ms |
48716 KB |
Time limit exceeded |
92 |
Execution timed out |
1065 ms |
50388 KB |
Time limit exceeded |