#include <bits/stdc++.h>
using namespace std;
int n, s;
int dx[4] = {1, -1, 0, 0}; int dy[4] = {0, 0, -1, 1};
int hiveDist[800][800];
char grid[800][800];
pair<int, int> start;
pair<int, int> dest;
vector<pair<int, int>> hives;
bool inside(int x, int y){
return x >= 0 and x < n and y >= 0 and y < n;
}
bool checkDone(int x, int y){
//check if bear is next to his home
return (abs(x-dest.first) == 1 and y-dest.second == 0) or
((x-dest.first) == 0 and abs(y-dest.second) == 1);
}
void bees(){
queue<pair<int, int>> trav;
fill(&hiveDist[0][0], &hiveDist[0][0]+sizeof(hiveDist)/sizeof(hiveDist[0][0]), -1);
for (pair<int, int> loc : hives){
trav.push(loc);
hiveDist[loc.first][loc.second] = 0;
}
while (!trav.empty()){
pair<int, int> curr = trav.front(); trav.pop();
for (int i = 0; i < 4; i++){
int x = curr.first+dx[i]; int y = curr.second+dy[i];
if (inside(x, y) and grid[x][y] == 'G' and hiveDist[x][y] == -1){
hiveDist[x][y] = hiveDist[curr.first][curr.second]+1;
trav.push({x, y});
}
}
}
}
bool bear(int wait){
queue<pair<int, int>> trav;
//floor(bearDist/s) = time
int bearDist[n][n];
fill(&bearDist[0][0], &bearDist[0][0]+sizeof(bearDist)/sizeof(bearDist[0][0]), -1);
trav.push(start); bearDist[start.first][start.second] = wait*s;
while (!trav.empty()){
pair<int, int> curr = trav.front(); trav.pop();
//smoll bfs on reachable positions
queue<pair<int, int>> pos; pos.push(curr);
while (!pos.empty()){
pair<int, int> loc = pos.front(); pos.pop();
for (int i = 0; i < 4; i++){
int x = loc.first+dx[i]; int y = loc.second+dy[i];
if (inside(x, y) and grid[x][y] == 'G' and bearDist[x][y] == -1){
bearDist[x][y] = bearDist[loc.first][loc.second]+1;
//uh oh, bees can reach here before you can
if (floor(bearDist[x][y]/s) >= hiveDist[x][y] and hiveDist[x][y] != -1) continue;
if (checkDone(x, y)) return true;
//max distance reached
int maxDist = (bearDist[curr.first][curr.second]/s)+1;
if (bearDist[x][y] == maxDist)
trav.push({x, y});
else
pos.push({x, y});
}
}
}
}
return false;
}
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') start = {i, j};
if (grid[i][j] == 'D') dest = {i, j};
if (grid[i][j] == 'H') hives.push_back({i, j});
}
}
bees();
int high = n*n; int low = 0; int ans;
while (low < high){
int mid = (low+high)/2;
if (bear(mid)){
ans = mid; low = mid+1;
}
else
high = mid;
}
cout<<ans;
}
Compilation message
mecho.cpp: In function 'int main()':
mecho.cpp:94:11: warning: 'ans' may be used uninitialized in this function [-Wmaybe-uninitialized]
94 | cout<<ans;
| ^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2764 KB |
Output is correct |
2 |
Correct |
2 ms |
2764 KB |
Output is correct |
3 |
Correct |
2 ms |
2764 KB |
Output is correct |
4 |
Correct |
2 ms |
2712 KB |
Output is correct |
5 |
Correct |
3 ms |
2764 KB |
Output is correct |
6 |
Correct |
2 ms |
2764 KB |
Output is correct |
7 |
Correct |
158 ms |
6588 KB |
Output is correct |
8 |
Incorrect |
2 ms |
2764 KB |
Output isn't correct |
9 |
Correct |
2 ms |
2764 KB |
Output is correct |
10 |
Correct |
3 ms |
2764 KB |
Output is correct |
11 |
Correct |
2 ms |
2764 KB |
Output is correct |
12 |
Incorrect |
3 ms |
2764 KB |
Output isn't correct |
13 |
Incorrect |
3 ms |
2764 KB |
Output isn't correct |
14 |
Incorrect |
3 ms |
2764 KB |
Output isn't correct |
15 |
Correct |
2 ms |
2764 KB |
Output is correct |
16 |
Correct |
2 ms |
2764 KB |
Output is correct |
17 |
Correct |
2 ms |
2764 KB |
Output is correct |
18 |
Correct |
2 ms |
2764 KB |
Output is correct |
19 |
Correct |
2 ms |
2764 KB |
Output is correct |
20 |
Correct |
2 ms |
2768 KB |
Output is correct |
21 |
Correct |
2 ms |
2764 KB |
Output is correct |
22 |
Correct |
2 ms |
2764 KB |
Output is correct |
23 |
Correct |
2 ms |
2864 KB |
Output is correct |
24 |
Correct |
2 ms |
2764 KB |
Output is correct |
25 |
Correct |
2 ms |
2764 KB |
Output is correct |
26 |
Correct |
2 ms |
2764 KB |
Output is correct |
27 |
Correct |
2 ms |
2764 KB |
Output is correct |
28 |
Correct |
2 ms |
2764 KB |
Output is correct |
29 |
Correct |
2 ms |
2872 KB |
Output is correct |
30 |
Correct |
2 ms |
2764 KB |
Output is correct |
31 |
Correct |
2 ms |
2892 KB |
Output is correct |
32 |
Correct |
3 ms |
2868 KB |
Output is correct |
33 |
Correct |
13 ms |
3648 KB |
Output is correct |
34 |
Correct |
12 ms |
3660 KB |
Output is correct |
35 |
Correct |
35 ms |
3648 KB |
Output is correct |
36 |
Correct |
15 ms |
3788 KB |
Output is correct |
37 |
Correct |
18 ms |
3768 KB |
Output is correct |
38 |
Correct |
50 ms |
3788 KB |
Output is correct |
39 |
Correct |
18 ms |
4072 KB |
Output is correct |
40 |
Correct |
18 ms |
4092 KB |
Output is correct |
41 |
Correct |
62 ms |
4140 KB |
Output is correct |
42 |
Correct |
25 ms |
4364 KB |
Output is correct |
43 |
Correct |
23 ms |
4380 KB |
Output is correct |
44 |
Correct |
74 ms |
4416 KB |
Output is correct |
45 |
Correct |
26 ms |
4676 KB |
Output is correct |
46 |
Correct |
27 ms |
4632 KB |
Output is correct |
47 |
Correct |
95 ms |
4672 KB |
Output is correct |
48 |
Correct |
32 ms |
4960 KB |
Output is correct |
49 |
Correct |
32 ms |
4932 KB |
Output is correct |
50 |
Correct |
100 ms |
5020 KB |
Output is correct |
51 |
Correct |
36 ms |
5280 KB |
Output is correct |
52 |
Correct |
37 ms |
5316 KB |
Output is correct |
53 |
Correct |
133 ms |
5360 KB |
Output is correct |
54 |
Correct |
42 ms |
5700 KB |
Output is correct |
55 |
Correct |
49 ms |
5616 KB |
Output is correct |
56 |
Correct |
151 ms |
5660 KB |
Output is correct |
57 |
Correct |
48 ms |
6084 KB |
Output is correct |
58 |
Correct |
48 ms |
6052 KB |
Output is correct |
59 |
Correct |
179 ms |
6128 KB |
Output is correct |
60 |
Correct |
54 ms |
6468 KB |
Output is correct |
61 |
Correct |
53 ms |
6420 KB |
Output is correct |
62 |
Correct |
213 ms |
6552 KB |
Output is correct |
63 |
Correct |
164 ms |
6548 KB |
Output is correct |
64 |
Correct |
248 ms |
6584 KB |
Output is correct |
65 |
Correct |
238 ms |
6548 KB |
Output is correct |
66 |
Correct |
182 ms |
6524 KB |
Output is correct |
67 |
Incorrect |
182 ms |
6496 KB |
Output isn't correct |
68 |
Correct |
95 ms |
6536 KB |
Output is correct |
69 |
Correct |
83 ms |
6656 KB |
Output is correct |
70 |
Correct |
89 ms |
6468 KB |
Output is correct |
71 |
Correct |
82 ms |
6532 KB |
Output is correct |
72 |
Incorrect |
74 ms |
6484 KB |
Output isn't correct |
73 |
Incorrect |
126 ms |
6724 KB |
Output isn't correct |
74 |
Correct |
108 ms |
6724 KB |
Output is correct |
75 |
Correct |
119 ms |
6788 KB |
Output is correct |
76 |
Correct |
114 ms |
6848 KB |
Output is correct |
77 |
Correct |
121 ms |
6776 KB |
Output is correct |
78 |
Incorrect |
146 ms |
6852 KB |
Output isn't correct |
79 |
Correct |
97 ms |
6724 KB |
Output is correct |
80 |
Correct |
114 ms |
6804 KB |
Output is correct |
81 |
Correct |
133 ms |
6852 KB |
Output is correct |
82 |
Correct |
132 ms |
6740 KB |
Output is correct |
83 |
Correct |
152 ms |
6760 KB |
Output is correct |
84 |
Correct |
128 ms |
6736 KB |
Output is correct |
85 |
Correct |
126 ms |
6724 KB |
Output is correct |
86 |
Correct |
137 ms |
6716 KB |
Output is correct |
87 |
Correct |
137 ms |
6688 KB |
Output is correct |
88 |
Correct |
147 ms |
6720 KB |
Output is correct |
89 |
Correct |
142 ms |
6716 KB |
Output is correct |
90 |
Correct |
159 ms |
6648 KB |
Output is correct |
91 |
Correct |
151 ms |
6596 KB |
Output is correct |
92 |
Correct |
143 ms |
6620 KB |
Output is correct |