#include <bits/stdc++.h>
using namespace std;
template<typename T> void dout(string name, T arg) {
cerr << name << " = " << arg << "\e[39m" << endl;
}
template<typename T1, typename... T2> void dout(string names, T1 arg, T2... args) {
cerr << names.substr(0, names.find(',')) << " = " << arg << " | ";
dout(names.substr(names.find(',') + 2), args...);
}
#define debug(...) cerr << "\e[91m" << "[" << __LINE__ << ":" << __func__ << "]=> ", dout(#__VA_ARGS__, __VA_ARGS__);
const int N = 800;
vector<string> field(N);
int n, s;
bool valid(int x, int y) {
return x >= 0 && x < n && y >= 0 && y < n && (field[x][y] == 'G' || field[x][y] == 'M');
}
bool reached(int m, int b) {
return m / s < b;
}
int main() {
ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
cin >> n >> s;
for (int i = 0; i < n; i++) {
cin >> field[i];
}
vector<pair<int, int>> hives;
pair<int, int> m, d;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (field[i][j] == 'M') {
m = {i, j};
}
if (field[i][j] == 'H') {
hives.push_back({i, j});
}
if (field[i][j] == 'D') {
d = {i, j};
}
}
}
int dx[4] = {-1, 0, 1, 0};
int dy[4] = {0, -1, 0, 1};
int l = 0, 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 mid = (l + r) / 2;
for (auto [x, y] : hives) {
q.push({x, y});
bees_visited[x][y] = true;
}
while (!q.empty()) {
auto [x, y] = q.front();
q.pop();
for (int i = 0; i < 4; i++) {
int xx = x + dx[i], yy = y + dy[i];
if (valid(xx, yy) && !bees_visited[xx][yy]) {
bees_time[xx][yy] = bees_time[x][y] + 1;
q.push({xx, yy});
bees_visited[xx][yy] = true;
}
}
}
q.push(m);
mecho_visited[m.first][m.second] = true;
if (bees_time[m.first][m.second] <= mid) {
q.pop();
}
while (!q.empty()) {
auto [x, y] = q.front();
q.pop();
for (int i = 0; i < 4; i++) {
int xx = x + dx[i], yy = y + dy[i];
if (valid(xx, yy) && !mecho_visited[xx][yy] && reached(mecho_time[xx][yy] + 1, bees_time[xx][yy] - mid)) {
mecho_time[xx][yy] = mecho_time[x][y] + 1;
q.push({xx, yy});
mecho_visited[xx][yy] = true;
}
}
}
bool reach = false;
for (int i = 0; i < 4; i++) {
int xx = d.first + dx[i], yy = d.second + dy[i];
if (valid(xx, yy) && reached(mecho_time[xx][yy], bees_time[xx][yy] - mid) && mecho_visited[xx][yy]) {
reach = true;
}
}
if (reach) {
l = mid + 1;
}
else {
r = mid - 1;
}
}
cout << l - 1 << '\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
600 ms |
6428 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
11 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
12 |
Correct |
2 ms |
340 KB |
Output is correct |
13 |
Correct |
2 ms |
340 KB |
Output is correct |
14 |
Correct |
3 ms |
400 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
2 ms |
372 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
2 ms |
340 KB |
Output is correct |
26 |
Correct |
2 ms |
396 KB |
Output is correct |
27 |
Correct |
0 ms |
340 KB |
Output is correct |
28 |
Correct |
2 ms |
340 KB |
Output is correct |
29 |
Correct |
3 ms |
384 KB |
Output is correct |
30 |
Correct |
3 ms |
404 KB |
Output is correct |
31 |
Correct |
3 ms |
340 KB |
Output is correct |
32 |
Correct |
2 ms |
340 KB |
Output is correct |
33 |
Correct |
42 ms |
1552 KB |
Output is correct |
34 |
Correct |
40 ms |
1548 KB |
Output is correct |
35 |
Correct |
77 ms |
1512 KB |
Output is correct |
36 |
Correct |
54 ms |
1880 KB |
Output is correct |
37 |
Correct |
58 ms |
1748 KB |
Output is correct |
38 |
Correct |
123 ms |
1924 KB |
Output is correct |
39 |
Correct |
73 ms |
2268 KB |
Output is correct |
40 |
Correct |
69 ms |
2284 KB |
Output is correct |
41 |
Correct |
151 ms |
2260 KB |
Output is correct |
42 |
Correct |
90 ms |
2732 KB |
Output is correct |
43 |
Correct |
86 ms |
2772 KB |
Output is correct |
44 |
Correct |
180 ms |
2720 KB |
Output is correct |
45 |
Correct |
113 ms |
3300 KB |
Output is correct |
46 |
Correct |
105 ms |
3200 KB |
Output is correct |
47 |
Correct |
247 ms |
3276 KB |
Output is correct |
48 |
Correct |
143 ms |
3760 KB |
Output is correct |
49 |
Correct |
126 ms |
3776 KB |
Output is correct |
50 |
Correct |
241 ms |
3756 KB |
Output is correct |
51 |
Correct |
181 ms |
4408 KB |
Output is correct |
52 |
Correct |
167 ms |
4456 KB |
Output is correct |
53 |
Correct |
310 ms |
4308 KB |
Output is correct |
54 |
Correct |
209 ms |
4928 KB |
Output is correct |
55 |
Correct |
182 ms |
5016 KB |
Output is correct |
56 |
Correct |
396 ms |
4980 KB |
Output is correct |
57 |
Correct |
231 ms |
5608 KB |
Output is correct |
58 |
Correct |
200 ms |
5672 KB |
Output is correct |
59 |
Correct |
467 ms |
5728 KB |
Output is correct |
60 |
Correct |
247 ms |
6336 KB |
Output is correct |
61 |
Correct |
233 ms |
6328 KB |
Output is correct |
62 |
Correct |
520 ms |
6228 KB |
Output is correct |
63 |
Incorrect |
633 ms |
6320 KB |
Output isn't correct |
64 |
Incorrect |
648 ms |
6356 KB |
Output isn't correct |
65 |
Incorrect |
643 ms |
6380 KB |
Output isn't correct |
66 |
Incorrect |
612 ms |
6368 KB |
Output isn't correct |
67 |
Incorrect |
757 ms |
6320 KB |
Output isn't correct |
68 |
Incorrect |
555 ms |
6400 KB |
Output isn't correct |
69 |
Correct |
535 ms |
6356 KB |
Output is correct |
70 |
Incorrect |
566 ms |
6344 KB |
Output isn't correct |
71 |
Incorrect |
549 ms |
6404 KB |
Output isn't correct |
72 |
Correct |
511 ms |
6348 KB |
Output is correct |
73 |
Correct |
525 ms |
6736 KB |
Output is correct |
74 |
Correct |
572 ms |
6736 KB |
Output is correct |
75 |
Correct |
694 ms |
6616 KB |
Output is correct |
76 |
Correct |
552 ms |
6620 KB |
Output is correct |
77 |
Correct |
615 ms |
6748 KB |
Output is correct |
78 |
Correct |
606 ms |
6588 KB |
Output is correct |
79 |
Correct |
512 ms |
6640 KB |
Output is correct |
80 |
Correct |
551 ms |
6580 KB |
Output is correct |
81 |
Correct |
589 ms |
6580 KB |
Output is correct |
82 |
Correct |
545 ms |
6588 KB |
Output is correct |
83 |
Correct |
626 ms |
6536 KB |
Output is correct |
84 |
Correct |
564 ms |
6536 KB |
Output is correct |
85 |
Correct |
675 ms |
6540 KB |
Output is correct |
86 |
Correct |
589 ms |
6540 KB |
Output is correct |
87 |
Correct |
562 ms |
6672 KB |
Output is correct |
88 |
Correct |
596 ms |
6608 KB |
Output is correct |
89 |
Correct |
564 ms |
6484 KB |
Output is correct |
90 |
Correct |
594 ms |
6488 KB |
Output is correct |
91 |
Correct |
613 ms |
6604 KB |
Output is correct |
92 |
Correct |
731 ms |
6484 KB |
Output is correct |