#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 = 1e18 + 5;
int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1};
bool bad[maxn][maxn];
ll dist_bee[maxn][maxn], dist[maxn][maxn];
int main(){
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
ll n, s; cin >> n >> s;
pair<ll, ll> st, en;
deque<pair<ll, ll>> 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<ll, ll> p = hol.front(); hol.pop_front();
rep(d,0,4){
ll 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] || pair<ll, ll>{i, j} == en) continue;
dist_bee[i][j] = dist_bee[p.ff][p.ss] + s;
hol.pb({i, j});
}
}
dist_bee[en.ff][en.ss] = inf;
auto chk = [&](ll t) -> bool{
if(dist_bee[st.ff][st.ss] <= 1LL*t*s) return 0;
rep(i,0,n){
rep(j,0,n){
dist[i][j] = inf;
}
}
dist[st.ff][st.ss] = 1LL*t*s;
deque<pair<ll, ll>> q;
q.push_back(st);
while(sz(q)){
pair<ll, ll> p = q.front(); q.pop_front();
if(p == en) return 1;
rep(d,0,4){
ll i = p.ff + dx[d], j = p.ss + dy[d];
if(i < 0 || i >= n || j < 0 || j >= n || bad[i][j]
|| dist[i][j] != inf || (dist[p.ff][p.ss] + 1) >= dist_bee[i][j]) continue;
dist[i][j] = dist[p.ff][p.ss] + 1;
q.push_back({i, j});
}
}
return 0;
};
ll l = -1, r = 1e7;
while(l + 1 < r){
ll mid = (l + r)/2;
if(chk(mid)) l = mid;
else r = mid;
}
cout << l;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
3 |
Correct |
0 ms |
332 KB |
Output is correct |
4 |
Correct |
0 ms |
332 KB |
Output is correct |
5 |
Correct |
0 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
131 ms |
11160 KB |
Output is correct |
8 |
Correct |
0 ms |
460 KB |
Output is correct |
9 |
Correct |
1 ms |
460 KB |
Output is correct |
10 |
Correct |
1 ms |
460 KB |
Output is correct |
11 |
Correct |
1 ms |
460 KB |
Output is correct |
12 |
Correct |
1 ms |
844 KB |
Output is correct |
13 |
Correct |
1 ms |
716 KB |
Output is correct |
14 |
Correct |
1 ms |
844 KB |
Output is correct |
15 |
Correct |
1 ms |
460 KB |
Output is correct |
16 |
Correct |
1 ms |
460 KB |
Output is correct |
17 |
Correct |
1 ms |
460 KB |
Output is correct |
18 |
Correct |
1 ms |
460 KB |
Output is correct |
19 |
Correct |
1 ms |
588 KB |
Output is correct |
20 |
Correct |
1 ms |
588 KB |
Output is correct |
21 |
Correct |
1 ms |
588 KB |
Output is correct |
22 |
Correct |
1 ms |
716 KB |
Output is correct |
23 |
Correct |
1 ms |
716 KB |
Output is correct |
24 |
Correct |
1 ms |
716 KB |
Output is correct |
25 |
Correct |
1 ms |
844 KB |
Output is correct |
26 |
Correct |
1 ms |
844 KB |
Output is correct |
27 |
Correct |
1 ms |
972 KB |
Output is correct |
28 |
Correct |
1 ms |
972 KB |
Output is correct |
29 |
Correct |
1 ms |
972 KB |
Output is correct |
30 |
Correct |
1 ms |
972 KB |
Output is correct |
31 |
Correct |
1 ms |
972 KB |
Output is correct |
32 |
Correct |
1 ms |
972 KB |
Output is correct |
33 |
Correct |
7 ms |
4940 KB |
Output is correct |
34 |
Correct |
7 ms |
4940 KB |
Output is correct |
35 |
Correct |
28 ms |
4940 KB |
Output is correct |
36 |
Correct |
8 ms |
5580 KB |
Output is correct |
37 |
Correct |
8 ms |
5580 KB |
Output is correct |
38 |
Correct |
37 ms |
5580 KB |
Output is correct |
39 |
Correct |
10 ms |
6348 KB |
Output is correct |
40 |
Correct |
10 ms |
6328 KB |
Output is correct |
41 |
Correct |
47 ms |
6260 KB |
Output is correct |
42 |
Correct |
13 ms |
6988 KB |
Output is correct |
43 |
Correct |
12 ms |
6988 KB |
Output is correct |
44 |
Correct |
58 ms |
7076 KB |
Output is correct |
45 |
Correct |
17 ms |
7688 KB |
Output is correct |
46 |
Correct |
15 ms |
7628 KB |
Output is correct |
47 |
Correct |
66 ms |
7700 KB |
Output is correct |
48 |
Correct |
16 ms |
8268 KB |
Output is correct |
49 |
Correct |
17 ms |
8300 KB |
Output is correct |
50 |
Correct |
82 ms |
8364 KB |
Output is correct |
51 |
Correct |
18 ms |
9036 KB |
Output is correct |
52 |
Correct |
20 ms |
9032 KB |
Output is correct |
53 |
Correct |
110 ms |
9028 KB |
Output is correct |
54 |
Correct |
22 ms |
9588 KB |
Output is correct |
55 |
Correct |
24 ms |
9592 KB |
Output is correct |
56 |
Correct |
123 ms |
9656 KB |
Output is correct |
57 |
Correct |
25 ms |
10264 KB |
Output is correct |
58 |
Correct |
24 ms |
10296 KB |
Output is correct |
59 |
Correct |
143 ms |
10272 KB |
Output is correct |
60 |
Correct |
27 ms |
11076 KB |
Output is correct |
61 |
Correct |
26 ms |
11004 KB |
Output is correct |
62 |
Correct |
174 ms |
11124 KB |
Output is correct |
63 |
Correct |
147 ms |
10976 KB |
Output is correct |
64 |
Correct |
213 ms |
11000 KB |
Output is correct |
65 |
Correct |
209 ms |
10916 KB |
Output is correct |
66 |
Correct |
182 ms |
11076 KB |
Output is correct |
67 |
Correct |
151 ms |
10948 KB |
Output is correct |
68 |
Correct |
71 ms |
11176 KB |
Output is correct |
69 |
Correct |
75 ms |
11084 KB |
Output is correct |
70 |
Correct |
54 ms |
11076 KB |
Output is correct |
71 |
Correct |
60 ms |
11036 KB |
Output is correct |
72 |
Correct |
46 ms |
10996 KB |
Output is correct |
73 |
Correct |
47 ms |
11480 KB |
Output is correct |
74 |
Correct |
81 ms |
11512 KB |
Output is correct |
75 |
Correct |
95 ms |
11584 KB |
Output is correct |
76 |
Correct |
86 ms |
11548 KB |
Output is correct |
77 |
Correct |
86 ms |
11460 KB |
Output is correct |
78 |
Correct |
103 ms |
11512 KB |
Output is correct |
79 |
Correct |
67 ms |
11460 KB |
Output is correct |
80 |
Correct |
81 ms |
11460 KB |
Output is correct |
81 |
Correct |
98 ms |
11480 KB |
Output is correct |
82 |
Correct |
86 ms |
11468 KB |
Output is correct |
83 |
Correct |
113 ms |
11404 KB |
Output is correct |
84 |
Correct |
97 ms |
11444 KB |
Output is correct |
85 |
Correct |
93 ms |
11412 KB |
Output is correct |
86 |
Correct |
104 ms |
11460 KB |
Output is correct |
87 |
Correct |
106 ms |
11388 KB |
Output is correct |
88 |
Correct |
111 ms |
11344 KB |
Output is correct |
89 |
Correct |
120 ms |
11308 KB |
Output is correct |
90 |
Correct |
117 ms |
11316 KB |
Output is correct |
91 |
Correct |
128 ms |
11316 KB |
Output is correct |
92 |
Correct |
123 ms |
11332 KB |
Output is correct |