#include<bits/stdc++.h>
using namespace std;
#define fast ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define ll long long int
const ll N = 800+5 , INF = 1e18 , MOD = 1e9+7;
ll sx,sy,ex,ey;
vector<ll> dx = {0,0,-1,1};
vector<ll> dy = {-1,1,0,0};
vector<vector<ll>> bee(N,vector<ll>(N,INF));
vector<vector<char>> grid(N,vector<char>(N));
ll n,s;
bool check(ll start){
vector<vector<double>> dist(n+5,vector<double>(n+5,INF));
dist[sx][sy] = start;
queue<pair<ll,ll>> q;
q.push(make_pair(sx,sy));
while(!q.empty()){
ll a = q.front().first;
ll b = q.front().second;
q.pop();
for(ll t = 0; t <= 3; t++){
ll x = a+dx[t];
ll y = b+dy[t];
if(x < 1 || x > n || y < 1 || y > n || grid[x][y] == 'T' || grid[x][y] == 'H') continue;
if(dist[x][y] != INF) continue;
double d = dist[a][b]+((double)1/(double)s);
if(bee[x][y] <= floor(d)) continue;
dist[x][y] = d;
q.push(make_pair(x,y));
}
}
return (dist[ex][ey] != INF);
}
void solve(){
cin >> n >> s;
queue<pair<ll,ll>> q;
for(ll i = 1; i <= n; i++){
for(ll j = 1; j <= n; j++){
cin >> grid[i][j];
if(grid[i][j] == 'M'){
sx = i;
sy = j;
}
else if(grid[i][j] == 'H'){
q.push(make_pair(i,j));
bee[i][j] = 0;
}
else if(grid[i][j] == 'D'){
ex = i;
ey = j;
}
}
}
while(!q.empty()){
ll a = q.front().first;
ll b = q.front().second;
q.pop();
for(ll t = 0; t <= 3; t++){
ll x = a+dx[t];
ll y = b+dy[t];
if(x < 1 || x > n || y < 1 || y > n || grid[x][y] == 'D' || grid[x][y] == 'T') continue;
if(bee[x][y] != INF) continue;
bee[x][y] = bee[a][b]+1;
q.push(make_pair(x,y));
}
}
ll low = 0 , high = bee[sx][sy]-1;
ll ans = INF;
while(low <= high){
ll mid = (low+high)/2;
if(check(mid)){
ans = mid;
low = mid+1;
}
else high = mid-1;
}
if(ans == INF) ans = -1;
cout << ans;
}
int main(){
fast;
ll tc = 1;
// cin >> tc;
while(tc--) solve();
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
5972 KB |
Output is correct |
2 |
Correct |
3 ms |
5972 KB |
Output is correct |
3 |
Correct |
3 ms |
5972 KB |
Output is correct |
4 |
Correct |
3 ms |
5972 KB |
Output is correct |
5 |
Correct |
3 ms |
5996 KB |
Output is correct |
6 |
Correct |
4 ms |
6052 KB |
Output is correct |
7 |
Correct |
137 ms |
11236 KB |
Output is correct |
8 |
Correct |
4 ms |
5972 KB |
Output is correct |
9 |
Correct |
4 ms |
5972 KB |
Output is correct |
10 |
Correct |
4 ms |
5972 KB |
Output is correct |
11 |
Correct |
3 ms |
5972 KB |
Output is correct |
12 |
Correct |
4 ms |
6100 KB |
Output is correct |
13 |
Correct |
3 ms |
6100 KB |
Output is correct |
14 |
Correct |
4 ms |
6124 KB |
Output is correct |
15 |
Correct |
4 ms |
5972 KB |
Output is correct |
16 |
Correct |
3 ms |
5972 KB |
Output is correct |
17 |
Correct |
3 ms |
5972 KB |
Output is correct |
18 |
Correct |
3 ms |
6100 KB |
Output is correct |
19 |
Correct |
3 ms |
5972 KB |
Output is correct |
20 |
Correct |
3 ms |
5972 KB |
Output is correct |
21 |
Correct |
3 ms |
6100 KB |
Output is correct |
22 |
Correct |
3 ms |
6100 KB |
Output is correct |
23 |
Correct |
4 ms |
6100 KB |
Output is correct |
24 |
Correct |
3 ms |
6100 KB |
Output is correct |
25 |
Correct |
3 ms |
6100 KB |
Output is correct |
26 |
Correct |
3 ms |
6100 KB |
Output is correct |
27 |
Correct |
4 ms |
6100 KB |
Output is correct |
28 |
Correct |
3 ms |
6100 KB |
Output is correct |
29 |
Correct |
4 ms |
6100 KB |
Output is correct |
30 |
Correct |
4 ms |
6100 KB |
Output is correct |
31 |
Correct |
4 ms |
6100 KB |
Output is correct |
32 |
Correct |
4 ms |
6100 KB |
Output is correct |
33 |
Correct |
11 ms |
7132 KB |
Output is correct |
34 |
Correct |
13 ms |
7128 KB |
Output is correct |
35 |
Correct |
26 ms |
7056 KB |
Output is correct |
36 |
Correct |
14 ms |
7428 KB |
Output is correct |
37 |
Correct |
15 ms |
7428 KB |
Output is correct |
38 |
Correct |
37 ms |
7392 KB |
Output is correct |
39 |
Correct |
18 ms |
7760 KB |
Output is correct |
40 |
Correct |
18 ms |
7764 KB |
Output is correct |
41 |
Correct |
47 ms |
7732 KB |
Output is correct |
42 |
Correct |
21 ms |
8132 KB |
Output is correct |
43 |
Correct |
22 ms |
8120 KB |
Output is correct |
44 |
Correct |
59 ms |
8152 KB |
Output is correct |
45 |
Correct |
27 ms |
8508 KB |
Output is correct |
46 |
Correct |
27 ms |
8460 KB |
Output is correct |
47 |
Correct |
68 ms |
8412 KB |
Output is correct |
48 |
Correct |
29 ms |
9064 KB |
Output is correct |
49 |
Correct |
31 ms |
9004 KB |
Output is correct |
50 |
Correct |
86 ms |
9072 KB |
Output is correct |
51 |
Correct |
36 ms |
9580 KB |
Output is correct |
52 |
Correct |
39 ms |
9544 KB |
Output is correct |
53 |
Correct |
110 ms |
9504 KB |
Output is correct |
54 |
Correct |
42 ms |
10068 KB |
Output is correct |
55 |
Correct |
42 ms |
10084 KB |
Output is correct |
56 |
Correct |
123 ms |
9992 KB |
Output is correct |
57 |
Correct |
51 ms |
10676 KB |
Output is correct |
58 |
Correct |
50 ms |
10572 KB |
Output is correct |
59 |
Correct |
142 ms |
10584 KB |
Output is correct |
60 |
Correct |
54 ms |
11312 KB |
Output is correct |
61 |
Correct |
56 ms |
11256 KB |
Output is correct |
62 |
Correct |
179 ms |
11188 KB |
Output is correct |
63 |
Correct |
148 ms |
11192 KB |
Output is correct |
64 |
Correct |
230 ms |
11220 KB |
Output is correct |
65 |
Correct |
230 ms |
11184 KB |
Output is correct |
66 |
Correct |
169 ms |
11192 KB |
Output is correct |
67 |
Correct |
161 ms |
11172 KB |
Output is correct |
68 |
Correct |
71 ms |
11228 KB |
Output is correct |
69 |
Correct |
65 ms |
11228 KB |
Output is correct |
70 |
Correct |
62 ms |
11276 KB |
Output is correct |
71 |
Correct |
57 ms |
11224 KB |
Output is correct |
72 |
Correct |
62 ms |
11264 KB |
Output is correct |
73 |
Correct |
56 ms |
11136 KB |
Output is correct |
74 |
Correct |
107 ms |
11236 KB |
Output is correct |
75 |
Correct |
88 ms |
11256 KB |
Output is correct |
76 |
Correct |
96 ms |
11360 KB |
Output is correct |
77 |
Correct |
95 ms |
11236 KB |
Output is correct |
78 |
Correct |
106 ms |
11200 KB |
Output is correct |
79 |
Correct |
88 ms |
11204 KB |
Output is correct |
80 |
Correct |
101 ms |
11220 KB |
Output is correct |
81 |
Correct |
91 ms |
11352 KB |
Output is correct |
82 |
Correct |
86 ms |
11248 KB |
Output is correct |
83 |
Correct |
116 ms |
11232 KB |
Output is correct |
84 |
Correct |
102 ms |
11224 KB |
Output is correct |
85 |
Correct |
106 ms |
11212 KB |
Output is correct |
86 |
Correct |
139 ms |
11296 KB |
Output is correct |
87 |
Correct |
107 ms |
11228 KB |
Output is correct |
88 |
Correct |
124 ms |
11208 KB |
Output is correct |
89 |
Correct |
125 ms |
11216 KB |
Output is correct |
90 |
Correct |
152 ms |
11188 KB |
Output is correct |
91 |
Correct |
128 ms |
11196 KB |
Output is correct |
92 |
Correct |
140 ms |
11188 KB |
Output is correct |