/* 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 = 1e9;
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 (t + (mdis[u][v] + s - 1) / s > bdis[u][v]) continue;
if (a[u][v] == 'D') return true;
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 |
1 ms |
344 KB |
Output isn't correct |
2 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
3 |
Incorrect |
0 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 |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
76 ms |
6952 KB |
Output is correct |
8 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
13 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Incorrect |
1 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 |
1 ms |
348 KB |
Output is correct |
21 |
Incorrect |
0 ms |
452 KB |
Output isn't correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Incorrect |
0 ms |
484 KB |
Output isn't correct |
28 |
Correct |
1 ms |
348 KB |
Output is correct |
29 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
30 |
Correct |
0 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 |
5 ms |
1676 KB |
Output isn't correct |
34 |
Correct |
5 ms |
1784 KB |
Output is correct |
35 |
Correct |
17 ms |
1784 KB |
Output is correct |
36 |
Incorrect |
8 ms |
2076 KB |
Output isn't correct |
37 |
Correct |
7 ms |
2104 KB |
Output is correct |
38 |
Correct |
22 ms |
2080 KB |
Output is correct |
39 |
Incorrect |
10 ms |
2492 KB |
Output isn't correct |
40 |
Correct |
10 ms |
2552 KB |
Output is correct |
41 |
Correct |
29 ms |
2544 KB |
Output is correct |
42 |
Incorrect |
12 ms |
2968 KB |
Output isn't correct |
43 |
Correct |
11 ms |
3076 KB |
Output is correct |
44 |
Correct |
38 ms |
2976 KB |
Output is correct |
45 |
Incorrect |
14 ms |
3564 KB |
Output isn't correct |
46 |
Correct |
13 ms |
3560 KB |
Output is correct |
47 |
Correct |
60 ms |
3548 KB |
Output is correct |
48 |
Incorrect |
17 ms |
4128 KB |
Output isn't correct |
49 |
Correct |
19 ms |
4172 KB |
Output is correct |
50 |
Correct |
57 ms |
4156 KB |
Output is correct |
51 |
Incorrect |
19 ms |
4696 KB |
Output isn't correct |
52 |
Correct |
20 ms |
4692 KB |
Output is correct |
53 |
Correct |
69 ms |
4780 KB |
Output is correct |
54 |
Incorrect |
22 ms |
5416 KB |
Output isn't correct |
55 |
Correct |
21 ms |
5416 KB |
Output is correct |
56 |
Correct |
83 ms |
5452 KB |
Output is correct |
57 |
Incorrect |
26 ms |
6116 KB |
Output isn't correct |
58 |
Correct |
24 ms |
6160 KB |
Output is correct |
59 |
Correct |
87 ms |
6156 KB |
Output is correct |
60 |
Incorrect |
30 ms |
7032 KB |
Output isn't correct |
61 |
Correct |
28 ms |
6956 KB |
Output is correct |
62 |
Correct |
99 ms |
6924 KB |
Output is correct |
63 |
Correct |
94 ms |
6920 KB |
Output is correct |
64 |
Correct |
160 ms |
6892 KB |
Output is correct |
65 |
Correct |
163 ms |
7048 KB |
Output is correct |
66 |
Incorrect |
117 ms |
6868 KB |
Output isn't correct |
67 |
Correct |
104 ms |
6872 KB |
Output is correct |
68 |
Correct |
51 ms |
6916 KB |
Output is correct |
69 |
Correct |
55 ms |
7044 KB |
Output is correct |
70 |
Correct |
52 ms |
6932 KB |
Output is correct |
71 |
Correct |
41 ms |
6956 KB |
Output is correct |
72 |
Incorrect |
38 ms |
7052 KB |
Output isn't correct |
73 |
Incorrect |
44 ms |
6944 KB |
Output isn't correct |
74 |
Correct |
56 ms |
6960 KB |
Output is correct |
75 |
Correct |
65 ms |
6984 KB |
Output is correct |
76 |
Correct |
55 ms |
6920 KB |
Output is correct |
77 |
Correct |
58 ms |
6904 KB |
Output is correct |
78 |
Correct |
69 ms |
6884 KB |
Output is correct |
79 |
Correct |
53 ms |
7016 KB |
Output is correct |
80 |
Correct |
63 ms |
7148 KB |
Output is correct |
81 |
Correct |
63 ms |
7052 KB |
Output is correct |
82 |
Correct |
54 ms |
6932 KB |
Output is correct |
83 |
Correct |
66 ms |
6912 KB |
Output is correct |
84 |
Correct |
67 ms |
6908 KB |
Output is correct |
85 |
Correct |
72 ms |
7072 KB |
Output is correct |
86 |
Correct |
72 ms |
6936 KB |
Output is correct |
87 |
Correct |
67 ms |
6952 KB |
Output is correct |
88 |
Correct |
66 ms |
6936 KB |
Output is correct |
89 |
Correct |
73 ms |
7044 KB |
Output is correct |
90 |
Correct |
80 ms |
7076 KB |
Output is correct |
91 |
Correct |
72 ms |
6912 KB |
Output is correct |
92 |
Correct |
79 ms |
6916 KB |
Output is correct |