#undef _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
void debug_out() {cerr<<endl;}
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {cerr<<" "<<to_string(H);debug_out(T...);}
#define debug(...) cerr<<"["<<#__VA_ARGS__<<"]:",debug_out(__VA_ARGS__)
#define fast ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define int long long
#define pb push_back
#define fi first
#define si second
typedef pair<int,int> pi;
typedef tuple<int,int,int> ti;
template<typename T> bool chmin(T &a, T b) {return (b < a) ? a = b, 1 : 0;}
template<typename T> bool chmax(T &a, T b) {return (b > a) ? a = b, 1 : 0;}
const int INF = 1e9;
int N, S;
char A[810][810];
int dist[810][810], rev[810][810];
int dx[] = {1, -1, 0, 0};
int dy[] = {0, 0, 1, -1};
queue<pi> q;
pi st;
bool inside(int x, int y) {return (x >= 1 && x <= N && y >= 1 && y <= N);}
bool good(int delta) {
while (q.size()) q.pop();
memset(rev, -1, sizeof rev);
rev[st.fi][st.si] = 0;
q.push(st);
while (q.size()) {
auto [x, y] = q.front(); q.pop();
if (rev[x][y] / S + delta >= dist[x][y]) continue;
if (A[x][y] == 'D') return 1;
for (int i = 0; i < 4; ++i) {
int nx = x + dx[i], ny = y + dy[i];
if (A[nx][ny] == 'T') continue;
if (!inside(nx, ny)) continue;
if (rev[nx][ny] != -1) continue;
rev[nx][ny] = rev[x][y] + 1;
q.push({nx, ny});
}
}
return 0;
}
signed main() {
fast;
cin >> N >> S;
for (int i = 1; i <= N; ++i) {
for (int j = 1; j <= N; ++j) {
cin >> A[i][j];
dist[i][j] = INF;
if (A[i][j] == 'H') {
q.push({i, j});
dist[i][j] = 0;
} else if (A[i][j] == 'M') st = pi(i, j);
}
}
while (q.size()) {
auto [x, y] = q.front(); q.pop();
for (int i = 0; i < 4; ++i) {
int nx = x + dx[i], ny = y + dy[i];
if (A[nx][ny] == 'T' || A[nx][ny] == 'D') continue;
if (!inside(nx, ny)) continue;
if (dist[nx][ny] != INF) continue;
dist[nx][ny] = dist[x][y] + 1;
q.push({nx, ny});
}
}
if (!good(0)) return cout << -1, 0;
int lo = -1, hi = 1e9 + 1;
while (lo + 1 < hi) {
int mid = (lo + hi) / 2;
if (good(mid)) lo = mid;
else hi = mid;
}
cout << lo;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
5460 KB |
Output is correct |
2 |
Correct |
7 ms |
5460 KB |
Output is correct |
3 |
Correct |
7 ms |
5460 KB |
Output is correct |
4 |
Correct |
7 ms |
5496 KB |
Output is correct |
5 |
Correct |
7 ms |
5504 KB |
Output is correct |
6 |
Correct |
7 ms |
5516 KB |
Output is correct |
7 |
Correct |
117 ms |
11392 KB |
Output is correct |
8 |
Correct |
2 ms |
5460 KB |
Output is correct |
9 |
Correct |
8 ms |
5460 KB |
Output is correct |
10 |
Correct |
7 ms |
5540 KB |
Output is correct |
11 |
Correct |
8 ms |
5460 KB |
Output is correct |
12 |
Correct |
9 ms |
5784 KB |
Output is correct |
13 |
Correct |
7 ms |
5716 KB |
Output is correct |
14 |
Correct |
3 ms |
5784 KB |
Output is correct |
15 |
Correct |
7 ms |
5564 KB |
Output is correct |
16 |
Correct |
8 ms |
5460 KB |
Output is correct |
17 |
Correct |
8 ms |
5588 KB |
Output is correct |
18 |
Correct |
7 ms |
5588 KB |
Output is correct |
19 |
Correct |
6 ms |
5588 KB |
Output is correct |
20 |
Correct |
7 ms |
5588 KB |
Output is correct |
21 |
Correct |
7 ms |
5588 KB |
Output is correct |
22 |
Correct |
7 ms |
5588 KB |
Output is correct |
23 |
Correct |
7 ms |
5716 KB |
Output is correct |
24 |
Correct |
7 ms |
5716 KB |
Output is correct |
25 |
Correct |
8 ms |
5784 KB |
Output is correct |
26 |
Correct |
7 ms |
5716 KB |
Output is correct |
27 |
Correct |
8 ms |
5716 KB |
Output is correct |
28 |
Correct |
7 ms |
5716 KB |
Output is correct |
29 |
Correct |
7 ms |
5844 KB |
Output is correct |
30 |
Correct |
7 ms |
5844 KB |
Output is correct |
31 |
Correct |
13 ms |
5872 KB |
Output is correct |
32 |
Correct |
8 ms |
5788 KB |
Output is correct |
33 |
Correct |
15 ms |
7892 KB |
Output is correct |
34 |
Correct |
15 ms |
7972 KB |
Output is correct |
35 |
Correct |
29 ms |
7892 KB |
Output is correct |
36 |
Correct |
11 ms |
8276 KB |
Output is correct |
37 |
Correct |
12 ms |
8312 KB |
Output is correct |
38 |
Correct |
31 ms |
8276 KB |
Output is correct |
39 |
Correct |
14 ms |
8668 KB |
Output is correct |
40 |
Correct |
11 ms |
8680 KB |
Output is correct |
41 |
Correct |
54 ms |
8672 KB |
Output is correct |
42 |
Correct |
13 ms |
9032 KB |
Output is correct |
43 |
Correct |
13 ms |
8916 KB |
Output is correct |
44 |
Correct |
74 ms |
8916 KB |
Output is correct |
45 |
Correct |
16 ms |
9392 KB |
Output is correct |
46 |
Correct |
19 ms |
9300 KB |
Output is correct |
47 |
Correct |
69 ms |
9392 KB |
Output is correct |
48 |
Correct |
16 ms |
9720 KB |
Output is correct |
49 |
Correct |
16 ms |
9744 KB |
Output is correct |
50 |
Correct |
71 ms |
9748 KB |
Output is correct |
51 |
Correct |
19 ms |
10060 KB |
Output is correct |
52 |
Correct |
19 ms |
10016 KB |
Output is correct |
53 |
Correct |
106 ms |
10232 KB |
Output is correct |
54 |
Correct |
19 ms |
10460 KB |
Output is correct |
55 |
Correct |
23 ms |
10364 KB |
Output is correct |
56 |
Correct |
95 ms |
10460 KB |
Output is correct |
57 |
Correct |
21 ms |
10776 KB |
Output is correct |
58 |
Correct |
19 ms |
10808 KB |
Output is correct |
59 |
Correct |
138 ms |
10804 KB |
Output is correct |
60 |
Correct |
25 ms |
11092 KB |
Output is correct |
61 |
Correct |
22 ms |
11164 KB |
Output is correct |
62 |
Correct |
128 ms |
11176 KB |
Output is correct |
63 |
Correct |
129 ms |
11160 KB |
Output is correct |
64 |
Correct |
221 ms |
11168 KB |
Output is correct |
65 |
Correct |
200 ms |
11172 KB |
Output is correct |
66 |
Correct |
134 ms |
11048 KB |
Output is correct |
67 |
Correct |
39 ms |
11264 KB |
Output is correct |
68 |
Correct |
62 ms |
11220 KB |
Output is correct |
69 |
Correct |
62 ms |
11216 KB |
Output is correct |
70 |
Correct |
44 ms |
11220 KB |
Output is correct |
71 |
Correct |
43 ms |
11208 KB |
Output is correct |
72 |
Correct |
40 ms |
11220 KB |
Output is correct |
73 |
Correct |
57 ms |
11608 KB |
Output is correct |
74 |
Correct |
78 ms |
11664 KB |
Output is correct |
75 |
Correct |
103 ms |
11720 KB |
Output is correct |
76 |
Correct |
75 ms |
11720 KB |
Output is correct |
77 |
Correct |
85 ms |
11716 KB |
Output is correct |
78 |
Correct |
37 ms |
11660 KB |
Output is correct |
79 |
Correct |
69 ms |
11772 KB |
Output is correct |
80 |
Correct |
68 ms |
11624 KB |
Output is correct |
81 |
Correct |
84 ms |
11668 KB |
Output is correct |
82 |
Correct |
95 ms |
11596 KB |
Output is correct |
83 |
Correct |
92 ms |
11584 KB |
Output is correct |
84 |
Correct |
88 ms |
11452 KB |
Output is correct |
85 |
Correct |
87 ms |
11584 KB |
Output is correct |
86 |
Correct |
113 ms |
11580 KB |
Output is correct |
87 |
Correct |
89 ms |
11484 KB |
Output is correct |
88 |
Correct |
96 ms |
11480 KB |
Output is correct |
89 |
Correct |
98 ms |
11476 KB |
Output is correct |
90 |
Correct |
102 ms |
11484 KB |
Output is correct |
91 |
Correct |
95 ms |
11468 KB |
Output is correct |
92 |
Correct |
92 ms |
11488 KB |
Output is correct |