#include <bits/stdc++.h>
using namespace std;
#define int long long
const int N = 8e2 + 5;
int n, s;
char grid[N][N];
pair<int, int> init, home;
vector<pair<int, int>> hive;
vector<int> dx = {-1, 0, 1, 0}, dy = {0, 1, 0, -1};
queue<pair<int, int>> qmecho, qbee;
bool vmecho[N][N], vbee[N][N];
int dmecho[N][N], dbee[N][N];
bool bee_inbound(int x, int y) {
return x >= 1 && x <= n && y >= 1 && y <= n && !vbee[x][y] && (grid[x][y] == 'G' || grid[x][y] == 'M');
}
bool mecho_inbound(int x, int y) {
return x >= 1 && x <= n && y >= 1 && y <= n && !vmecho[x][y] && (grid[x][y] == 'G' || grid[x][y] == 'D');
}
bool valid(int m) {
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
vmecho[i][j] = vbee[i][j] = false;
dmecho[i][j] = dbee[i][j] = 1e18;
}
}
for (auto&b : hive) {
int x = b.first, y = b.second;
dbee[x][y] = 0, vbee[x][y] = true, qbee.push(b);
}
while (qbee.size()) {
int x = qbee.front().first, y = qbee.front().second; qbee.pop();
for (int k = 0; k < 4; k++) {
int tox = x + dx[k], toy = y + dy[k];
if (!bee_inbound(tox, toy)) continue;
dbee[tox][toy] = dbee[x][y] + 1, vbee[tox][toy] = true, qbee.push({tox, toy});
}
}
int x = init.first, y = init.second;
dmecho[x][y] = 0, vmecho[x][y] = true, qmecho.push({x, y});
while (qmecho.size()) {
int x = qmecho.front().first, y = qmecho.front().second; qmecho.pop();
if (dmecho[x][y]/s >= dbee[x][y] - m) continue;
for (int k = 0; k < 4; k++) {
int tox = x + dx[k], toy = y + dy[k];
if (!mecho_inbound(tox, toy)) continue;
dmecho[tox][toy] = dmecho[x][y] + 1, vmecho[tox][toy] = true, qmecho.push({tox, toy});
}
}
x = home.first, y = home.second;
return vmecho[x][y] && dmecho[x][y]/s < dbee[x][y] - m;
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n >> s;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
cin >> grid[i][j];
if (grid[i][j] == 'M') init = {i, j};
else if(grid[i][j] == 'D') home = {i, j};
else if(grid[i][j] == 'H') hive.push_back({i, j});
}
}
int l = 0, r = n*n, ans = -1;
while (l <= r) {
int m = (l + r)/2;
if (valid(m)) ans = m, l = m + 1;
else r = m - 1;
}
cout << ans << '\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 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 |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
539 ms |
12560 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
0 ms |
468 KB |
Output is correct |
10 |
Correct |
0 ms |
468 KB |
Output is correct |
11 |
Correct |
0 ms |
468 KB |
Output is correct |
12 |
Correct |
1 ms |
980 KB |
Output is correct |
13 |
Correct |
2 ms |
852 KB |
Output is correct |
14 |
Correct |
2 ms |
980 KB |
Output is correct |
15 |
Correct |
0 ms |
468 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
1 ms |
596 KB |
Output is correct |
18 |
Correct |
1 ms |
596 KB |
Output is correct |
19 |
Correct |
1 ms |
596 KB |
Output is correct |
20 |
Correct |
1 ms |
596 KB |
Output is correct |
21 |
Correct |
1 ms |
788 KB |
Output is correct |
22 |
Correct |
1 ms |
724 KB |
Output is correct |
23 |
Correct |
1 ms |
852 KB |
Output is correct |
24 |
Correct |
1 ms |
852 KB |
Output is correct |
25 |
Correct |
1 ms |
980 KB |
Output is correct |
26 |
Correct |
1 ms |
980 KB |
Output is correct |
27 |
Correct |
2 ms |
980 KB |
Output is correct |
28 |
Correct |
2 ms |
980 KB |
Output is correct |
29 |
Correct |
2 ms |
1108 KB |
Output is correct |
30 |
Correct |
2 ms |
1108 KB |
Output is correct |
31 |
Correct |
2 ms |
1108 KB |
Output is correct |
32 |
Correct |
2 ms |
1108 KB |
Output is correct |
33 |
Correct |
33 ms |
5568 KB |
Output is correct |
34 |
Correct |
38 ms |
5584 KB |
Output is correct |
35 |
Correct |
76 ms |
5588 KB |
Output is correct |
36 |
Correct |
56 ms |
6228 KB |
Output is correct |
37 |
Correct |
62 ms |
6320 KB |
Output is correct |
38 |
Correct |
89 ms |
6328 KB |
Output is correct |
39 |
Correct |
54 ms |
7068 KB |
Output is correct |
40 |
Correct |
66 ms |
6996 KB |
Output is correct |
41 |
Correct |
125 ms |
7080 KB |
Output is correct |
42 |
Correct |
75 ms |
7764 KB |
Output is correct |
43 |
Correct |
76 ms |
7816 KB |
Output is correct |
44 |
Correct |
183 ms |
7888 KB |
Output is correct |
45 |
Correct |
106 ms |
8564 KB |
Output is correct |
46 |
Correct |
83 ms |
8564 KB |
Output is correct |
47 |
Correct |
181 ms |
8580 KB |
Output is correct |
48 |
Correct |
100 ms |
9312 KB |
Output is correct |
49 |
Correct |
131 ms |
9308 KB |
Output is correct |
50 |
Correct |
229 ms |
9340 KB |
Output is correct |
51 |
Correct |
151 ms |
10068 KB |
Output is correct |
52 |
Correct |
140 ms |
10068 KB |
Output is correct |
53 |
Correct |
276 ms |
10096 KB |
Output is correct |
54 |
Correct |
162 ms |
10804 KB |
Output is correct |
55 |
Correct |
159 ms |
10808 KB |
Output is correct |
56 |
Correct |
358 ms |
10816 KB |
Output is correct |
57 |
Correct |
179 ms |
11552 KB |
Output is correct |
58 |
Correct |
178 ms |
11476 KB |
Output is correct |
59 |
Correct |
350 ms |
11592 KB |
Output is correct |
60 |
Correct |
215 ms |
12304 KB |
Output is correct |
61 |
Correct |
176 ms |
12244 KB |
Output is correct |
62 |
Correct |
525 ms |
12324 KB |
Output is correct |
63 |
Correct |
509 ms |
12320 KB |
Output is correct |
64 |
Correct |
662 ms |
12344 KB |
Output is correct |
65 |
Correct |
614 ms |
12320 KB |
Output is correct |
66 |
Correct |
559 ms |
12324 KB |
Output is correct |
67 |
Correct |
500 ms |
12336 KB |
Output is correct |
68 |
Correct |
460 ms |
12372 KB |
Output is correct |
69 |
Correct |
535 ms |
12376 KB |
Output is correct |
70 |
Correct |
497 ms |
12384 KB |
Output is correct |
71 |
Correct |
478 ms |
12492 KB |
Output is correct |
72 |
Correct |
456 ms |
12380 KB |
Output is correct |
73 |
Correct |
453 ms |
12884 KB |
Output is correct |
74 |
Correct |
431 ms |
12960 KB |
Output is correct |
75 |
Correct |
481 ms |
13020 KB |
Output is correct |
76 |
Correct |
406 ms |
12992 KB |
Output is correct |
77 |
Correct |
417 ms |
13036 KB |
Output is correct |
78 |
Correct |
457 ms |
12840 KB |
Output is correct |
79 |
Correct |
395 ms |
12852 KB |
Output is correct |
80 |
Correct |
449 ms |
12836 KB |
Output is correct |
81 |
Correct |
442 ms |
12968 KB |
Output is correct |
82 |
Correct |
460 ms |
12840 KB |
Output is correct |
83 |
Correct |
511 ms |
12764 KB |
Output is correct |
84 |
Correct |
508 ms |
12768 KB |
Output is correct |
85 |
Correct |
472 ms |
12772 KB |
Output is correct |
86 |
Correct |
441 ms |
12780 KB |
Output is correct |
87 |
Correct |
509 ms |
12764 KB |
Output is correct |
88 |
Correct |
449 ms |
12652 KB |
Output is correct |
89 |
Correct |
477 ms |
12752 KB |
Output is correct |
90 |
Correct |
459 ms |
12652 KB |
Output is correct |
91 |
Correct |
467 ms |
12696 KB |
Output is correct |
92 |
Correct |
571 ms |
12656 KB |
Output is correct |