#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define rep(i,l,r) for(int i = (l); i < r; i++)
#define per(i,r,l) for(int i = (r); i >= l; i--)
#define all(x) x.begin(), x.end()
#define sz(x) (int)(x).size()
#define pb push_back
#define ff first
#define ss second
const ll mod = 998244353, maxn = 800 + 5, inf = 1e9 + 5;
struct node{
int x, y, rem, eq;
node(){}
node(int x, int y, int r, int e):
x(x), y(y), rem(r), eq(e){}
};
int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1};
bool bad[maxn][maxn], vis[maxn][maxn];
int dist_bee[maxn][maxn];
int main(){
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int n, s; cin >> n >> s;
pair<int, int> st, en;
deque<pair<int, int>> hol;
rep(i,0,n){
rep(j,0,n){
dist_bee[i][j] = inf;
char e; cin >> e;
if(e == 'M'){
st = {i, j};
}
if(e == 'D'){
en = {i, j};
}
if(e == 'H'){
hol.push_back({i, j});
dist_bee[i][j] = 0;
}
if(e == 'H' || e == 'T') bad[i][j] = 1;
}
}
while(sz(hol)){
pair<int, int> p = hol.front(); hol.pop_front();
rep(d,0,4){
int i = p.ff + dx[d], j = p.ss + dy[d];
if(i < 0 || i >= n || j < 0 || j >= n || dist_bee[i][j] != inf || bad[i][j]) continue;
dist_bee[i][j] = dist_bee[p.ff][p.ss] + 1;
hol.pb({i, j});
}
}
dist_bee[en.ff][en.ss] = inf;
// rep(i,0,n){
// rep(j,0,n){
// if(dist_bee[i][j] == inf) cout << -1 << ' ';
// else cout << dist_bee[i][j] << ' ';
// }
// cout << endl;
// }
auto chk = [&](int t) -> bool{
memset(vis, 0, sizeof vis);
deque<node> q;
q.push_back(node(st.ff, st.ss, s, t + 1));
vis[st.ff][st.ss] = 1;
while(sz(q)){
node p = q.front(); q.pop_front();
rep(d,0,4){
int i = p.x + dx[d], j = p.y + dy[d];
if(i < 0 || i >= n || j < 0 || j >= n || vis[i][j] || bad[i][j]) continue;
node r = node(i, j, p.rem - 1, p.eq);
if(!r.rem){
if(dist_bee[i][j] <= p.eq) continue;
r.rem = s, r.eq++;
}
vis[i][j] = 1;
q.push_back(r);
}
}
return vis[en.ff][en.ss];
};
int l = -1, r = 1e6;
while(l + 1 < r){
int mid = (l + r)/2;
if(chk(mid)) l = mid;
else r = mid;
}
cout << l;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
844 KB |
Output is correct |
2 |
Correct |
1 ms |
972 KB |
Output is correct |
3 |
Correct |
1 ms |
844 KB |
Output is correct |
4 |
Correct |
1 ms |
972 KB |
Output is correct |
5 |
Incorrect |
1 ms |
972 KB |
Output isn't correct |
6 |
Incorrect |
1 ms |
972 KB |
Output isn't correct |
7 |
Incorrect |
102 ms |
4684 KB |
Output isn't correct |
8 |
Correct |
1 ms |
972 KB |
Output is correct |
9 |
Correct |
1 ms |
972 KB |
Output is correct |
10 |
Correct |
1 ms |
972 KB |
Output is correct |
11 |
Incorrect |
1 ms |
972 KB |
Output isn't correct |
12 |
Incorrect |
2 ms |
1100 KB |
Output isn't correct |
13 |
Incorrect |
2 ms |
1100 KB |
Output isn't correct |
14 |
Incorrect |
2 ms |
1100 KB |
Output isn't correct |
15 |
Correct |
1 ms |
972 KB |
Output is correct |
16 |
Incorrect |
1 ms |
972 KB |
Output isn't correct |
17 |
Correct |
1 ms |
972 KB |
Output is correct |
18 |
Incorrect |
2 ms |
972 KB |
Output isn't correct |
19 |
Correct |
2 ms |
972 KB |
Output is correct |
20 |
Incorrect |
1 ms |
972 KB |
Output isn't correct |
21 |
Correct |
1 ms |
1100 KB |
Output is correct |
22 |
Incorrect |
1 ms |
1100 KB |
Output isn't correct |
23 |
Correct |
1 ms |
1100 KB |
Output is correct |
24 |
Incorrect |
1 ms |
1100 KB |
Output isn't correct |
25 |
Correct |
1 ms |
1088 KB |
Output is correct |
26 |
Incorrect |
2 ms |
1100 KB |
Output isn't correct |
27 |
Correct |
2 ms |
1100 KB |
Output is correct |
28 |
Incorrect |
2 ms |
1100 KB |
Output isn't correct |
29 |
Correct |
1 ms |
1228 KB |
Output is correct |
30 |
Incorrect |
2 ms |
1228 KB |
Output isn't correct |
31 |
Correct |
2 ms |
1228 KB |
Output is correct |
32 |
Incorrect |
2 ms |
1228 KB |
Output isn't correct |
33 |
Correct |
6 ms |
2456 KB |
Output is correct |
34 |
Incorrect |
7 ms |
2444 KB |
Output isn't correct |
35 |
Incorrect |
32 ms |
2384 KB |
Output isn't correct |
36 |
Correct |
7 ms |
2628 KB |
Output is correct |
37 |
Incorrect |
7 ms |
2636 KB |
Output isn't correct |
38 |
Incorrect |
44 ms |
2672 KB |
Output isn't correct |
39 |
Correct |
9 ms |
2804 KB |
Output is correct |
40 |
Incorrect |
9 ms |
2892 KB |
Output isn't correct |
41 |
Incorrect |
50 ms |
2908 KB |
Output isn't correct |
42 |
Correct |
11 ms |
3148 KB |
Output is correct |
43 |
Incorrect |
12 ms |
3056 KB |
Output isn't correct |
44 |
Incorrect |
56 ms |
3180 KB |
Output isn't correct |
45 |
Correct |
12 ms |
3348 KB |
Output is correct |
46 |
Incorrect |
13 ms |
3384 KB |
Output isn't correct |
47 |
Incorrect |
66 ms |
3428 KB |
Output isn't correct |
48 |
Correct |
14 ms |
3552 KB |
Output is correct |
49 |
Incorrect |
15 ms |
3652 KB |
Output isn't correct |
50 |
Incorrect |
90 ms |
3688 KB |
Output isn't correct |
51 |
Correct |
17 ms |
3820 KB |
Output is correct |
52 |
Incorrect |
17 ms |
3916 KB |
Output isn't correct |
53 |
Incorrect |
101 ms |
3936 KB |
Output isn't correct |
54 |
Correct |
19 ms |
4172 KB |
Output is correct |
55 |
Incorrect |
22 ms |
4064 KB |
Output isn't correct |
56 |
Incorrect |
110 ms |
4184 KB |
Output isn't correct |
57 |
Correct |
23 ms |
4420 KB |
Output is correct |
58 |
Incorrect |
20 ms |
4428 KB |
Output isn't correct |
59 |
Incorrect |
177 ms |
4372 KB |
Output isn't correct |
60 |
Correct |
22 ms |
4712 KB |
Output is correct |
61 |
Incorrect |
24 ms |
4680 KB |
Output isn't correct |
62 |
Incorrect |
154 ms |
4732 KB |
Output isn't correct |
63 |
Correct |
125 ms |
4680 KB |
Output is correct |
64 |
Correct |
182 ms |
4676 KB |
Output is correct |
65 |
Incorrect |
179 ms |
4724 KB |
Output isn't correct |
66 |
Correct |
148 ms |
4724 KB |
Output is correct |
67 |
Correct |
128 ms |
4624 KB |
Output is correct |
68 |
Incorrect |
55 ms |
4676 KB |
Output isn't correct |
69 |
Correct |
74 ms |
4752 KB |
Output is correct |
70 |
Incorrect |
43 ms |
4676 KB |
Output isn't correct |
71 |
Incorrect |
44 ms |
4632 KB |
Output isn't correct |
72 |
Incorrect |
46 ms |
4748 KB |
Output isn't correct |
73 |
Incorrect |
56 ms |
4892 KB |
Output isn't correct |
74 |
Incorrect |
125 ms |
4996 KB |
Output isn't correct |
75 |
Incorrect |
74 ms |
5088 KB |
Output isn't correct |
76 |
Incorrect |
68 ms |
4920 KB |
Output isn't correct |
77 |
Incorrect |
71 ms |
4996 KB |
Output isn't correct |
78 |
Incorrect |
88 ms |
4932 KB |
Output isn't correct |
79 |
Incorrect |
132 ms |
4972 KB |
Output isn't correct |
80 |
Incorrect |
70 ms |
4968 KB |
Output isn't correct |
81 |
Incorrect |
80 ms |
4932 KB |
Output isn't correct |
82 |
Incorrect |
68 ms |
4972 KB |
Output isn't correct |
83 |
Incorrect |
90 ms |
4924 KB |
Output isn't correct |
84 |
Incorrect |
141 ms |
4932 KB |
Output isn't correct |
85 |
Incorrect |
85 ms |
4936 KB |
Output isn't correct |
86 |
Incorrect |
87 ms |
4820 KB |
Output isn't correct |
87 |
Incorrect |
106 ms |
4932 KB |
Output isn't correct |
88 |
Incorrect |
104 ms |
4876 KB |
Output isn't correct |
89 |
Incorrect |
149 ms |
4780 KB |
Output isn't correct |
90 |
Incorrect |
96 ms |
4884 KB |
Output isn't correct |
91 |
Incorrect |
97 ms |
4880 KB |
Output isn't correct |
92 |
Incorrect |
86 ms |
4780 KB |
Output isn't correct |