#include <bits/stdc++.h>
#define INF 0x3f3f3f3f
using namespace std;
void testCase() {
int n, s;
cin >> n >> s;
vector<string> a(n);
queue<pair<int, int>> q;
vector<vector<int>> bee(n, vector<int>(n, INF));
int istart = -1, jstart = -1, istop = -1, jstop = -1;
for (int i = 0; i < n; ++i) {
cin >> a[i];
for (int j = 0; j < n; ++j) {
if (a[i][j] == 'H') {
bee[i][j] = 0;
q.emplace(i, j);
} else if (a[i][j] == 'M') {
istart = i, jstart = j;
} else if (a[i][j] == 'D') {
istop = i, jstop = j;
}
}
}
const int di[] = {-1, 0, 1, 0}, dj[] = {0, 1, 0, -1};
auto valid = [&](const int &i, const int &j) -> bool {
return 0 <= min(i, j) && max(i, j) < n && a[i][j] != 'T' && a[i][j] != 'H';
};
while (!q.empty()) {
int i, j;
tie(i, j) = q.front();
q.pop();
for (int k = 0; k < 4; ++k) {
int iv = i + di[k], jv = j + dj[k];
if (valid(iv, jv) && a[iv][jv] != 'D' && bee[iv][jv] == INF) {
bee[iv][jv] = bee[i][j] + 1;
q.emplace(iv, jv);
}
}
}
auto checkDistance = [&](const int &m, const int &b) -> bool {
return m / s < b;
};
auto check = [&](const int &t) -> bool {
vector<vector<int>> dp(n, vector<int>(n, INF));
queue<pair<int, int>> q;
dp[istart][jstart] = 0;
q.emplace(istart, jstart);
while (!q.empty()) {
int i, j;
tie(i, j) = q.front();
q.pop();
for (int k = 0; k < 4; ++k) {
int iv = i + di[k], jv = j + dj[k];
if (valid(iv, jv) && dp[iv][jv] == INF && checkDistance(dp[i][j] + 1, bee[iv][jv] - t)) {
dp[iv][jv] = dp[i][j] + 1;
q.emplace(iv, jv);
}
}
}
if (dp[istop][jstop] == INF) {
return false;
}
return true;
};
int l = 0, r = bee[istart][jstart] - 1, ans = -1;
while (l <= r) {
int mid = (l + r) / 2;
if (check(mid)) {
ans = mid;
l = mid + 1;
} else {
r = mid - 1;
}
}
cout << ans << '\n';
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int tests = 1;
for (int tc = 0; tc < tests; ++tc) {
testCase();
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
83 ms |
6104 KB |
Output is correct |
8 |
Correct |
1 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 |
0 ms |
204 KB |
Output is correct |
12 |
Correct |
0 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 |
0 ms |
204 KB |
Output is correct |
16 |
Correct |
1 ms |
320 KB |
Output is correct |
17 |
Correct |
0 ms |
204 KB |
Output is correct |
18 |
Correct |
0 ms |
204 KB |
Output is correct |
19 |
Correct |
0 ms |
204 KB |
Output is correct |
20 |
Correct |
0 ms |
204 KB |
Output is correct |
21 |
Correct |
1 ms |
332 KB |
Output is correct |
22 |
Correct |
0 ms |
204 KB |
Output is correct |
23 |
Correct |
1 ms |
332 KB |
Output is correct |
24 |
Correct |
0 ms |
332 KB |
Output is correct |
25 |
Correct |
1 ms |
332 KB |
Output is correct |
26 |
Correct |
1 ms |
332 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 |
332 KB |
Output is correct |
30 |
Correct |
1 ms |
332 KB |
Output is correct |
31 |
Correct |
1 ms |
332 KB |
Output is correct |
32 |
Correct |
1 ms |
332 KB |
Output is correct |
33 |
Correct |
6 ms |
1468 KB |
Output is correct |
34 |
Correct |
5 ms |
1416 KB |
Output is correct |
35 |
Correct |
18 ms |
1412 KB |
Output is correct |
36 |
Correct |
7 ms |
1740 KB |
Output is correct |
37 |
Correct |
7 ms |
1740 KB |
Output is correct |
38 |
Correct |
26 ms |
1792 KB |
Output is correct |
39 |
Correct |
9 ms |
2160 KB |
Output is correct |
40 |
Correct |
10 ms |
2168 KB |
Output is correct |
41 |
Correct |
37 ms |
2176 KB |
Output is correct |
42 |
Correct |
16 ms |
2616 KB |
Output is correct |
43 |
Correct |
12 ms |
2584 KB |
Output is correct |
44 |
Correct |
40 ms |
2612 KB |
Output is correct |
45 |
Correct |
13 ms |
3036 KB |
Output is correct |
46 |
Correct |
15 ms |
3040 KB |
Output is correct |
47 |
Correct |
49 ms |
3044 KB |
Output is correct |
48 |
Correct |
15 ms |
3568 KB |
Output is correct |
49 |
Correct |
18 ms |
3616 KB |
Output is correct |
50 |
Correct |
70 ms |
3596 KB |
Output is correct |
51 |
Correct |
18 ms |
4180 KB |
Output is correct |
52 |
Correct |
20 ms |
4152 KB |
Output is correct |
53 |
Correct |
70 ms |
4128 KB |
Output is correct |
54 |
Correct |
21 ms |
4788 KB |
Output is correct |
55 |
Correct |
23 ms |
4776 KB |
Output is correct |
56 |
Correct |
80 ms |
4740 KB |
Output is correct |
57 |
Correct |
26 ms |
5388 KB |
Output is correct |
58 |
Correct |
29 ms |
5408 KB |
Output is correct |
59 |
Correct |
94 ms |
5392 KB |
Output is correct |
60 |
Correct |
28 ms |
6180 KB |
Output is correct |
61 |
Correct |
31 ms |
6116 KB |
Output is correct |
62 |
Correct |
115 ms |
6064 KB |
Output is correct |
63 |
Correct |
114 ms |
6164 KB |
Output is correct |
64 |
Correct |
202 ms |
6160 KB |
Output is correct |
65 |
Correct |
201 ms |
6056 KB |
Output is correct |
66 |
Correct |
137 ms |
6140 KB |
Output is correct |
67 |
Correct |
121 ms |
6072 KB |
Output is correct |
68 |
Correct |
49 ms |
6092 KB |
Output is correct |
69 |
Correct |
43 ms |
6116 KB |
Output is correct |
70 |
Correct |
42 ms |
6116 KB |
Output is correct |
71 |
Correct |
34 ms |
6092 KB |
Output is correct |
72 |
Correct |
35 ms |
6208 KB |
Output is correct |
73 |
Correct |
35 ms |
6128 KB |
Output is correct |
74 |
Correct |
66 ms |
6096 KB |
Output is correct |
75 |
Correct |
62 ms |
6104 KB |
Output is correct |
76 |
Correct |
62 ms |
6112 KB |
Output is correct |
77 |
Correct |
63 ms |
6120 KB |
Output is correct |
78 |
Correct |
74 ms |
6104 KB |
Output is correct |
79 |
Correct |
59 ms |
6124 KB |
Output is correct |
80 |
Correct |
62 ms |
6108 KB |
Output is correct |
81 |
Correct |
68 ms |
6120 KB |
Output is correct |
82 |
Correct |
58 ms |
6092 KB |
Output is correct |
83 |
Correct |
77 ms |
6100 KB |
Output is correct |
84 |
Correct |
66 ms |
6104 KB |
Output is correct |
85 |
Correct |
65 ms |
6088 KB |
Output is correct |
86 |
Correct |
86 ms |
6104 KB |
Output is correct |
87 |
Correct |
77 ms |
6148 KB |
Output is correct |
88 |
Correct |
81 ms |
6096 KB |
Output is correct |
89 |
Correct |
80 ms |
6092 KB |
Output is correct |
90 |
Correct |
83 ms |
6084 KB |
Output is correct |
91 |
Correct |
82 ms |
6068 KB |
Output is correct |
92 |
Correct |
84 ms |
6076 KB |
Output is correct |