#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;
}
//precompute how far every cell is to the nearest hive
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' or grid[x][y] == 'M') 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;
//time it takes for bees is floor(bearDist/s)
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;
//check if bees can reach mecho before he even moves
int hiveToStart = hiveDist[start.first][start.second];
if (wait >= hiveToStart and hiveToStart != -1) return false;
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' or grid[x][y] == 'D') and bearDist[x][y] == -1){
bearDist[x][y] = bearDist[loc.first][loc.second]+1;
//ok, mecho reached home
if (grid[x][y] == 'D') return true;
//uh oh, bees can reach here before you can
if (floor(bearDist[x][y]/s) >= hiveDist[x][y] and hiveDist[x][y] != -1) continue;
//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();
//impossible
if (!bear(0)){
cout<<-1; return 0;
}
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;
| ^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2764 KB |
Output is correct |
2 |
Correct |
2 ms |
2764 KB |
Output is correct |
3 |
Correct |
3 ms |
2764 KB |
Output is correct |
4 |
Correct |
2 ms |
2764 KB |
Output is correct |
5 |
Correct |
2 ms |
2764 KB |
Output is correct |
6 |
Correct |
2 ms |
2764 KB |
Output is correct |
7 |
Correct |
164 ms |
6048 KB |
Output is correct |
8 |
Correct |
2 ms |
2768 KB |
Output is correct |
9 |
Correct |
2 ms |
2764 KB |
Output is correct |
10 |
Correct |
2 ms |
2764 KB |
Output is correct |
11 |
Correct |
2 ms |
2764 KB |
Output is correct |
12 |
Correct |
2 ms |
2764 KB |
Output is correct |
13 |
Correct |
2 ms |
2764 KB |
Output is correct |
14 |
Correct |
3 ms |
2764 KB |
Output is 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 |
2764 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 |
2764 KB |
Output is correct |
24 |
Correct |
2 ms |
2764 KB |
Output is correct |
25 |
Correct |
2 ms |
2764 KB |
Output is correct |
26 |
Correct |
3 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 |
2764 KB |
Output is correct |
30 |
Correct |
2 ms |
2764 KB |
Output is correct |
31 |
Correct |
3 ms |
2764 KB |
Output is correct |
32 |
Correct |
3 ms |
2764 KB |
Output is correct |
33 |
Correct |
14 ms |
3460 KB |
Output is correct |
34 |
Correct |
12 ms |
3532 KB |
Output is correct |
35 |
Correct |
36 ms |
3540 KB |
Output is correct |
36 |
Correct |
15 ms |
3632 KB |
Output is correct |
37 |
Correct |
16 ms |
3748 KB |
Output is correct |
38 |
Correct |
51 ms |
3636 KB |
Output is correct |
39 |
Correct |
19 ms |
3916 KB |
Output is correct |
40 |
Correct |
19 ms |
3916 KB |
Output is correct |
41 |
Correct |
66 ms |
3916 KB |
Output is correct |
42 |
Correct |
28 ms |
4172 KB |
Output is correct |
43 |
Correct |
23 ms |
4112 KB |
Output is correct |
44 |
Correct |
77 ms |
4172 KB |
Output is correct |
45 |
Correct |
27 ms |
4300 KB |
Output is correct |
46 |
Correct |
27 ms |
4316 KB |
Output is correct |
47 |
Correct |
97 ms |
4336 KB |
Output is correct |
48 |
Correct |
32 ms |
4548 KB |
Output is correct |
49 |
Correct |
34 ms |
4548 KB |
Output is correct |
50 |
Correct |
101 ms |
4684 KB |
Output is correct |
51 |
Correct |
42 ms |
4896 KB |
Output is correct |
52 |
Correct |
37 ms |
4916 KB |
Output is correct |
53 |
Correct |
130 ms |
4968 KB |
Output is correct |
54 |
Correct |
42 ms |
5200 KB |
Output is correct |
55 |
Correct |
42 ms |
5228 KB |
Output is correct |
56 |
Correct |
184 ms |
5224 KB |
Output is correct |
57 |
Correct |
53 ms |
5568 KB |
Output is correct |
58 |
Correct |
48 ms |
5464 KB |
Output is correct |
59 |
Correct |
182 ms |
5540 KB |
Output is correct |
60 |
Correct |
54 ms |
5828 KB |
Output is correct |
61 |
Correct |
58 ms |
5904 KB |
Output is correct |
62 |
Correct |
219 ms |
5912 KB |
Output is correct |
63 |
Correct |
191 ms |
5944 KB |
Output is correct |
64 |
Correct |
264 ms |
5932 KB |
Output is correct |
65 |
Correct |
256 ms |
5916 KB |
Output is correct |
66 |
Correct |
212 ms |
6088 KB |
Output is correct |
67 |
Correct |
72 ms |
5804 KB |
Output is correct |
68 |
Correct |
101 ms |
5916 KB |
Output is correct |
69 |
Correct |
95 ms |
5904 KB |
Output is correct |
70 |
Correct |
86 ms |
5828 KB |
Output is correct |
71 |
Correct |
83 ms |
5928 KB |
Output is correct |
72 |
Correct |
90 ms |
5892 KB |
Output is correct |
73 |
Correct |
85 ms |
6148 KB |
Output is correct |
74 |
Correct |
127 ms |
6112 KB |
Output is correct |
75 |
Correct |
126 ms |
6212 KB |
Output is correct |
76 |
Correct |
122 ms |
6376 KB |
Output is correct |
77 |
Correct |
130 ms |
6212 KB |
Output is correct |
78 |
Correct |
72 ms |
6112 KB |
Output is correct |
79 |
Correct |
115 ms |
6084 KB |
Output is correct |
80 |
Correct |
123 ms |
6084 KB |
Output is correct |
81 |
Correct |
169 ms |
6080 KB |
Output is correct |
82 |
Correct |
123 ms |
6180 KB |
Output is correct |
83 |
Correct |
156 ms |
6220 KB |
Output is correct |
84 |
Correct |
147 ms |
6144 KB |
Output is correct |
85 |
Correct |
134 ms |
6056 KB |
Output is correct |
86 |
Correct |
143 ms |
6084 KB |
Output is correct |
87 |
Correct |
155 ms |
6112 KB |
Output is correct |
88 |
Correct |
159 ms |
5988 KB |
Output is correct |
89 |
Correct |
161 ms |
6036 KB |
Output is correct |
90 |
Correct |
154 ms |
6088 KB |
Output is correct |
91 |
Correct |
167 ms |
6128 KB |
Output is correct |
92 |
Correct |
152 ms |
6100 KB |
Output is correct |