#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]) 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();
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 dist[en.ff][en.ss] != inf;
};
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 |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
0 ms |
332 KB |
Output is correct |
7 |
Correct |
130 ms |
11212 KB |
Output is correct |
8 |
Correct |
1 ms |
460 KB |
Output is correct |
9 |
Correct |
0 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 |
Incorrect |
1 ms |
844 KB |
Output isn't correct |
13 |
Correct |
1 ms |
844 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 |
0 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 |
716 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 |
37 ms |
5016 KB |
Output is correct |
36 |
Correct |
11 ms |
5604 KB |
Output is correct |
37 |
Correct |
8 ms |
5600 KB |
Output is correct |
38 |
Correct |
41 ms |
5684 KB |
Output is correct |
39 |
Correct |
10 ms |
6348 KB |
Output is correct |
40 |
Correct |
11 ms |
6332 KB |
Output is correct |
41 |
Correct |
50 ms |
6348 KB |
Output is correct |
42 |
Correct |
13 ms |
7008 KB |
Output is correct |
43 |
Correct |
13 ms |
6948 KB |
Output is correct |
44 |
Correct |
61 ms |
6988 KB |
Output is correct |
45 |
Correct |
20 ms |
7628 KB |
Output is correct |
46 |
Correct |
20 ms |
7696 KB |
Output is correct |
47 |
Correct |
84 ms |
7684 KB |
Output is correct |
48 |
Correct |
17 ms |
8352 KB |
Output is correct |
49 |
Correct |
19 ms |
8328 KB |
Output is correct |
50 |
Correct |
92 ms |
8268 KB |
Output is correct |
51 |
Correct |
19 ms |
9036 KB |
Output is correct |
52 |
Correct |
23 ms |
9016 KB |
Output is correct |
53 |
Correct |
119 ms |
9028 KB |
Output is correct |
54 |
Correct |
25 ms |
9700 KB |
Output is correct |
55 |
Correct |
26 ms |
9700 KB |
Output is correct |
56 |
Correct |
125 ms |
9668 KB |
Output is correct |
57 |
Correct |
27 ms |
10264 KB |
Output is correct |
58 |
Correct |
28 ms |
10316 KB |
Output is correct |
59 |
Correct |
161 ms |
10372 KB |
Output is correct |
60 |
Correct |
27 ms |
10948 KB |
Output is correct |
61 |
Correct |
28 ms |
10984 KB |
Output is correct |
62 |
Correct |
186 ms |
11108 KB |
Output is correct |
63 |
Correct |
135 ms |
10976 KB |
Output is correct |
64 |
Correct |
260 ms |
10948 KB |
Output is correct |
65 |
Correct |
222 ms |
10952 KB |
Output is correct |
66 |
Correct |
181 ms |
11000 KB |
Output is correct |
67 |
Correct |
161 ms |
11028 KB |
Output is correct |
68 |
Correct |
88 ms |
11084 KB |
Output is correct |
69 |
Correct |
76 ms |
10984 KB |
Output is correct |
70 |
Correct |
58 ms |
11040 KB |
Output is correct |
71 |
Correct |
60 ms |
11088 KB |
Output is correct |
72 |
Correct |
45 ms |
11076 KB |
Output is correct |
73 |
Correct |
47 ms |
11480 KB |
Output is correct |
74 |
Correct |
85 ms |
11488 KB |
Output is correct |
75 |
Correct |
100 ms |
11540 KB |
Output is correct |
76 |
Correct |
90 ms |
11460 KB |
Output is correct |
77 |
Correct |
91 ms |
11504 KB |
Output is correct |
78 |
Correct |
104 ms |
11520 KB |
Output is correct |
79 |
Correct |
74 ms |
11508 KB |
Output is correct |
80 |
Correct |
88 ms |
11528 KB |
Output is correct |
81 |
Correct |
100 ms |
11540 KB |
Output is correct |
82 |
Correct |
92 ms |
11460 KB |
Output is correct |
83 |
Correct |
105 ms |
11444 KB |
Output is correct |
84 |
Correct |
102 ms |
11340 KB |
Output is correct |
85 |
Correct |
94 ms |
11460 KB |
Output is correct |
86 |
Correct |
101 ms |
11340 KB |
Output is correct |
87 |
Correct |
106 ms |
11332 KB |
Output is correct |
88 |
Correct |
123 ms |
11340 KB |
Output is correct |
89 |
Correct |
135 ms |
11468 KB |
Output is correct |
90 |
Correct |
127 ms |
11332 KB |
Output is correct |
91 |
Correct |
118 ms |
11332 KB |
Output is correct |
92 |
Correct |
130 ms |
11352 KB |
Output is correct |