#include <bits/stdc++.h>
#define INF 0x3f3f3f3f
using namespace std;
void testCase() {
int n, s;
cin >> n >> s;
vector<string> a(n + 1);
queue<pair<int, int>> q;
vector<vector<int>> bee(n + 1, vector<int>(n + 1, INF));
int istart = -1, jstart = -1, istop = -1, jstop = -1;
for (int i = 1; i <= n; ++i) {
cin >> a[i];
a[i] = '$' + a[i];
for (int j = 1; 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 1 <= min(i, j) && max(i, j) <= n && a[i][j] != 'T';
};
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] > bee[i][j] + 1) {
bee[iv][jv] = bee[i][j] + 1;
q.emplace(iv, jv);
}
}
}
auto check = [&](const int &t) -> bool {
vector<vector<int>> dp(n + 1, vector<int>(n + 1, INF));
queue<pair<int, int>> q;
dp[istart][jstart] = t;
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] > dp[i][j] + 1 && (dp[i][j] + 1) / 6 < (bee[i][j] - 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 |
Incorrect |
0 ms |
208 KB |
Output isn't correct |
2 |
Incorrect |
0 ms |
208 KB |
Output isn't correct |
3 |
Incorrect |
0 ms |
208 KB |
Output isn't correct |
4 |
Incorrect |
0 ms |
316 KB |
Output isn't correct |
5 |
Correct |
1 ms |
316 KB |
Output is correct |
6 |
Incorrect |
0 ms |
316 KB |
Output isn't correct |
7 |
Incorrect |
61 ms |
6748 KB |
Output isn't correct |
8 |
Incorrect |
1 ms |
208 KB |
Output isn't correct |
9 |
Incorrect |
0 ms |
316 KB |
Output isn't correct |
10 |
Incorrect |
1 ms |
208 KB |
Output isn't correct |
11 |
Correct |
1 ms |
316 KB |
Output is correct |
12 |
Incorrect |
1 ms |
336 KB |
Output isn't correct |
13 |
Correct |
1 ms |
316 KB |
Output is correct |
14 |
Incorrect |
1 ms |
336 KB |
Output isn't correct |
15 |
Incorrect |
0 ms |
208 KB |
Output isn't correct |
16 |
Incorrect |
0 ms |
208 KB |
Output isn't correct |
17 |
Incorrect |
1 ms |
316 KB |
Output isn't correct |
18 |
Incorrect |
1 ms |
208 KB |
Output isn't correct |
19 |
Incorrect |
0 ms |
320 KB |
Output isn't correct |
20 |
Incorrect |
1 ms |
208 KB |
Output isn't correct |
21 |
Incorrect |
1 ms |
208 KB |
Output isn't correct |
22 |
Incorrect |
1 ms |
208 KB |
Output isn't correct |
23 |
Incorrect |
0 ms |
336 KB |
Output isn't correct |
24 |
Incorrect |
0 ms |
336 KB |
Output isn't correct |
25 |
Incorrect |
1 ms |
336 KB |
Output isn't correct |
26 |
Incorrect |
1 ms |
336 KB |
Output isn't correct |
27 |
Incorrect |
1 ms |
336 KB |
Output isn't correct |
28 |
Incorrect |
1 ms |
336 KB |
Output isn't correct |
29 |
Incorrect |
1 ms |
336 KB |
Output isn't correct |
30 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
31 |
Incorrect |
1 ms |
336 KB |
Output isn't correct |
32 |
Incorrect |
1 ms |
336 KB |
Output isn't correct |
33 |
Incorrect |
6 ms |
1584 KB |
Output isn't correct |
34 |
Incorrect |
5 ms |
1568 KB |
Output isn't correct |
35 |
Incorrect |
15 ms |
1528 KB |
Output isn't correct |
36 |
Incorrect |
10 ms |
1932 KB |
Output isn't correct |
37 |
Incorrect |
7 ms |
1868 KB |
Output isn't correct |
38 |
Incorrect |
18 ms |
1944 KB |
Output isn't correct |
39 |
Incorrect |
9 ms |
2372 KB |
Output isn't correct |
40 |
Incorrect |
8 ms |
2368 KB |
Output isn't correct |
41 |
Incorrect |
23 ms |
2364 KB |
Output isn't correct |
42 |
Incorrect |
14 ms |
2840 KB |
Output isn't correct |
43 |
Incorrect |
11 ms |
2880 KB |
Output isn't correct |
44 |
Incorrect |
29 ms |
2820 KB |
Output isn't correct |
45 |
Incorrect |
13 ms |
3400 KB |
Output isn't correct |
46 |
Incorrect |
12 ms |
3404 KB |
Output isn't correct |
47 |
Incorrect |
38 ms |
3380 KB |
Output isn't correct |
48 |
Incorrect |
15 ms |
3940 KB |
Output isn't correct |
49 |
Incorrect |
16 ms |
3980 KB |
Output isn't correct |
50 |
Incorrect |
46 ms |
3932 KB |
Output isn't correct |
51 |
Incorrect |
18 ms |
4616 KB |
Output isn't correct |
52 |
Incorrect |
18 ms |
4604 KB |
Output isn't correct |
53 |
Incorrect |
52 ms |
4584 KB |
Output isn't correct |
54 |
Incorrect |
21 ms |
5252 KB |
Output isn't correct |
55 |
Incorrect |
21 ms |
5268 KB |
Output isn't correct |
56 |
Incorrect |
77 ms |
5548 KB |
Output isn't correct |
57 |
Incorrect |
26 ms |
6000 KB |
Output isn't correct |
58 |
Incorrect |
24 ms |
5952 KB |
Output isn't correct |
59 |
Incorrect |
71 ms |
5956 KB |
Output isn't correct |
60 |
Incorrect |
34 ms |
6752 KB |
Output isn't correct |
61 |
Incorrect |
28 ms |
6744 KB |
Output isn't correct |
62 |
Incorrect |
84 ms |
6712 KB |
Output isn't correct |
63 |
Incorrect |
145 ms |
6688 KB |
Output isn't correct |
64 |
Incorrect |
171 ms |
6804 KB |
Output isn't correct |
65 |
Incorrect |
159 ms |
6712 KB |
Output isn't correct |
66 |
Incorrect |
126 ms |
6700 KB |
Output isn't correct |
67 |
Incorrect |
141 ms |
6800 KB |
Output isn't correct |
68 |
Incorrect |
32 ms |
6716 KB |
Output isn't correct |
69 |
Incorrect |
30 ms |
6732 KB |
Output isn't correct |
70 |
Incorrect |
31 ms |
6712 KB |
Output isn't correct |
71 |
Incorrect |
30 ms |
6716 KB |
Output isn't correct |
72 |
Incorrect |
31 ms |
6720 KB |
Output isn't correct |
73 |
Correct |
35 ms |
6716 KB |
Output is correct |
74 |
Incorrect |
62 ms |
6788 KB |
Output isn't correct |
75 |
Incorrect |
61 ms |
6728 KB |
Output isn't correct |
76 |
Incorrect |
58 ms |
6760 KB |
Output isn't correct |
77 |
Incorrect |
63 ms |
6748 KB |
Output isn't correct |
78 |
Correct |
60 ms |
6848 KB |
Output is correct |
79 |
Incorrect |
54 ms |
6728 KB |
Output isn't correct |
80 |
Incorrect |
54 ms |
6724 KB |
Output isn't correct |
81 |
Incorrect |
62 ms |
6744 KB |
Output isn't correct |
82 |
Incorrect |
63 ms |
6736 KB |
Output isn't correct |
83 |
Incorrect |
69 ms |
6744 KB |
Output isn't correct |
84 |
Incorrect |
54 ms |
6728 KB |
Output isn't correct |
85 |
Incorrect |
51 ms |
6712 KB |
Output isn't correct |
86 |
Incorrect |
56 ms |
6732 KB |
Output isn't correct |
87 |
Incorrect |
54 ms |
6728 KB |
Output isn't correct |
88 |
Incorrect |
75 ms |
6764 KB |
Output isn't correct |
89 |
Incorrect |
68 ms |
6856 KB |
Output isn't correct |
90 |
Incorrect |
67 ms |
6732 KB |
Output isn't correct |
91 |
Incorrect |
66 ms |
6716 KB |
Output isn't correct |
92 |
Incorrect |
59 ms |
6708 KB |
Output isn't correct |