/* 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};
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};
}
}
}
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] == 'T' || a[uu][vv] == 'D') continue;
bdis[uu][vv] = bdis[u][v] + 1;
q.push({uu, vv});
}
}
auto check = [&] (int t) -> bool {
queue<pair<int, int>> bfs;
vector<vector<int>> mdis(n, vector<int> (n, INF));
mdis[mecho.first][mecho.second] = 0;
bfs.push(mecho);
while (!bfs.empty()) {
auto &[u, v] = bfs.front();
bfs.pop();
if (a[u][v] == 'D') return true;
if ((mdis[u][v] % s) && (t + (mdis[u][v] + s - 1) / s > bdis[u][v])) continue;
if ((mdis[u][v] % s == 0) && (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] == 'T') continue;
mdis[uu][vv] = mdis[u][v] + 1;
bfs.push({uu, vv});
}
}
return false;
};
int l = 0, r = bdis[mecho.first][mecho.second];
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 |
Correct |
1 ms |
344 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 |
604 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
73 ms |
6888 KB |
Output is correct |
8 |
Correct |
1 ms |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 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 |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
344 KB |
Output is correct |
23 |
Correct |
0 ms |
344 KB |
Output is correct |
24 |
Correct |
1 ms |
344 KB |
Output is correct |
25 |
Correct |
1 ms |
460 KB |
Output is correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
0 ms |
464 KB |
Output is correct |
29 |
Correct |
1 ms |
348 KB |
Output is correct |
30 |
Correct |
0 ms |
348 KB |
Output is correct |
31 |
Correct |
0 ms |
348 KB |
Output is correct |
32 |
Correct |
1 ms |
348 KB |
Output is correct |
33 |
Correct |
4 ms |
1676 KB |
Output is correct |
34 |
Correct |
4 ms |
1668 KB |
Output is correct |
35 |
Correct |
14 ms |
1736 KB |
Output is correct |
36 |
Correct |
5 ms |
2032 KB |
Output is correct |
37 |
Correct |
5 ms |
1880 KB |
Output is correct |
38 |
Correct |
20 ms |
1884 KB |
Output is correct |
39 |
Correct |
7 ms |
2496 KB |
Output is correct |
40 |
Correct |
6 ms |
2492 KB |
Output is correct |
41 |
Correct |
33 ms |
2520 KB |
Output is correct |
42 |
Correct |
8 ms |
2956 KB |
Output is correct |
43 |
Correct |
8 ms |
2960 KB |
Output is correct |
44 |
Correct |
29 ms |
2964 KB |
Output is correct |
45 |
Correct |
10 ms |
3484 KB |
Output is correct |
46 |
Correct |
10 ms |
3476 KB |
Output is correct |
47 |
Correct |
44 ms |
3488 KB |
Output is correct |
48 |
Correct |
13 ms |
4064 KB |
Output is correct |
49 |
Correct |
12 ms |
4068 KB |
Output is correct |
50 |
Correct |
46 ms |
4084 KB |
Output is correct |
51 |
Correct |
15 ms |
4728 KB |
Output is correct |
52 |
Correct |
12 ms |
4692 KB |
Output is correct |
53 |
Correct |
53 ms |
4696 KB |
Output is correct |
54 |
Correct |
17 ms |
5392 KB |
Output is correct |
55 |
Correct |
16 ms |
5412 KB |
Output is correct |
56 |
Correct |
69 ms |
5424 KB |
Output is correct |
57 |
Correct |
20 ms |
6088 KB |
Output is correct |
58 |
Correct |
18 ms |
6120 KB |
Output is correct |
59 |
Correct |
71 ms |
6036 KB |
Output is correct |
60 |
Correct |
22 ms |
6892 KB |
Output is correct |
61 |
Correct |
20 ms |
6980 KB |
Output is correct |
62 |
Correct |
85 ms |
6844 KB |
Output is correct |
63 |
Correct |
88 ms |
6860 KB |
Output is correct |
64 |
Correct |
137 ms |
6820 KB |
Output is correct |
65 |
Correct |
138 ms |
6840 KB |
Output is correct |
66 |
Correct |
123 ms |
6840 KB |
Output is correct |
67 |
Correct |
106 ms |
6840 KB |
Output is correct |
68 |
Correct |
45 ms |
6836 KB |
Output is correct |
69 |
Correct |
37 ms |
6860 KB |
Output is correct |
70 |
Correct |
39 ms |
6816 KB |
Output is correct |
71 |
Correct |
34 ms |
6860 KB |
Output is correct |
72 |
Correct |
27 ms |
6840 KB |
Output is correct |
73 |
Correct |
29 ms |
6780 KB |
Output is correct |
74 |
Correct |
44 ms |
6888 KB |
Output is correct |
75 |
Correct |
47 ms |
6840 KB |
Output is correct |
76 |
Correct |
46 ms |
6868 KB |
Output is correct |
77 |
Correct |
48 ms |
6880 KB |
Output is correct |
78 |
Correct |
52 ms |
6876 KB |
Output is correct |
79 |
Correct |
49 ms |
6872 KB |
Output is correct |
80 |
Correct |
46 ms |
6864 KB |
Output is correct |
81 |
Correct |
51 ms |
6876 KB |
Output is correct |
82 |
Correct |
44 ms |
6832 KB |
Output is correct |
83 |
Correct |
54 ms |
6848 KB |
Output is correct |
84 |
Correct |
49 ms |
6864 KB |
Output is correct |
85 |
Correct |
51 ms |
6880 KB |
Output is correct |
86 |
Correct |
57 ms |
6864 KB |
Output is correct |
87 |
Correct |
50 ms |
6856 KB |
Output is correct |
88 |
Correct |
54 ms |
6880 KB |
Output is correct |
89 |
Correct |
57 ms |
6796 KB |
Output is correct |
90 |
Correct |
68 ms |
7152 KB |
Output is correct |
91 |
Correct |
62 ms |
6892 KB |
Output is correct |
92 |
Correct |
61 ms |
6860 KB |
Output is correct |