#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[2];
bool act = 0;
q[act].push_back(node(st.ff, st.ss, s, t + 1));
vis[st.ff][st.ss] = 1;
while(1){
if(!sz(q[act])){
act ^= 1;
}
if(!sz(q[act])) break;
node p = q[act].front(); q[act].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]
|| p.eq > dist_bee[i][j]) continue;
node r = node(i, j, p.rem - 1, p.eq);
vis[i][j] = 1;
if(!r.rem){
r.rem = s, r.eq++;
q[!act].pb(r);
}else{
q[act].pb(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 |
Incorrect |
1 ms |
844 KB |
Output isn't correct |
2 |
Incorrect |
1 ms |
844 KB |
Output isn't correct |
3 |
Incorrect |
2 ms |
844 KB |
Output isn't correct |
4 |
Incorrect |
1 ms |
972 KB |
Output isn't correct |
5 |
Correct |
1 ms |
972 KB |
Output is correct |
6 |
Correct |
1 ms |
972 KB |
Output is correct |
7 |
Correct |
110 ms |
4172 KB |
Output is correct |
8 |
Incorrect |
1 ms |
972 KB |
Output isn't correct |
9 |
Correct |
1 ms |
972 KB |
Output is correct |
10 |
Correct |
1 ms |
972 KB |
Output is correct |
11 |
Correct |
1 ms |
972 KB |
Output is correct |
12 |
Incorrect |
1 ms |
1100 KB |
Output isn't correct |
13 |
Incorrect |
2 ms |
1100 KB |
Output isn't correct |
14 |
Correct |
2 ms |
1100 KB |
Output is correct |
15 |
Incorrect |
1 ms |
972 KB |
Output isn't correct |
16 |
Correct |
1 ms |
972 KB |
Output is correct |
17 |
Incorrect |
1 ms |
972 KB |
Output isn't correct |
18 |
Correct |
2 ms |
984 KB |
Output is correct |
19 |
Incorrect |
1 ms |
972 KB |
Output isn't correct |
20 |
Correct |
1 ms |
972 KB |
Output is correct |
21 |
Incorrect |
2 ms |
1100 KB |
Output isn't correct |
22 |
Correct |
1 ms |
1100 KB |
Output is correct |
23 |
Incorrect |
1 ms |
1100 KB |
Output isn't correct |
24 |
Correct |
1 ms |
1100 KB |
Output is correct |
25 |
Incorrect |
1 ms |
1100 KB |
Output isn't correct |
26 |
Correct |
1 ms |
1100 KB |
Output is correct |
27 |
Incorrect |
2 ms |
1100 KB |
Output isn't correct |
28 |
Correct |
1 ms |
1100 KB |
Output is correct |
29 |
Incorrect |
1 ms |
1228 KB |
Output isn't correct |
30 |
Correct |
2 ms |
1228 KB |
Output is correct |
31 |
Incorrect |
2 ms |
1228 KB |
Output isn't correct |
32 |
Correct |
1 ms |
1228 KB |
Output is correct |
33 |
Incorrect |
6 ms |
2252 KB |
Output isn't correct |
34 |
Correct |
7 ms |
2252 KB |
Output is correct |
35 |
Correct |
29 ms |
2252 KB |
Output is correct |
36 |
Incorrect |
7 ms |
2508 KB |
Output isn't correct |
37 |
Correct |
7 ms |
2508 KB |
Output is correct |
38 |
Correct |
38 ms |
2508 KB |
Output is correct |
39 |
Incorrect |
11 ms |
2636 KB |
Output isn't correct |
40 |
Correct |
9 ms |
2636 KB |
Output is correct |
41 |
Correct |
46 ms |
2636 KB |
Output is correct |
42 |
Incorrect |
11 ms |
2892 KB |
Output isn't correct |
43 |
Correct |
13 ms |
2892 KB |
Output is correct |
44 |
Correct |
61 ms |
2936 KB |
Output is correct |
45 |
Incorrect |
11 ms |
3020 KB |
Output isn't correct |
46 |
Correct |
12 ms |
3132 KB |
Output is correct |
47 |
Correct |
72 ms |
3116 KB |
Output is correct |
48 |
Incorrect |
14 ms |
3324 KB |
Output isn't correct |
49 |
Correct |
15 ms |
3320 KB |
Output is correct |
50 |
Correct |
91 ms |
3308 KB |
Output is correct |
51 |
Incorrect |
16 ms |
3496 KB |
Output isn't correct |
52 |
Correct |
17 ms |
3520 KB |
Output is correct |
53 |
Correct |
104 ms |
3512 KB |
Output is correct |
54 |
Incorrect |
20 ms |
3676 KB |
Output isn't correct |
55 |
Correct |
22 ms |
3712 KB |
Output is correct |
56 |
Correct |
119 ms |
3660 KB |
Output is correct |
57 |
Incorrect |
20 ms |
3788 KB |
Output isn't correct |
58 |
Correct |
20 ms |
3940 KB |
Output is correct |
59 |
Correct |
138 ms |
3896 KB |
Output is correct |
60 |
Incorrect |
22 ms |
4116 KB |
Output isn't correct |
61 |
Correct |
22 ms |
4088 KB |
Output is correct |
62 |
Correct |
162 ms |
4336 KB |
Output is correct |
63 |
Correct |
123 ms |
4092 KB |
Output is correct |
64 |
Correct |
195 ms |
4036 KB |
Output is correct |
65 |
Correct |
175 ms |
4092 KB |
Output is correct |
66 |
Incorrect |
162 ms |
4036 KB |
Output isn't correct |
67 |
Correct |
123 ms |
3976 KB |
Output is correct |
68 |
Correct |
58 ms |
4036 KB |
Output is correct |
69 |
Correct |
55 ms |
4004 KB |
Output is correct |
70 |
Correct |
53 ms |
4036 KB |
Output is correct |
71 |
Correct |
44 ms |
4056 KB |
Output is correct |
72 |
Incorrect |
38 ms |
4036 KB |
Output isn't correct |
73 |
Incorrect |
39 ms |
4256 KB |
Output isn't correct |
74 |
Correct |
75 ms |
4388 KB |
Output is correct |
75 |
Correct |
84 ms |
4364 KB |
Output is correct |
76 |
Correct |
78 ms |
4372 KB |
Output is correct |
77 |
Correct |
80 ms |
4368 KB |
Output is correct |
78 |
Correct |
92 ms |
4300 KB |
Output is correct |
79 |
Correct |
70 ms |
4420 KB |
Output is correct |
80 |
Correct |
79 ms |
4232 KB |
Output is correct |
81 |
Correct |
89 ms |
4344 KB |
Output is correct |
82 |
Correct |
65 ms |
4220 KB |
Output is correct |
83 |
Correct |
102 ms |
4304 KB |
Output is correct |
84 |
Correct |
90 ms |
4300 KB |
Output is correct |
85 |
Correct |
85 ms |
4300 KB |
Output is correct |
86 |
Correct |
99 ms |
4424 KB |
Output is correct |
87 |
Correct |
91 ms |
4208 KB |
Output is correct |
88 |
Correct |
99 ms |
4252 KB |
Output is correct |
89 |
Correct |
101 ms |
4248 KB |
Output is correct |
90 |
Correct |
106 ms |
4244 KB |
Output is correct |
91 |
Correct |
104 ms |
4248 KB |
Output is correct |
92 |
Correct |
103 ms |
4164 KB |
Output is correct |