/* Author: goats_9 */
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
constexpr int INF = 1e9;
int main() {
vector<pair<int, int>> moves = {
{-1, 0},
{1, 0},
{0, 1},
{0, -1},
};
cin.tie(0)->sync_with_stdio(0);
int n, s;
cin >> n >> s;
vector<string> a(n);
for (auto &u : a) cin >> u;
vector<vector<int>> bdis(n, vector<int>(n, INF));
queue<pair<int, int>> q;
pair<int, int> mecho = {-1, -1}, home = {-1, -1};
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (a[i][j] == 'H') {
bdis[i][j] = 0;
q.push({i, j});
} else if (a[i][j] == 'M') {
mecho = {i, j};
} else if (a[i][j] == 'D') {
home = {i, j};
}
}
}
while (!q.empty()) {
auto &[u, v] = q.front();
q.pop();
for (auto &[dx, dy] : moves) {
int uu = u + dx, vv = v + dy;
if (uu < 0 || uu >= n || vv < 0 || vv >= n || bdis[uu][vv] != INF || (a[uu][vv] != 'G' && a[uu][vv] != 'M')) continue;
bdis[uu][vv] = bdis[u][v] + 1;
q.push({uu, vv});
}
}
int l = 0, r = 2000;
auto check = [&] (int t) -> bool {
queue<pair<int, int>> q;
vector<vector<int>> mdis(n, vector<int> (n, INF));
mdis[mecho.first][mecho.second] = 0;
q.push(mecho);
while (!q.empty()) {
auto &[u, v] = q.front();
q.pop();
if (a[u][v] == 'D') return true;
if (t + (mdis[u][v] + s - 1) / s > bdis[u][v]) continue;
for (auto &[dx, dy] : moves) {
int uu = u + dx, vv = v + dy;
if (uu < 0 || uu >= n || vv < 0 || vv >= n || mdis[uu][vv] != INF || (a[uu][vv] != 'G' && a[uu][vv] != 'D')) continue;
mdis[uu][vv] = mdis[u][v] + 1;
q.push({uu, vv});
}
}
return false;
};
while (r - l > 1) {
int g = (l + r) / 2;
if (check(g)) l = g;
else r = g;
}
if (check(l)) cout << l;
else cout << -1;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
344 KB |
Output isn't correct |
2 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
3 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
4 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
63 ms |
6872 KB |
Output is correct |
8 |
Incorrect |
0 ms |
344 KB |
Output isn't correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
13 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
26 |
Correct |
0 ms |
452 KB |
Output is correct |
27 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
28 |
Correct |
0 ms |
348 KB |
Output is correct |
29 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
30 |
Correct |
1 ms |
348 KB |
Output is correct |
31 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
32 |
Correct |
1 ms |
348 KB |
Output is correct |
33 |
Incorrect |
3 ms |
1680 KB |
Output isn't correct |
34 |
Incorrect |
5 ms |
1928 KB |
Output isn't correct |
35 |
Correct |
16 ms |
1680 KB |
Output is correct |
36 |
Incorrect |
5 ms |
1884 KB |
Output isn't correct |
37 |
Incorrect |
4 ms |
1952 KB |
Output isn't correct |
38 |
Correct |
19 ms |
2012 KB |
Output is correct |
39 |
Incorrect |
5 ms |
2492 KB |
Output isn't correct |
40 |
Incorrect |
5 ms |
2492 KB |
Output isn't correct |
41 |
Correct |
25 ms |
2500 KB |
Output is correct |
42 |
Incorrect |
8 ms |
2992 KB |
Output isn't correct |
43 |
Incorrect |
7 ms |
2996 KB |
Output isn't correct |
44 |
Correct |
31 ms |
3028 KB |
Output is correct |
45 |
Incorrect |
8 ms |
3500 KB |
Output isn't correct |
46 |
Incorrect |
8 ms |
3484 KB |
Output isn't correct |
47 |
Correct |
40 ms |
3468 KB |
Output is correct |
48 |
Incorrect |
12 ms |
4064 KB |
Output isn't correct |
49 |
Incorrect |
9 ms |
4076 KB |
Output isn't correct |
50 |
Correct |
47 ms |
4092 KB |
Output is correct |
51 |
Incorrect |
11 ms |
4692 KB |
Output isn't correct |
52 |
Incorrect |
11 ms |
4556 KB |
Output isn't correct |
53 |
Correct |
56 ms |
4684 KB |
Output is correct |
54 |
Incorrect |
14 ms |
5372 KB |
Output isn't correct |
55 |
Incorrect |
12 ms |
5372 KB |
Output isn't correct |
56 |
Correct |
66 ms |
5388 KB |
Output is correct |
57 |
Incorrect |
15 ms |
6364 KB |
Output isn't correct |
58 |
Incorrect |
13 ms |
6060 KB |
Output isn't correct |
59 |
Correct |
76 ms |
6092 KB |
Output is correct |
60 |
Incorrect |
24 ms |
6876 KB |
Output isn't correct |
61 |
Incorrect |
16 ms |
6880 KB |
Output isn't correct |
62 |
Correct |
87 ms |
6852 KB |
Output is correct |
63 |
Correct |
86 ms |
6836 KB |
Output is correct |
64 |
Correct |
156 ms |
6828 KB |
Output is correct |
65 |
Correct |
143 ms |
6840 KB |
Output is correct |
66 |
Incorrect |
120 ms |
6844 KB |
Output isn't correct |
67 |
Correct |
99 ms |
6856 KB |
Output is correct |
68 |
Correct |
41 ms |
6888 KB |
Output is correct |
69 |
Correct |
44 ms |
6888 KB |
Output is correct |
70 |
Correct |
37 ms |
6872 KB |
Output is correct |
71 |
Correct |
30 ms |
6860 KB |
Output is correct |
72 |
Incorrect |
27 ms |
6864 KB |
Output isn't correct |
73 |
Incorrect |
32 ms |
6864 KB |
Output isn't correct |
74 |
Correct |
45 ms |
6888 KB |
Output is correct |
75 |
Correct |
51 ms |
6840 KB |
Output is correct |
76 |
Correct |
43 ms |
7028 KB |
Output is correct |
77 |
Correct |
52 ms |
6856 KB |
Output is correct |
78 |
Correct |
52 ms |
6840 KB |
Output is correct |
79 |
Correct |
41 ms |
6864 KB |
Output is correct |
80 |
Correct |
45 ms |
6864 KB |
Output is correct |
81 |
Correct |
51 ms |
6896 KB |
Output is correct |
82 |
Correct |
46 ms |
6872 KB |
Output is correct |
83 |
Correct |
53 ms |
6852 KB |
Output is correct |
84 |
Correct |
57 ms |
6852 KB |
Output is correct |
85 |
Correct |
52 ms |
6880 KB |
Output is correct |
86 |
Correct |
57 ms |
6880 KB |
Output is correct |
87 |
Correct |
56 ms |
6872 KB |
Output is correct |
88 |
Correct |
54 ms |
6888 KB |
Output is correct |
89 |
Correct |
60 ms |
6868 KB |
Output is correct |
90 |
Correct |
65 ms |
6872 KB |
Output is correct |
91 |
Correct |
61 ms |
6860 KB |
Output is correct |
92 |
Correct |
61 ms |
6888 KB |
Output is correct |