#include <bits/stdc++.h>
using namespace std;
#ifdef tabr
#include "library/debug.cpp"
#else
#define debug(...)
#endif
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n, m;
cin >> n >> m;
vector<string> s(n);
for (int i = 0; i < n; i++) {
cin >> s[i];
}
vector<vector<int>> a(n, vector<int>(n, -1));
queue<int> que;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (s[i][j] == 'H') {
a[i][j] = 0;
que.emplace(i * n + j);
} else if (s[i][j] == 'D' || s[i][j] == 'T') {
a[i][j] = 0;
}
}
}
vector<int> dx = {1, 0, -1, 0};
vector<int> dy = {0, 1, 0, -1};
while (!que.empty()) {
int x = que.front() / n;
int y = que.front() % n;
que.pop();
for (int dir = 0; dir < 4; dir++) {
int nx = x + dx[dir];
int ny = y + dy[dir];
if (min(nx, ny) < 0 || max(nx, ny) >= n || a[nx][ny] != -1) {
continue;
}
a[nx][ny] = a[x][y] + 1;
que.emplace(nx * n + ny);
}
}
int sx = -1;
int sy = -1;
int tx = -1;
int ty = -1;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (s[i][j] == 'M') {
sx = i;
sy = j;
} else if (s[i][j] == 'D') {
tx = i;
ty = j;
}
}
}
debug(a);
int lo = -1;
int hi = a[sx][sy];
while (hi - lo > 1) {
int mid = (hi + lo) / 2;
vector<vector<int>> b(n, vector<int>(n, -1));
que.emplace(sx * n + sy);
b[sx][sy] = 0;
while (!que.empty()) {
int x = que.front() / n;
int y = que.front() % n;
que.pop();
for (int dir = 0; dir < 4; dir++) {
int nx = x + dx[dir];
int ny = y + dy[dir];
if (nx == tx && ny == ty) {
b[tx][ty] = 0;
break;
}
if (min(nx, ny) < 0 || max(nx, ny) >= n || b[nx][ny] != -1 || b[x][y] + 1 >= m * (a[nx][ny] - mid)) {
continue;
}
b[nx][ny] = b[x][y] + 1;
que.emplace(nx * n + ny);
}
}
if (b[tx][ty] != -1) {
lo = mid;
} else {
hi = mid;
}
}
cout << lo << '\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
308 KB |
Output is correct |
6 |
Correct |
0 ms |
308 KB |
Output is correct |
7 |
Correct |
80 ms |
6764 KB |
Output is correct |
8 |
Correct |
0 ms |
204 KB |
Output is correct |
9 |
Correct |
0 ms |
204 KB |
Output is correct |
10 |
Correct |
0 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
316 KB |
Output is correct |
16 |
Correct |
1 ms |
204 KB |
Output is correct |
17 |
Correct |
1 ms |
204 KB |
Output is correct |
18 |
Correct |
0 ms |
204 KB |
Output is correct |
19 |
Correct |
1 ms |
204 KB |
Output is correct |
20 |
Correct |
0 ms |
312 KB |
Output is correct |
21 |
Correct |
1 ms |
332 KB |
Output is correct |
22 |
Correct |
0 ms |
332 KB |
Output is correct |
23 |
Correct |
0 ms |
312 KB |
Output is correct |
24 |
Correct |
1 ms |
312 KB |
Output is correct |
25 |
Correct |
1 ms |
332 KB |
Output is correct |
26 |
Correct |
1 ms |
324 KB |
Output is correct |
27 |
Correct |
1 ms |
332 KB |
Output is correct |
28 |
Correct |
1 ms |
332 KB |
Output is correct |
29 |
Correct |
1 ms |
320 KB |
Output is correct |
30 |
Correct |
1 ms |
312 KB |
Output is correct |
31 |
Correct |
1 ms |
332 KB |
Output is correct |
32 |
Correct |
1 ms |
332 KB |
Output is correct |
33 |
Correct |
5 ms |
1548 KB |
Output is correct |
34 |
Correct |
5 ms |
1580 KB |
Output is correct |
35 |
Correct |
18 ms |
1524 KB |
Output is correct |
36 |
Correct |
7 ms |
1916 KB |
Output is correct |
37 |
Correct |
8 ms |
1932 KB |
Output is correct |
38 |
Correct |
25 ms |
1856 KB |
Output is correct |
39 |
Correct |
9 ms |
2344 KB |
Output is correct |
40 |
Correct |
9 ms |
2328 KB |
Output is correct |
41 |
Correct |
32 ms |
2328 KB |
Output is correct |
42 |
Correct |
11 ms |
2892 KB |
Output is correct |
43 |
Correct |
12 ms |
2868 KB |
Output is correct |
44 |
Correct |
48 ms |
2804 KB |
Output is correct |
45 |
Correct |
20 ms |
3332 KB |
Output is correct |
46 |
Correct |
15 ms |
3328 KB |
Output is correct |
47 |
Correct |
49 ms |
3320 KB |
Output is correct |
48 |
Correct |
16 ms |
3964 KB |
Output is correct |
49 |
Correct |
16 ms |
3932 KB |
Output is correct |
50 |
Correct |
62 ms |
4032 KB |
Output is correct |
51 |
Correct |
20 ms |
4524 KB |
Output is correct |
52 |
Correct |
20 ms |
4580 KB |
Output is correct |
53 |
Correct |
76 ms |
4544 KB |
Output is correct |
54 |
Correct |
24 ms |
5256 KB |
Output is correct |
55 |
Correct |
23 ms |
5264 KB |
Output is correct |
56 |
Correct |
80 ms |
5228 KB |
Output is correct |
57 |
Correct |
27 ms |
5964 KB |
Output is correct |
58 |
Correct |
26 ms |
5960 KB |
Output is correct |
59 |
Correct |
112 ms |
5932 KB |
Output is correct |
60 |
Correct |
34 ms |
6836 KB |
Output is correct |
61 |
Correct |
29 ms |
6740 KB |
Output is correct |
62 |
Correct |
123 ms |
6704 KB |
Output is correct |
63 |
Correct |
111 ms |
6700 KB |
Output is correct |
64 |
Correct |
169 ms |
6692 KB |
Output is correct |
65 |
Correct |
173 ms |
6820 KB |
Output is correct |
66 |
Correct |
134 ms |
6688 KB |
Output is correct |
67 |
Correct |
112 ms |
6688 KB |
Output is correct |
68 |
Correct |
45 ms |
6736 KB |
Output is correct |
69 |
Correct |
42 ms |
6720 KB |
Output is correct |
70 |
Correct |
37 ms |
6716 KB |
Output is correct |
71 |
Correct |
30 ms |
6732 KB |
Output is correct |
72 |
Correct |
33 ms |
6712 KB |
Output is correct |
73 |
Correct |
33 ms |
6716 KB |
Output is correct |
74 |
Correct |
57 ms |
6756 KB |
Output is correct |
75 |
Correct |
75 ms |
6672 KB |
Output is correct |
76 |
Correct |
73 ms |
6736 KB |
Output is correct |
77 |
Correct |
57 ms |
6732 KB |
Output is correct |
78 |
Correct |
64 ms |
6736 KB |
Output is correct |
79 |
Correct |
57 ms |
6712 KB |
Output is correct |
80 |
Correct |
62 ms |
6736 KB |
Output is correct |
81 |
Correct |
59 ms |
6720 KB |
Output is correct |
82 |
Correct |
56 ms |
6736 KB |
Output is correct |
83 |
Correct |
74 ms |
6704 KB |
Output is correct |
84 |
Correct |
79 ms |
7000 KB |
Output is correct |
85 |
Correct |
63 ms |
6776 KB |
Output is correct |
86 |
Correct |
71 ms |
6720 KB |
Output is correct |
87 |
Correct |
66 ms |
6716 KB |
Output is correct |
88 |
Correct |
92 ms |
6756 KB |
Output is correct |
89 |
Correct |
70 ms |
6700 KB |
Output is correct |
90 |
Correct |
78 ms |
6724 KB |
Output is correct |
91 |
Correct |
88 ms |
6724 KB |
Output is correct |
92 |
Correct |
77 ms |
6684 KB |
Output is correct |