#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});
}
}
auto chk = [&](int t) -> bool{
if(dist_bee[st.ff][st.ss] <= t) return 0;
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] || (p.rem == 1 && 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 = 1e7;
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 |
Incorrect |
1 ms |
844 KB |
Output isn't correct |
3 |
Correct |
1 ms |
844 KB |
Output is correct |
4 |
Incorrect |
1 ms |
972 KB |
Output isn't correct |
5 |
Correct |
1 ms |
972 KB |
Output is correct |
6 |
Incorrect |
1 ms |
972 KB |
Output isn't correct |
7 |
Incorrect |
103 ms |
4176 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 |
Correct |
1 ms |
972 KB |
Output is correct |
12 |
Incorrect |
1 ms |
1100 KB |
Output isn't correct |
13 |
Correct |
1 ms |
1100 KB |
Output is correct |
14 |
Correct |
2 ms |
1100 KB |
Output is correct |
15 |
Correct |
1 ms |
972 KB |
Output is correct |
16 |
Correct |
1 ms |
972 KB |
Output is correct |
17 |
Correct |
1 ms |
972 KB |
Output is correct |
18 |
Correct |
1 ms |
972 KB |
Output is correct |
19 |
Correct |
1 ms |
972 KB |
Output is correct |
20 |
Correct |
1 ms |
972 KB |
Output is correct |
21 |
Correct |
1 ms |
1100 KB |
Output is correct |
22 |
Correct |
1 ms |
1100 KB |
Output is correct |
23 |
Correct |
1 ms |
1100 KB |
Output is correct |
24 |
Correct |
1 ms |
1100 KB |
Output is correct |
25 |
Correct |
1 ms |
1100 KB |
Output is correct |
26 |
Correct |
2 ms |
1100 KB |
Output is correct |
27 |
Correct |
1 ms |
1100 KB |
Output is correct |
28 |
Correct |
1 ms |
1100 KB |
Output is correct |
29 |
Correct |
1 ms |
1228 KB |
Output is correct |
30 |
Correct |
1 ms |
1228 KB |
Output is correct |
31 |
Correct |
1 ms |
1228 KB |
Output is correct |
32 |
Correct |
1 ms |
1236 KB |
Output is correct |
33 |
Correct |
6 ms |
2252 KB |
Output is correct |
34 |
Correct |
5 ms |
2252 KB |
Output is correct |
35 |
Correct |
27 ms |
2344 KB |
Output is correct |
36 |
Correct |
7 ms |
2508 KB |
Output is correct |
37 |
Correct |
6 ms |
2508 KB |
Output is correct |
38 |
Correct |
36 ms |
2528 KB |
Output is correct |
39 |
Correct |
9 ms |
2636 KB |
Output is correct |
40 |
Correct |
8 ms |
2676 KB |
Output is correct |
41 |
Correct |
47 ms |
2636 KB |
Output is correct |
42 |
Correct |
10 ms |
2892 KB |
Output is correct |
43 |
Correct |
10 ms |
2864 KB |
Output is correct |
44 |
Correct |
57 ms |
2928 KB |
Output is correct |
45 |
Correct |
13 ms |
3020 KB |
Output is correct |
46 |
Correct |
14 ms |
3020 KB |
Output is correct |
47 |
Correct |
67 ms |
3020 KB |
Output is correct |
48 |
Correct |
13 ms |
3288 KB |
Output is correct |
49 |
Correct |
13 ms |
3328 KB |
Output is correct |
50 |
Correct |
79 ms |
3324 KB |
Output is correct |
51 |
Correct |
15 ms |
3404 KB |
Output is correct |
52 |
Correct |
16 ms |
3404 KB |
Output is correct |
53 |
Correct |
102 ms |
3524 KB |
Output is correct |
54 |
Correct |
18 ms |
3616 KB |
Output is correct |
55 |
Correct |
20 ms |
3712 KB |
Output is correct |
56 |
Correct |
117 ms |
3696 KB |
Output is correct |
57 |
Correct |
21 ms |
3788 KB |
Output is correct |
58 |
Correct |
22 ms |
3868 KB |
Output is correct |
59 |
Correct |
139 ms |
3900 KB |
Output is correct |
60 |
Correct |
21 ms |
4116 KB |
Output is correct |
61 |
Correct |
19 ms |
4064 KB |
Output is correct |
62 |
Correct |
159 ms |
4096 KB |
Output is correct |
63 |
Correct |
124 ms |
4036 KB |
Output is correct |
64 |
Correct |
181 ms |
4048 KB |
Output is correct |
65 |
Correct |
178 ms |
3980 KB |
Output is correct |
66 |
Correct |
157 ms |
4092 KB |
Output is correct |
67 |
Correct |
135 ms |
4092 KB |
Output is correct |
68 |
Correct |
61 ms |
4008 KB |
Output is correct |
69 |
Correct |
62 ms |
4104 KB |
Output is correct |
70 |
Correct |
44 ms |
4008 KB |
Output is correct |
71 |
Correct |
49 ms |
4008 KB |
Output is correct |
72 |
Correct |
36 ms |
4008 KB |
Output is correct |
73 |
Correct |
41 ms |
4252 KB |
Output is correct |
74 |
Correct |
68 ms |
4356 KB |
Output is correct |
75 |
Correct |
90 ms |
4296 KB |
Output is correct |
76 |
Correct |
73 ms |
4368 KB |
Output is correct |
77 |
Correct |
72 ms |
4252 KB |
Output is correct |
78 |
Correct |
93 ms |
4224 KB |
Output is correct |
79 |
Correct |
58 ms |
4240 KB |
Output is correct |
80 |
Correct |
83 ms |
4252 KB |
Output is correct |
81 |
Correct |
90 ms |
4336 KB |
Output is correct |
82 |
Correct |
80 ms |
4292 KB |
Output is correct |
83 |
Correct |
100 ms |
4176 KB |
Output is correct |
84 |
Correct |
84 ms |
4292 KB |
Output is correct |
85 |
Correct |
85 ms |
4300 KB |
Output is correct |
86 |
Correct |
95 ms |
4300 KB |
Output is correct |
87 |
Correct |
101 ms |
4308 KB |
Output is correct |
88 |
Correct |
100 ms |
4252 KB |
Output is correct |
89 |
Correct |
99 ms |
4268 KB |
Output is correct |
90 |
Correct |
103 ms |
4148 KB |
Output is correct |
91 |
Correct |
115 ms |
4164 KB |
Output is correct |
92 |
Correct |
107 ms |
4140 KB |
Output is correct |