#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 << 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 |
2 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
444 KB |
Output is correct |
4 |
Correct |
2 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
80 ms |
8004 KB |
Output is correct |
7 |
Execution timed out |
1054 ms |
51900 KB |
Time limit exceeded |
8 |
Incorrect |
9 ms |
356 KB |
Output isn't correct |
9 |
Runtime error |
139 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 |
14 ms |
800 KB |
Output is correct |
13 |
Correct |
825 ms |
1308 KB |
Output is correct |
14 |
Execution timed out |
1008 ms |
10764 KB |
Time limit exceeded |
15 |
Correct |
2 ms |
476 KB |
Output is correct |
16 |
Runtime error |
216 ms |
65536 KB |
Execution killed with signal 9 |
17 |
Correct |
2 ms |
344 KB |
Output is correct |
18 |
Runtime error |
181 ms |
65536 KB |
Execution killed with signal 9 |
19 |
Correct |
3 ms |
344 KB |
Output is correct |
20 |
Runtime error |
186 ms |
65536 KB |
Execution killed with signal 9 |
21 |
Correct |
4 ms |
344 KB |
Output is correct |
22 |
Runtime error |
110 ms |
65536 KB |
Execution killed with signal 9 |
23 |
Correct |
5 ms |
348 KB |
Output is correct |
24 |
Runtime error |
100 ms |
65536 KB |
Execution killed with signal 9 |
25 |
Correct |
8 ms |
604 KB |
Output is correct |
26 |
Runtime error |
104 ms |
65536 KB |
Execution killed with signal 9 |
27 |
Correct |
8 ms |
600 KB |
Output is correct |
28 |
Runtime error |
103 ms |
65536 KB |
Execution killed with signal 9 |
29 |
Correct |
9 ms |
600 KB |
Output is correct |
30 |
Runtime error |
106 ms |
65536 KB |
Execution killed with signal 9 |
31 |
Correct |
10 ms |
600 KB |
Output is correct |
32 |
Runtime error |
103 ms |
65536 KB |
Execution killed with signal 9 |
33 |
Correct |
239 ms |
4952 KB |
Output is correct |
34 |
Runtime error |
157 ms |
65536 KB |
Execution killed with signal 9 |
35 |
Runtime error |
872 ms |
65536 KB |
Execution killed with signal 9 |
36 |
Correct |
244 ms |
6620 KB |
Output is correct |
37 |
Runtime error |
176 ms |
65536 KB |
Execution killed with signal 9 |
38 |
Runtime error |
883 ms |
65536 KB |
Execution killed with signal 9 |
39 |
Correct |
364 ms |
8016 KB |
Output is correct |
40 |
Runtime error |
174 ms |
65536 KB |
Execution killed with signal 9 |
41 |
Runtime error |
850 ms |
65536 KB |
Execution killed with signal 9 |
42 |
Correct |
483 ms |
9948 KB |
Output is correct |
43 |
Runtime error |
201 ms |
65536 KB |
Execution killed with signal 9 |
44 |
Runtime error |
848 ms |
65536 KB |
Execution killed with signal 9 |
45 |
Correct |
520 ms |
11548 KB |
Output is correct |
46 |
Runtime error |
216 ms |
65536 KB |
Execution killed with signal 9 |
47 |
Runtime error |
841 ms |
65536 KB |
Execution killed with signal 9 |
48 |
Correct |
738 ms |
13732 KB |
Output is correct |
49 |
Runtime error |
215 ms |
65536 KB |
Execution killed with signal 9 |
50 |
Runtime error |
837 ms |
65536 KB |
Execution killed with signal 9 |
51 |
Correct |
779 ms |
15696 KB |
Output is correct |
52 |
Runtime error |
238 ms |
65536 KB |
Execution killed with signal 9 |
53 |
Runtime error |
827 ms |
65536 KB |
Execution killed with signal 9 |
54 |
Execution timed out |
1037 ms |
18252 KB |
Time limit exceeded |
55 |
Runtime error |
274 ms |
65536 KB |
Execution killed with signal 9 |
56 |
Runtime error |
864 ms |
65536 KB |
Execution killed with signal 9 |
57 |
Execution timed out |
1069 ms |
20832 KB |
Time limit exceeded |
58 |
Runtime error |
273 ms |
65536 KB |
Execution killed with signal 9 |
59 |
Runtime error |
866 ms |
65536 KB |
Execution killed with signal 9 |
60 |
Execution timed out |
1064 ms |
23356 KB |
Time limit exceeded |
61 |
Runtime error |
308 ms |
65536 KB |
Execution killed with signal 9 |
62 |
Runtime error |
861 ms |
65536 KB |
Execution killed with signal 9 |
63 |
Execution timed out |
1036 ms |
27480 KB |
Time limit exceeded |
64 |
Execution timed out |
1028 ms |
27472 KB |
Time limit exceeded |
65 |
Execution timed out |
1036 ms |
27216 KB |
Time limit exceeded |
66 |
Execution timed out |
1042 ms |
27324 KB |
Time limit exceeded |
67 |
Execution timed out |
1076 ms |
27220 KB |
Time limit exceeded |
68 |
Execution timed out |
1043 ms |
27356 KB |
Time limit exceeded |
69 |
Execution timed out |
1058 ms |
27332 KB |
Time limit exceeded |
70 |
Execution timed out |
1014 ms |
27484 KB |
Time limit exceeded |
71 |
Execution timed out |
1082 ms |
27256 KB |
Time limit exceeded |
72 |
Execution timed out |
1036 ms |
27476 KB |
Time limit exceeded |
73 |
Execution timed out |
1048 ms |
42988 KB |
Time limit exceeded |
74 |
Execution timed out |
1040 ms |
42756 KB |
Time limit exceeded |
75 |
Execution timed out |
1066 ms |
43028 KB |
Time limit exceeded |
76 |
Execution timed out |
1068 ms |
43544 KB |
Time limit exceeded |
77 |
Execution timed out |
1022 ms |
42628 KB |
Time limit exceeded |
78 |
Execution timed out |
1043 ms |
43460 KB |
Time limit exceeded |
79 |
Execution timed out |
1062 ms |
44656 KB |
Time limit exceeded |
80 |
Execution timed out |
1048 ms |
44464 KB |
Time limit exceeded |
81 |
Execution timed out |
1050 ms |
44332 KB |
Time limit exceeded |
82 |
Execution timed out |
1020 ms |
44264 KB |
Time limit exceeded |
83 |
Execution timed out |
1070 ms |
46236 KB |
Time limit exceeded |
84 |
Execution timed out |
1078 ms |
47668 KB |
Time limit exceeded |
85 |
Execution timed out |
1025 ms |
45788 KB |
Time limit exceeded |
86 |
Execution timed out |
1082 ms |
47868 KB |
Time limit exceeded |
87 |
Execution timed out |
1037 ms |
46508 KB |
Time limit exceeded |
88 |
Execution timed out |
1067 ms |
50200 KB |
Time limit exceeded |
89 |
Execution timed out |
1072 ms |
50584 KB |
Time limit exceeded |
90 |
Execution timed out |
1024 ms |
48496 KB |
Time limit exceeded |
91 |
Execution timed out |
1040 ms |
49176 KB |
Time limit exceeded |
92 |
Execution timed out |
1062 ms |
49844 KB |
Time limit exceeded |