#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];
//ok, mecho reached home
if (grid[x][y] == 'D') return true;
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;
//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 |
3 ms |
2764 KB |
Output is correct |
3 |
Correct |
2 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 |
Incorrect |
123 ms |
6028 KB |
Output isn't correct |
8 |
Correct |
2 ms |
2764 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 |
2 ms |
2764 KB |
Output is correct |
15 |
Correct |
2 ms |
2764 KB |
Output is correct |
16 |
Correct |
3 ms |
2732 KB |
Output is correct |
17 |
Correct |
3 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 |
2 ms |
2764 KB |
Output is correct |
27 |
Correct |
2 ms |
2764 KB |
Output is correct |
28 |
Correct |
3 ms |
2764 KB |
Output is correct |
29 |
Correct |
3 ms |
2764 KB |
Output is correct |
30 |
Correct |
3 ms |
2764 KB |
Output is correct |
31 |
Correct |
3 ms |
2764 KB |
Output is correct |
32 |
Correct |
4 ms |
2764 KB |
Output is correct |
33 |
Correct |
15 ms |
3448 KB |
Output is correct |
34 |
Correct |
15 ms |
3464 KB |
Output is correct |
35 |
Correct |
35 ms |
3532 KB |
Output is correct |
36 |
Correct |
16 ms |
3668 KB |
Output is correct |
37 |
Correct |
15 ms |
3664 KB |
Output is correct |
38 |
Correct |
50 ms |
3660 KB |
Output is correct |
39 |
Correct |
22 ms |
3928 KB |
Output is correct |
40 |
Correct |
22 ms |
3948 KB |
Output is correct |
41 |
Correct |
74 ms |
3932 KB |
Output is correct |
42 |
Correct |
22 ms |
4172 KB |
Output is correct |
43 |
Correct |
26 ms |
4036 KB |
Output is correct |
44 |
Correct |
86 ms |
4172 KB |
Output is correct |
45 |
Correct |
26 ms |
4300 KB |
Output is correct |
46 |
Correct |
33 ms |
4380 KB |
Output is correct |
47 |
Correct |
95 ms |
4300 KB |
Output is correct |
48 |
Correct |
33 ms |
4596 KB |
Output is correct |
49 |
Correct |
32 ms |
4648 KB |
Output is correct |
50 |
Correct |
106 ms |
4732 KB |
Output is correct |
51 |
Correct |
36 ms |
4932 KB |
Output is correct |
52 |
Correct |
37 ms |
4860 KB |
Output is correct |
53 |
Correct |
163 ms |
4856 KB |
Output is correct |
54 |
Correct |
50 ms |
5168 KB |
Output is correct |
55 |
Correct |
50 ms |
5136 KB |
Output is correct |
56 |
Correct |
151 ms |
5260 KB |
Output is correct |
57 |
Correct |
47 ms |
5480 KB |
Output is correct |
58 |
Correct |
49 ms |
5540 KB |
Output is correct |
59 |
Correct |
177 ms |
5584 KB |
Output is correct |
60 |
Correct |
54 ms |
5848 KB |
Output is correct |
61 |
Incorrect |
53 ms |
5820 KB |
Output isn't correct |
62 |
Correct |
213 ms |
5872 KB |
Output is correct |
63 |
Correct |
185 ms |
5852 KB |
Output is correct |
64 |
Incorrect |
113 ms |
5820 KB |
Output isn't correct |
65 |
Incorrect |
116 ms |
5916 KB |
Output isn't correct |
66 |
Correct |
195 ms |
5956 KB |
Output is correct |
67 |
Incorrect |
133 ms |
5844 KB |
Output isn't correct |
68 |
Correct |
98 ms |
5868 KB |
Output is correct |
69 |
Correct |
91 ms |
6084 KB |
Output is correct |
70 |
Correct |
96 ms |
5916 KB |
Output is correct |
71 |
Correct |
89 ms |
5948 KB |
Output is correct |
72 |
Correct |
78 ms |
6044 KB |
Output is correct |
73 |
Correct |
84 ms |
6216 KB |
Output is correct |
74 |
Correct |
123 ms |
6228 KB |
Output is correct |
75 |
Correct |
133 ms |
6216 KB |
Output is correct |
76 |
Correct |
127 ms |
6128 KB |
Output is correct |
77 |
Correct |
124 ms |
6100 KB |
Output is correct |
78 |
Correct |
69 ms |
6184 KB |
Output is correct |
79 |
Correct |
113 ms |
6120 KB |
Output is correct |
80 |
Correct |
127 ms |
6084 KB |
Output is correct |
81 |
Correct |
141 ms |
6084 KB |
Output is correct |
82 |
Correct |
118 ms |
6084 KB |
Output is correct |
83 |
Correct |
155 ms |
6264 KB |
Output is correct |
84 |
Correct |
138 ms |
6040 KB |
Output is correct |
85 |
Correct |
129 ms |
6044 KB |
Output is correct |
86 |
Correct |
136 ms |
6140 KB |
Output is correct |
87 |
Correct |
164 ms |
6212 KB |
Output is correct |
88 |
Correct |
151 ms |
6044 KB |
Output is correct |
89 |
Correct |
151 ms |
6084 KB |
Output is correct |
90 |
Correct |
156 ms |
6092 KB |
Output is correct |
91 |
Correct |
165 ms |
6012 KB |
Output is correct |
92 |
Correct |
142 ms |
6056 KB |
Output is correct |