#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;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
5460 KB |
Output is correct |
2 |
Correct |
9 ms |
5460 KB |
Output is correct |
3 |
Correct |
7 ms |
5460 KB |
Output is correct |
4 |
Correct |
7 ms |
5460 KB |
Output is correct |
5 |
Correct |
6 ms |
5460 KB |
Output is correct |
6 |
Correct |
6 ms |
5512 KB |
Output is correct |
7 |
Correct |
97 ms |
11384 KB |
Output is correct |
8 |
Correct |
2 ms |
5460 KB |
Output is correct |
9 |
Correct |
6 ms |
5460 KB |
Output is correct |
10 |
Correct |
8 ms |
5536 KB |
Output is correct |
11 |
Correct |
6 ms |
5432 KB |
Output is correct |
12 |
Correct |
11 ms |
5716 KB |
Output is correct |
13 |
Correct |
8 ms |
5716 KB |
Output is correct |
14 |
Correct |
4 ms |
5716 KB |
Output is correct |
15 |
Correct |
10 ms |
5460 KB |
Output is correct |
16 |
Correct |
9 ms |
5564 KB |
Output is correct |
17 |
Correct |
7 ms |
5588 KB |
Output is correct |
18 |
Correct |
7 ms |
5588 KB |
Output is correct |
19 |
Correct |
6 ms |
5612 KB |
Output is correct |
20 |
Correct |
8 ms |
5588 KB |
Output is correct |
21 |
Correct |
7 ms |
5616 KB |
Output is correct |
22 |
Correct |
7 ms |
5588 KB |
Output is correct |
23 |
Correct |
6 ms |
5716 KB |
Output is correct |
24 |
Correct |
7 ms |
5716 KB |
Output is correct |
25 |
Correct |
8 ms |
5788 KB |
Output is correct |
26 |
Correct |
9 ms |
5788 KB |
Output is correct |
27 |
Correct |
8 ms |
5716 KB |
Output is correct |
28 |
Correct |
8 ms |
5812 KB |
Output is correct |
29 |
Correct |
8 ms |
5844 KB |
Output is correct |
30 |
Correct |
8 ms |
5844 KB |
Output is correct |
31 |
Correct |
8 ms |
5876 KB |
Output is correct |
32 |
Correct |
7 ms |
5872 KB |
Output is correct |
33 |
Correct |
12 ms |
7956 KB |
Output is correct |
34 |
Correct |
10 ms |
7964 KB |
Output is correct |
35 |
Correct |
24 ms |
7980 KB |
Output is correct |
36 |
Correct |
10 ms |
8276 KB |
Output is correct |
37 |
Correct |
10 ms |
8276 KB |
Output is correct |
38 |
Correct |
35 ms |
8276 KB |
Output is correct |
39 |
Correct |
13 ms |
8680 KB |
Output is correct |
40 |
Correct |
13 ms |
8660 KB |
Output is correct |
41 |
Correct |
50 ms |
8684 KB |
Output is correct |
42 |
Correct |
16 ms |
8916 KB |
Output is correct |
43 |
Correct |
15 ms |
9040 KB |
Output is correct |
44 |
Correct |
49 ms |
9044 KB |
Output is correct |
45 |
Correct |
14 ms |
9300 KB |
Output is correct |
46 |
Correct |
17 ms |
9300 KB |
Output is correct |
47 |
Correct |
72 ms |
9300 KB |
Output is correct |
48 |
Correct |
20 ms |
9684 KB |
Output is correct |
49 |
Correct |
16 ms |
9684 KB |
Output is correct |
50 |
Correct |
81 ms |
9748 KB |
Output is correct |
51 |
Correct |
20 ms |
10068 KB |
Output is correct |
52 |
Correct |
20 ms |
10192 KB |
Output is correct |
53 |
Correct |
99 ms |
10068 KB |
Output is correct |
54 |
Correct |
23 ms |
10452 KB |
Output is correct |
55 |
Correct |
22 ms |
10448 KB |
Output is correct |
56 |
Correct |
135 ms |
10464 KB |
Output is correct |
57 |
Correct |
27 ms |
10708 KB |
Output is correct |
58 |
Correct |
20 ms |
10816 KB |
Output is correct |
59 |
Correct |
129 ms |
10828 KB |
Output is correct |
60 |
Correct |
25 ms |
11084 KB |
Output is correct |
61 |
Correct |
24 ms |
11092 KB |
Output is correct |
62 |
Correct |
148 ms |
11156 KB |
Output is correct |
63 |
Correct |
132 ms |
11168 KB |
Output is correct |
64 |
Correct |
202 ms |
11176 KB |
Output is correct |
65 |
Correct |
188 ms |
11092 KB |
Output is correct |
66 |
Correct |
139 ms |
11160 KB |
Output is correct |
67 |
Correct |
39 ms |
11156 KB |
Output is correct |
68 |
Correct |
73 ms |
11208 KB |
Output is correct |
69 |
Correct |
63 ms |
11224 KB |
Output is correct |
70 |
Correct |
41 ms |
11132 KB |
Output is correct |
71 |
Correct |
41 ms |
11220 KB |
Output is correct |
72 |
Correct |
35 ms |
11216 KB |
Output is correct |
73 |
Correct |
52 ms |
11828 KB |
Output is correct |
74 |
Correct |
81 ms |
11676 KB |
Output is correct |
75 |
Correct |
80 ms |
11592 KB |
Output is correct |
76 |
Correct |
71 ms |
11716 KB |
Output is correct |
77 |
Correct |
74 ms |
11728 KB |
Output is correct |
78 |
Correct |
33 ms |
11596 KB |
Output is correct |
79 |
Correct |
69 ms |
11560 KB |
Output is correct |
80 |
Correct |
73 ms |
11572 KB |
Output is correct |
81 |
Correct |
82 ms |
11644 KB |
Output is correct |
82 |
Correct |
64 ms |
11604 KB |
Output is correct |
83 |
Correct |
92 ms |
11580 KB |
Output is correct |
84 |
Correct |
91 ms |
11580 KB |
Output is correct |
85 |
Correct |
110 ms |
11588 KB |
Output is correct |
86 |
Correct |
111 ms |
11588 KB |
Output is correct |
87 |
Correct |
82 ms |
11584 KB |
Output is correct |
88 |
Correct |
94 ms |
11476 KB |
Output is correct |
89 |
Correct |
113 ms |
11492 KB |
Output is correct |
90 |
Correct |
103 ms |
11412 KB |
Output is correct |
91 |
Correct |
96 ms |
11592 KB |
Output is correct |
92 |
Correct |
91 ms |
11484 KB |
Output is correct |