#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 = 640000;
while (lo < hi){
int mid = (lo + hi + 1) / 2;
if (possible(mid))
lo = mid;
else hi = mid - 1;
}
cout << (lo ? lo : -1) << endl;
// cout << possible(2) << endl;
// cout << home.first << " " << home.second << endl;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
600 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
2 ms |
348 KB |
Output is correct |
5 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
6 |
Correct |
81 ms |
7884 KB |
Output is correct |
7 |
Execution timed out |
1105 ms |
53008 KB |
Time limit exceeded |
8 |
Correct |
9 ms |
348 KB |
Output is correct |
9 |
Runtime error |
144 ms |
65536 KB |
Execution killed with signal 9 |
10 |
Correct |
15 ms |
1116 KB |
Output is correct |
11 |
Runtime error |
164 ms |
65536 KB |
Execution killed with signal 9 |
12 |
Correct |
13 ms |
856 KB |
Output is correct |
13 |
Incorrect |
826 ms |
1116 KB |
Output isn't correct |
14 |
Execution timed out |
1060 ms |
10684 KB |
Time limit exceeded |
15 |
Correct |
2 ms |
348 KB |
Output is correct |
16 |
Runtime error |
217 ms |
65536 KB |
Execution killed with signal 9 |
17 |
Correct |
2 ms |
348 KB |
Output is correct |
18 |
Runtime error |
181 ms |
65536 KB |
Execution killed with signal 9 |
19 |
Correct |
2 ms |
348 KB |
Output is correct |
20 |
Runtime error |
185 ms |
65536 KB |
Execution killed with signal 9 |
21 |
Correct |
4 ms |
344 KB |
Output is correct |
22 |
Runtime error |
100 ms |
65536 KB |
Execution killed with signal 9 |
23 |
Correct |
5 ms |
344 KB |
Output is correct |
24 |
Runtime error |
101 ms |
65536 KB |
Execution killed with signal 9 |
25 |
Correct |
7 ms |
604 KB |
Output is correct |
26 |
Runtime error |
107 ms |
65536 KB |
Execution killed with signal 9 |
27 |
Correct |
8 ms |
604 KB |
Output is correct |
28 |
Runtime error |
107 ms |
65536 KB |
Execution killed with signal 9 |
29 |
Correct |
9 ms |
604 KB |
Output is correct |
30 |
Runtime error |
101 ms |
65536 KB |
Execution killed with signal 9 |
31 |
Correct |
10 ms |
604 KB |
Output is correct |
32 |
Runtime error |
106 ms |
65536 KB |
Execution killed with signal 9 |
33 |
Correct |
239 ms |
5012 KB |
Output is correct |
34 |
Runtime error |
154 ms |
65536 KB |
Execution killed with signal 9 |
35 |
Runtime error |
872 ms |
65536 KB |
Execution killed with signal 9 |
36 |
Correct |
285 ms |
6228 KB |
Output is correct |
37 |
Runtime error |
174 ms |
65536 KB |
Execution killed with signal 9 |
38 |
Runtime error |
884 ms |
65536 KB |
Execution killed with signal 9 |
39 |
Correct |
370 ms |
7916 KB |
Output is correct |
40 |
Runtime error |
178 ms |
65536 KB |
Execution killed with signal 9 |
41 |
Runtime error |
875 ms |
65536 KB |
Execution killed with signal 9 |
42 |
Correct |
496 ms |
9628 KB |
Output is correct |
43 |
Runtime error |
199 ms |
65536 KB |
Execution killed with signal 9 |
44 |
Runtime error |
871 ms |
65536 KB |
Execution killed with signal 9 |
45 |
Correct |
556 ms |
11600 KB |
Output is correct |
46 |
Runtime error |
213 ms |
65536 KB |
Execution killed with signal 9 |
47 |
Runtime error |
883 ms |
65536 KB |
Execution killed with signal 9 |
48 |
Correct |
752 ms |
13588 KB |
Output is correct |
49 |
Runtime error |
212 ms |
65536 KB |
Execution killed with signal 9 |
50 |
Runtime error |
883 ms |
65536 KB |
Execution killed with signal 9 |
51 |
Correct |
885 ms |
15952 KB |
Output is correct |
52 |
Runtime error |
254 ms |
65536 KB |
Execution killed with signal 9 |
53 |
Runtime error |
866 ms |
65536 KB |
Execution killed with signal 9 |
54 |
Execution timed out |
1041 ms |
18260 KB |
Time limit exceeded |
55 |
Runtime error |
287 ms |
65536 KB |
Execution killed with signal 9 |
56 |
Runtime error |
861 ms |
65536 KB |
Execution killed with signal 9 |
57 |
Execution timed out |
1085 ms |
20840 KB |
Time limit exceeded |
58 |
Runtime error |
279 ms |
65536 KB |
Execution killed with signal 9 |
59 |
Runtime error |
899 ms |
65536 KB |
Execution killed with signal 9 |
60 |
Execution timed out |
1062 ms |
23380 KB |
Time limit exceeded |
61 |
Runtime error |
321 ms |
65536 KB |
Execution killed with signal 9 |
62 |
Runtime error |
857 ms |
65536 KB |
Execution killed with signal 9 |
63 |
Execution timed out |
1051 ms |
27328 KB |
Time limit exceeded |
64 |
Execution timed out |
1044 ms |
27536 KB |
Time limit exceeded |
65 |
Execution timed out |
1090 ms |
27548 KB |
Time limit exceeded |
66 |
Execution timed out |
1050 ms |
27308 KB |
Time limit exceeded |
67 |
Execution timed out |
1049 ms |
27368 KB |
Time limit exceeded |
68 |
Execution timed out |
1040 ms |
27472 KB |
Time limit exceeded |
69 |
Execution timed out |
1062 ms |
27476 KB |
Time limit exceeded |
70 |
Execution timed out |
1030 ms |
27396 KB |
Time limit exceeded |
71 |
Execution timed out |
1050 ms |
27492 KB |
Time limit exceeded |
72 |
Execution timed out |
1032 ms |
27392 KB |
Time limit exceeded |
73 |
Execution timed out |
1053 ms |
42728 KB |
Time limit exceeded |
74 |
Execution timed out |
1032 ms |
42468 KB |
Time limit exceeded |
75 |
Execution timed out |
1066 ms |
43112 KB |
Time limit exceeded |
76 |
Execution timed out |
1052 ms |
42980 KB |
Time limit exceeded |
77 |
Execution timed out |
1075 ms |
42908 KB |
Time limit exceeded |
78 |
Execution timed out |
1035 ms |
44536 KB |
Time limit exceeded |
79 |
Execution timed out |
1069 ms |
45380 KB |
Time limit exceeded |
80 |
Execution timed out |
1043 ms |
43236 KB |
Time limit exceeded |
81 |
Execution timed out |
1069 ms |
44836 KB |
Time limit exceeded |
82 |
Execution timed out |
1010 ms |
43376 KB |
Time limit exceeded |
83 |
Execution timed out |
1039 ms |
46144 KB |
Time limit exceeded |
84 |
Execution timed out |
1045 ms |
46396 KB |
Time limit exceeded |
85 |
Execution timed out |
1055 ms |
45836 KB |
Time limit exceeded |
86 |
Execution timed out |
1060 ms |
46576 KB |
Time limit exceeded |
87 |
Execution timed out |
1006 ms |
44268 KB |
Time limit exceeded |
88 |
Execution timed out |
1049 ms |
49104 KB |
Time limit exceeded |
89 |
Execution timed out |
1064 ms |
49564 KB |
Time limit exceeded |
90 |
Execution timed out |
1038 ms |
48636 KB |
Time limit exceeded |
91 |
Execution timed out |
1043 ms |
48780 KB |
Time limit exceeded |
92 |
Execution timed out |
1048 ms |
48684 KB |
Time limit exceeded |