#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 800;
vector<string> field(MAX_N);
int n, s;
bool valid_sq(int x, int y) {
return x >= 0 && x < n && y >= 0 && y < n &&
(field[x][y] == 'G' || field[x][y] == 'M');
}
bool mecho_reached(int mecho_dis, int bees_dis) { return mecho_dis / s < bees_dis; }
int main() {
cin >> n >> s;
for (int i = 0; i < n; i++) { cin >> field[i]; }
vector<pair<int, int>> hives;
int mechox, mechoy, home_x, home_y;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (field[i][j] == 'M') {
mechox = i;
mechoy = j;
} else if (field[i][j] == 'H') {
hives.push_back({i, j});
} else if (field[i][j] == 'D') {
home_x = i;
home_y = j;
}
}
}
int dx[] = {-1, 0, 1, 0};
int dy[] = {0, -1, 0, 1};
int l = 0;
int r = n * n;
while (l <= r) {
vector<vector<bool>> bees_visited(n, vector<bool>(n));
vector<vector<bool>> mecho_visited(n, vector<bool>(n));
vector<vector<int>> bees_time(n, vector<int>(n));
vector<vector<int>> mecho_time(n, vector<int>(n));
queue<pair<int, int>> q;
int eating_time = (l + r) / 2;
for (auto i : hives) {
q.push({i.first, i.second});
bees_visited[i.first][i.second] = true;
}
while (!q.empty()) {
int x = q.front().first, y = q.front().second;
q.pop();
for (int i = 0; i < 4; i++) {
int nx = x + dx[i], ny = y + dy[i];
if (valid_sq(nx, ny) && !bees_visited[nx][ny]) {
bees_time[nx][ny] = bees_time[x][y] + 1;
q.push({nx, ny});
bees_visited[nx][ny] = true;
}
}
}
q.push({mechox, mechoy});
mecho_visited[mechox][mechoy] = true;
if (bees_time[mechox][mechoy] <= eating_time) { q.pop(); }
while (!q.empty()) {
int x = q.front().first, y = q.front().second;
q.pop();
for (int i = 0; i < 4; i++) {
int nx = x + dx[i], ny = y + dy[i];
if (valid_sq(nx, ny) && !mecho_visited[nx][ny] &&
mecho_reached(mecho_time[x][y] + 1,
bees_time[nx][ny] - eating_time)) {
mecho_visited[nx][ny] = true;
q.push({nx, ny});
mecho_time[nx][ny] = mecho_time[x][y] + 1;
}
}
}
bool reached = false;
for (int i = 0; i < 4; i++) {
int nx = home_x + dx[i], ny = home_y + dy[i];
if (valid_sq(nx, ny) &&
mecho_reached(mecho_time[nx][ny], bees_time[nx][ny] - eating_time) &&
mecho_visited[nx][ny]) {
reached = true;
}
}
if (reached) {
l = eating_time + 1;
} else {
r = eating_time - 1;
}
}
cout << l - 1 << '\n';
}
Compilation message
mecho.cpp: In function 'int main()':
mecho.cpp:65:23: warning: 'mechox' may be used uninitialized in this function [-Wmaybe-uninitialized]
65 | mecho_visited[mechox][mechoy] = true;
| ^
mecho.cpp:84:29: warning: 'home_y' may be used uninitialized in this function [-Wmaybe-uninitialized]
84 | int nx = home_x + dx[i], ny = home_y + dy[i];
| ^~
mecho.cpp:84:8: warning: 'home_x' may be used uninitialized in this function [-Wmaybe-uninitialized]
84 | int nx = home_x + dx[i], ny = home_y + dy[i];
| ^~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
388 KB |
Output is correct |
7 |
Correct |
334 ms |
7260 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
344 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
2 ms |
604 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
344 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
348 KB |
Output is correct |
29 |
Correct |
1 ms |
348 KB |
Output is correct |
30 |
Correct |
1 ms |
348 KB |
Output is correct |
31 |
Correct |
1 ms |
348 KB |
Output is correct |
32 |
Correct |
1 ms |
348 KB |
Output is correct |
33 |
Correct |
30 ms |
1828 KB |
Output is correct |
34 |
Correct |
28 ms |
1800 KB |
Output is correct |
35 |
Correct |
48 ms |
1784 KB |
Output is correct |
36 |
Correct |
39 ms |
2140 KB |
Output is correct |
37 |
Correct |
38 ms |
2136 KB |
Output is correct |
38 |
Correct |
64 ms |
2140 KB |
Output is correct |
39 |
Correct |
46 ms |
2608 KB |
Output is correct |
40 |
Correct |
49 ms |
2708 KB |
Output is correct |
41 |
Correct |
83 ms |
2396 KB |
Output is correct |
42 |
Correct |
62 ms |
3328 KB |
Output is correct |
43 |
Correct |
60 ms |
3432 KB |
Output is correct |
44 |
Correct |
106 ms |
3276 KB |
Output is correct |
45 |
Correct |
79 ms |
3832 KB |
Output is correct |
46 |
Correct |
69 ms |
3824 KB |
Output is correct |
47 |
Correct |
123 ms |
3672 KB |
Output is correct |
48 |
Correct |
86 ms |
4400 KB |
Output is correct |
49 |
Correct |
87 ms |
4412 KB |
Output is correct |
50 |
Correct |
160 ms |
4404 KB |
Output is correct |
51 |
Correct |
104 ms |
5016 KB |
Output is correct |
52 |
Correct |
99 ms |
5036 KB |
Output is correct |
53 |
Correct |
194 ms |
4952 KB |
Output is correct |
54 |
Correct |
133 ms |
5660 KB |
Output is correct |
55 |
Correct |
116 ms |
5704 KB |
Output is correct |
56 |
Correct |
221 ms |
5468 KB |
Output is correct |
57 |
Correct |
143 ms |
6400 KB |
Output is correct |
58 |
Correct |
131 ms |
6444 KB |
Output is correct |
59 |
Correct |
247 ms |
6380 KB |
Output is correct |
60 |
Correct |
171 ms |
7152 KB |
Output is correct |
61 |
Correct |
150 ms |
7172 KB |
Output is correct |
62 |
Correct |
285 ms |
7000 KB |
Output is correct |
63 |
Correct |
379 ms |
7148 KB |
Output is correct |
64 |
Correct |
464 ms |
7000 KB |
Output is correct |
65 |
Correct |
426 ms |
7108 KB |
Output is correct |
66 |
Correct |
406 ms |
7112 KB |
Output is correct |
67 |
Correct |
380 ms |
7000 KB |
Output is correct |
68 |
Correct |
376 ms |
7004 KB |
Output is correct |
69 |
Correct |
369 ms |
7004 KB |
Output is correct |
70 |
Correct |
346 ms |
7004 KB |
Output is correct |
71 |
Correct |
361 ms |
7000 KB |
Output is correct |
72 |
Correct |
348 ms |
7004 KB |
Output is correct |
73 |
Correct |
306 ms |
7260 KB |
Output is correct |
74 |
Correct |
335 ms |
7328 KB |
Output is correct |
75 |
Correct |
353 ms |
7436 KB |
Output is correct |
76 |
Correct |
330 ms |
7260 KB |
Output is correct |
77 |
Correct |
338 ms |
7436 KB |
Output is correct |
78 |
Correct |
326 ms |
7384 KB |
Output is correct |
79 |
Correct |
306 ms |
7260 KB |
Output is correct |
80 |
Correct |
308 ms |
7256 KB |
Output is correct |
81 |
Correct |
335 ms |
7260 KB |
Output is correct |
82 |
Correct |
312 ms |
7408 KB |
Output is correct |
83 |
Correct |
345 ms |
7260 KB |
Output is correct |
84 |
Correct |
331 ms |
7308 KB |
Output is correct |
85 |
Correct |
347 ms |
7512 KB |
Output is correct |
86 |
Correct |
331 ms |
7256 KB |
Output is correct |
87 |
Correct |
330 ms |
7260 KB |
Output is correct |
88 |
Correct |
334 ms |
7260 KB |
Output is correct |
89 |
Correct |
345 ms |
7260 KB |
Output is correct |
90 |
Correct |
336 ms |
7260 KB |
Output is correct |
91 |
Correct |
338 ms |
7316 KB |
Output is correct |
92 |
Correct |
359 ms |
7308 KB |
Output is correct |