#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#include "debug.h"
#else
#define debug(...) 42
#endif
const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, -1, 0, 1};
int main() {
ios::sync_with_stdio(false); cin.tie(nullptr);
int n, k; cin >> n >> k;
vector a(n, vector<char>(n));
vector ma(n, vector<int>(n, -1)), d(n, vector<int>(n, -1)), c(n, vector<int>(n, -1));
queue<array<int, 2>> q;
array<int, 2> s{}, t{};
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
cin >> a[i][j];
if (a[i][j] == 'H') {
q.push({i, j});
d[i][j] = 0;
} else if (a[i][j] == 'M') {
s = {i, j};
} else if (a[i][j] == 'D') {
t = {i, j};
}
}
}
auto inside = [&](int i, int j) {
return 0 <= i && i < n && 0 <= j && j < n;
};
auto bfs = [&](vector<vector<int>> &d) {
while (q.size()) {
auto [i, j] = q.front(); q.pop();
for (int dr = 0; dr < 4; ++dr) {
int u = i + dx[dr], v = j + dy[dr];
if (inside(u, v) && a[u][v] != 'T' && a[u][v] != 'H' && d[u][v] == -1) {
d[u][v] = d[i][j] + 1;
q.push({u, v});
}
}
}
};
bfs(d);
c[s[0]][s[1]] = 0;
q.push(s);
bfs(c);
ma[s[0]][s[1]] = 1e9;
q.push(s);
d[t[0]][t[1]] = 1e9;
vector vis(n, vector<bool>(n));
vis[s[0]][s[1]] = 1;
while (q.size()) {
auto [i, j] = q.front(); q.pop();
ma[i][j] = max(-1, min(ma[i][j], d[i][j] - (c[i][j] + k - 1) / k));
for (int dr = 0; dr < 4; ++dr) {
int u = i + dx[dr], v = j + dy[dr];
if (inside(u, v) && a[u][v] != 'T' && a[u][v] != 'H' && c[u][v] == c[i][j] + 1) {
ma[u][v] = max(ma[u][v], ma[i][j]);
if (!vis[u][v]) {
q.push({u, v});
vis[u][v] = 1;
}
}
}
}
cout << ma[t[0]][t[1]];
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
2 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
3 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
4 |
Incorrect |
1 ms |
344 KB |
Output isn't correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
600 KB |
Output is correct |
7 |
Correct |
59 ms |
9456 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 |
0 ms |
348 KB |
Output is correct |
12 |
Incorrect |
1 ms |
408 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 |
1 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 |
1 ms |
344 KB |
Output isn't correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
27 |
Incorrect |
1 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 |
1 ms |
348 KB |
Output isn't correct |
32 |
Correct |
1 ms |
348 KB |
Output is correct |
33 |
Incorrect |
6 ms |
2140 KB |
Output isn't correct |
34 |
Correct |
6 ms |
2140 KB |
Output is correct |
35 |
Incorrect |
7 ms |
2180 KB |
Output isn't correct |
36 |
Incorrect |
8 ms |
2652 KB |
Output isn't correct |
37 |
Correct |
7 ms |
2712 KB |
Output is correct |
38 |
Incorrect |
10 ms |
2652 KB |
Output isn't correct |
39 |
Incorrect |
9 ms |
3164 KB |
Output isn't correct |
40 |
Correct |
8 ms |
3288 KB |
Output is correct |
41 |
Incorrect |
13 ms |
3164 KB |
Output isn't correct |
42 |
Incorrect |
11 ms |
3932 KB |
Output isn't correct |
43 |
Correct |
11 ms |
3932 KB |
Output is correct |
44 |
Incorrect |
15 ms |
3968 KB |
Output isn't correct |
45 |
Incorrect |
12 ms |
4700 KB |
Output isn't correct |
46 |
Correct |
12 ms |
4696 KB |
Output is correct |
47 |
Incorrect |
19 ms |
4680 KB |
Output isn't correct |
48 |
Incorrect |
17 ms |
5488 KB |
Output isn't correct |
49 |
Correct |
15 ms |
5464 KB |
Output is correct |
50 |
Incorrect |
28 ms |
5468 KB |
Output isn't correct |
51 |
Incorrect |
18 ms |
6232 KB |
Output isn't correct |
52 |
Correct |
17 ms |
6236 KB |
Output is correct |
53 |
Incorrect |
24 ms |
6236 KB |
Output isn't correct |
54 |
Incorrect |
21 ms |
7260 KB |
Output isn't correct |
55 |
Correct |
19 ms |
7512 KB |
Output is correct |
56 |
Incorrect |
32 ms |
7260 KB |
Output isn't correct |
57 |
Incorrect |
25 ms |
8260 KB |
Output isn't correct |
58 |
Correct |
25 ms |
8284 KB |
Output is correct |
59 |
Incorrect |
32 ms |
8280 KB |
Output isn't correct |
60 |
Incorrect |
27 ms |
9308 KB |
Output isn't correct |
61 |
Correct |
34 ms |
9560 KB |
Output is correct |
62 |
Incorrect |
36 ms |
9304 KB |
Output isn't correct |
63 |
Correct |
58 ms |
9308 KB |
Output is correct |
64 |
Correct |
53 ms |
9308 KB |
Output is correct |
65 |
Correct |
58 ms |
9344 KB |
Output is correct |
66 |
Incorrect |
61 ms |
9304 KB |
Output isn't correct |
67 |
Correct |
56 ms |
9344 KB |
Output is correct |
68 |
Correct |
68 ms |
9372 KB |
Output is correct |
69 |
Correct |
69 ms |
9304 KB |
Output is correct |
70 |
Correct |
62 ms |
9308 KB |
Output is correct |
71 |
Correct |
61 ms |
9304 KB |
Output is correct |
72 |
Incorrect |
74 ms |
9556 KB |
Output isn't correct |
73 |
Incorrect |
52 ms |
9564 KB |
Output isn't correct |
74 |
Correct |
66 ms |
9564 KB |
Output is correct |
75 |
Correct |
50 ms |
9604 KB |
Output is correct |
76 |
Correct |
53 ms |
9608 KB |
Output is correct |
77 |
Correct |
54 ms |
9816 KB |
Output is correct |
78 |
Correct |
57 ms |
9564 KB |
Output is correct |
79 |
Correct |
53 ms |
9580 KB |
Output is correct |
80 |
Correct |
53 ms |
9560 KB |
Output is correct |
81 |
Correct |
53 ms |
9356 KB |
Output is correct |
82 |
Correct |
53 ms |
9564 KB |
Output is correct |
83 |
Correct |
62 ms |
9304 KB |
Output is correct |
84 |
Correct |
53 ms |
9304 KB |
Output is correct |
85 |
Correct |
50 ms |
9560 KB |
Output is correct |
86 |
Correct |
49 ms |
9564 KB |
Output is correct |
87 |
Correct |
58 ms |
9560 KB |
Output is correct |
88 |
Correct |
53 ms |
9308 KB |
Output is correct |
89 |
Correct |
53 ms |
9308 KB |
Output is correct |
90 |
Correct |
55 ms |
9308 KB |
Output is correct |
91 |
Correct |
56 ms |
9308 KB |
Output is correct |
92 |
Correct |
49 ms |
9304 KB |
Output is correct |