#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
#define ll long long
#define all(x) x.begin(), x.end()
ll n, s, times[810][810];
vector<string> v(810);
pair<ll, ll> st, en;
const ll dx[4] = {0, 0, -1, 1};
const ll dy[4] = {-1, 1, 0, 0};
inline bool valid(ll x, ll y) {
return (x >= 0 && x < n && y >= 0 && y < n && v[x][y] != 'T');
}
bool check(ll m) {
if (!(m < times[st.first][st.second])) return 0;
queue<pair<ll, ll>> q;
q.push(st);
bool vis[n + 2][n + 2];
memset(vis, 0, sizeof(vis));
vis[st.first][st.second] = 1;
ll moves = 0;
do {
ll sz = q.size();
while (sz--) {
ll x = q.front().first, y = q.front().second;
q.pop();
for (ll d = 0; d < 4; d++) {
ll nx = x + dx[d], ny = y + dy[d];
if (valid(nx, ny) && ((m + (moves + 1 == s)) < times[nx][ny] || ((m + (moves + 1 == s)) == times[nx][ny] && make_pair(nx, ny) == en)) && !vis[nx][ny]) {
vis[nx][ny] = 1;
q.push({nx, ny});
if (make_pair(nx, ny) == en) return 1;
}
}
}
moves++;
if (moves >= s) {
moves = 0;
m++;
}
}
while (q.size() > 0);
return 0;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n >> s;
queue<pair<ll, ll>> h;
for (ll i = 0; i < n; i++) {
cin >> v[i];
for (ll j = 0; j < n; j++) {
times[i][j] = n * n + 1000;
if (v[i][j] == 'M') st = {i, j};
if (v[i][j] == 'H') {
h.push({i, j});
times[i][j] = 0;
}
if (v[i][j] == 'D') en = {i, j};
}
}
ll mx = 0;
while (h.size() > 0) {
ll x = h.front().first, y = h.front().second;
h.pop();
for (ll d = 0; d < 4; d++) {
ll nx = x + dx[d], ny = y + dy[d];
if (valid(nx, ny) && times[x][y] + 1 < times[nx][ny] && v[nx][ny] != 'D') {
times[nx][ny] = times[x][y] + 1;
mx = max(mx, times[nx][ny]);
h.push({nx, ny});
}
}
}
ll l = -1, r = mx + 1;
while (l + 1 < r) {
ll m = (l + r) / 2;
if (check(m)) l = m;
else r = m;
}
cout << l;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
109 ms |
6844 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
596 KB |
Output is correct |
13 |
Correct |
1 ms |
468 KB |
Output is correct |
14 |
Correct |
1 ms |
596 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
0 ms |
340 KB |
Output is correct |
18 |
Correct |
0 ms |
340 KB |
Output is correct |
19 |
Correct |
0 ms |
468 KB |
Output is correct |
20 |
Correct |
0 ms |
468 KB |
Output is correct |
21 |
Correct |
0 ms |
468 KB |
Output is correct |
22 |
Correct |
1 ms |
552 KB |
Output is correct |
23 |
Correct |
0 ms |
468 KB |
Output is correct |
24 |
Correct |
1 ms |
468 KB |
Output is correct |
25 |
Correct |
1 ms |
596 KB |
Output is correct |
26 |
Correct |
1 ms |
616 KB |
Output is correct |
27 |
Correct |
1 ms |
596 KB |
Output is correct |
28 |
Correct |
1 ms |
596 KB |
Output is correct |
29 |
Correct |
1 ms |
596 KB |
Output is correct |
30 |
Correct |
1 ms |
596 KB |
Output is correct |
31 |
Correct |
1 ms |
596 KB |
Output is correct |
32 |
Correct |
1 ms |
596 KB |
Output is correct |
33 |
Correct |
3 ms |
2772 KB |
Output is correct |
34 |
Correct |
4 ms |
2772 KB |
Output is correct |
35 |
Correct |
20 ms |
2712 KB |
Output is correct |
36 |
Correct |
4 ms |
3156 KB |
Output is correct |
37 |
Correct |
3 ms |
3156 KB |
Output is correct |
38 |
Correct |
22 ms |
3232 KB |
Output is correct |
39 |
Correct |
4 ms |
3540 KB |
Output is correct |
40 |
Correct |
4 ms |
3540 KB |
Output is correct |
41 |
Correct |
31 ms |
3628 KB |
Output is correct |
42 |
Correct |
5 ms |
3924 KB |
Output is correct |
43 |
Correct |
5 ms |
4028 KB |
Output is correct |
44 |
Correct |
38 ms |
3932 KB |
Output is correct |
45 |
Correct |
5 ms |
4412 KB |
Output is correct |
46 |
Correct |
6 ms |
4436 KB |
Output is correct |
47 |
Correct |
45 ms |
4436 KB |
Output is correct |
48 |
Correct |
6 ms |
4820 KB |
Output is correct |
49 |
Correct |
6 ms |
4884 KB |
Output is correct |
50 |
Correct |
57 ms |
4892 KB |
Output is correct |
51 |
Correct |
6 ms |
5332 KB |
Output is correct |
52 |
Correct |
6 ms |
5316 KB |
Output is correct |
53 |
Correct |
70 ms |
5324 KB |
Output is correct |
54 |
Correct |
11 ms |
5696 KB |
Output is correct |
55 |
Correct |
11 ms |
5740 KB |
Output is correct |
56 |
Correct |
94 ms |
5708 KB |
Output is correct |
57 |
Correct |
12 ms |
6156 KB |
Output is correct |
58 |
Correct |
8 ms |
6228 KB |
Output is correct |
59 |
Correct |
107 ms |
6248 KB |
Output is correct |
60 |
Correct |
9 ms |
6612 KB |
Output is correct |
61 |
Correct |
9 ms |
6612 KB |
Output is correct |
62 |
Correct |
101 ms |
6628 KB |
Output is correct |
63 |
Correct |
76 ms |
6712 KB |
Output is correct |
64 |
Correct |
144 ms |
6712 KB |
Output is correct |
65 |
Correct |
129 ms |
6708 KB |
Output is correct |
66 |
Correct |
99 ms |
6704 KB |
Output is correct |
67 |
Correct |
85 ms |
6612 KB |
Output is correct |
68 |
Correct |
34 ms |
6740 KB |
Output is correct |
69 |
Correct |
26 ms |
6672 KB |
Output is correct |
70 |
Correct |
27 ms |
6768 KB |
Output is correct |
71 |
Correct |
25 ms |
6788 KB |
Output is correct |
72 |
Correct |
20 ms |
6668 KB |
Output is correct |
73 |
Correct |
31 ms |
7172 KB |
Output is correct |
74 |
Correct |
52 ms |
7216 KB |
Output is correct |
75 |
Correct |
57 ms |
7268 KB |
Output is correct |
76 |
Correct |
52 ms |
7168 KB |
Output is correct |
77 |
Correct |
57 ms |
7212 KB |
Output is correct |
78 |
Correct |
61 ms |
7112 KB |
Output is correct |
79 |
Correct |
39 ms |
7124 KB |
Output is correct |
80 |
Correct |
50 ms |
7124 KB |
Output is correct |
81 |
Correct |
59 ms |
7156 KB |
Output is correct |
82 |
Correct |
45 ms |
7236 KB |
Output is correct |
83 |
Correct |
69 ms |
7124 KB |
Output is correct |
84 |
Correct |
62 ms |
7124 KB |
Output is correct |
85 |
Correct |
56 ms |
7124 KB |
Output is correct |
86 |
Correct |
67 ms |
7136 KB |
Output is correct |
87 |
Correct |
66 ms |
7124 KB |
Output is correct |
88 |
Correct |
71 ms |
7024 KB |
Output is correct |
89 |
Correct |
78 ms |
7048 KB |
Output is correct |
90 |
Correct |
75 ms |
6996 KB |
Output is correct |
91 |
Correct |
71 ms |
6936 KB |
Output is correct |
92 |
Correct |
73 ms |
6996 KB |
Output is correct |