// Judges with GCC >= 12 only needs Ofast
// #pragma GCC optimize("O3,no-stack-protector,fast-math,unroll-loops,tree-vectorize")
// MLE optimization
// #pragma GCC optimize("conserve-stack")
// Old judges
// #pragma GCC target("sse4.2,popcnt,lzcnt,abm,mmx,fma,bmi,bmi2")
// New judges. Test with assert(__builtin_cpu_supports("avx2"));
// #pragma GCC target("avx2,popcnt,lzcnt,abm,bmi,bmi2,fma,tune=native")
// Atcoder
// #pragma GCC target("avx2,popcnt,lzcnt,abm,bmi,bmi2,fma")
/*
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> ordered_set;
- insert(x),erase(x)
- find_by_order(k): return iterator to the k-th smallest element
- order_of_key(x): the number of elements that are strictly smaller
*/
#include<bits/stdc++.h>
using namespace std;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
uniform_real_distribution<> pp(0.0,1.0);
#define int long long
#define ld long double
#define pii pair<int,int>
#define piii pair<int,pii>
#define mpp make_pair
#define fi first
#define se second
const long long inf=1e18;
const int mod=998244353;
const int maxn=805;
const int B=650;
const int maxs=655;
const int maxm=200005;
const int maxq=1000005;
const int maxl=25;
const int maxa=1000000;
const int root=3;
int power(int a,int n){
int res=1;
while(n){
if(n&1) res=res*a%mod;
a=a*a%mod;n>>=1;
}
return res;
}
const int iroot=power(3,mod-2);
const int base=101;
int sx,sy,tx,ty;
int n,S,d[maxn][maxn],dd[maxn][maxn];
char c[maxn][maxn];
int dx[]={0,0,1,-1},
dy[]={1,-1,0,0};
bool check(int k){
memset(dd,-1,sizeof(dd));
dd[sx][sy]=k*S;
queue<pii> q;
q.push({sx,sy});
//cout << "check " << k << '\n';
while(!q.empty()){
auto [x,y]=q.front();q.pop();
//cout << x << ' ' << y << ' ' << dd[x][y] << '\n';
if(x==tx && y==ty) return true;
if(dd[x][y]/S>=d[x][y]) continue;
for(int t=0;t<4;t++){
int xt=x+dx[t],yt=y+dy[t];
if(xt<=0 || yt<=0 || xt>n || yt>n || c[xt][yt]=='T' || dd[xt][yt]!=-1) continue;
dd[xt][yt]=dd[x][y]+1;
q.push({xt,yt});
}
}
return false;
}
void solve(){
cin >> n >> S;
memset(d,-1,sizeof(d));
queue<pii> q;
for(int i=1;i<=n;i++) for(int j=1;j<=n;j++){
cin >> c[i][j];
if(c[i][j]=='H'){
d[i][j]=0;
q.push({i,j});
}
else if(c[i][j]=='M') sx=i,sy=j;
else if(c[i][j]=='D') tx=i,ty=j;
}
while(!q.empty()){
auto [x,y]=q.front();q.pop();
//cout << x << ' ' << y << ' ' << d[x][y] << '\n';
for(int t=0;t<4;t++){
int xt=x+dx[t],yt=y+dy[t];
if(xt<=0 || yt<=0 || xt>n || yt>n || c[xt][yt]=='T' || d[xt][yt]!=-1) continue;
d[xt][yt]=d[x][y]+1;
q.push({xt,yt});
}
}
int l=0,r=n*n,res=-1;
while(l<=r){
int mid=(l+r)>>1;
if(check(mid)) res=mid,l=mid+1;
else r=mid-1;
}
cout << res << '\n';
}
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);cout.tie(NULL);
int test=1;//cin >> test;
while(test--) solve();
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
10844 KB |
Output is correct |
2 |
Correct |
2 ms |
11256 KB |
Output is correct |
3 |
Correct |
2 ms |
10844 KB |
Output is correct |
4 |
Correct |
2 ms |
10844 KB |
Output is correct |
5 |
Correct |
2 ms |
10844 KB |
Output is correct |
6 |
Correct |
2 ms |
10844 KB |
Output is correct |
7 |
Correct |
63 ms |
11488 KB |
Output is correct |
8 |
Correct |
2 ms |
10844 KB |
Output is correct |
9 |
Correct |
2 ms |
10840 KB |
Output is correct |
10 |
Correct |
3 ms |
10844 KB |
Output is correct |
11 |
Correct |
2 ms |
10844 KB |
Output is correct |
12 |
Incorrect |
3 ms |
11100 KB |
Output isn't correct |
13 |
Correct |
2 ms |
11100 KB |
Output is correct |
14 |
Correct |
2 ms |
11100 KB |
Output is correct |
15 |
Correct |
2 ms |
10844 KB |
Output is correct |
16 |
Correct |
2 ms |
10844 KB |
Output is correct |
17 |
Correct |
2 ms |
10844 KB |
Output is correct |
18 |
Correct |
2 ms |
10844 KB |
Output is correct |
19 |
Correct |
2 ms |
10844 KB |
Output is correct |
20 |
Correct |
2 ms |
11080 KB |
Output is correct |
21 |
Correct |
2 ms |
10980 KB |
Output is correct |
22 |
Correct |
3 ms |
10844 KB |
Output is correct |
23 |
Correct |
3 ms |
10960 KB |
Output is correct |
24 |
Correct |
3 ms |
11096 KB |
Output is correct |
25 |
Correct |
3 ms |
11100 KB |
Output is correct |
26 |
Correct |
3 ms |
11096 KB |
Output is correct |
27 |
Correct |
3 ms |
11100 KB |
Output is correct |
28 |
Correct |
3 ms |
11100 KB |
Output is correct |
29 |
Correct |
3 ms |
11128 KB |
Output is correct |
30 |
Correct |
3 ms |
10964 KB |
Output is correct |
31 |
Correct |
3 ms |
11096 KB |
Output is correct |
32 |
Correct |
3 ms |
10968 KB |
Output is correct |
33 |
Correct |
5 ms |
11140 KB |
Output is correct |
34 |
Correct |
5 ms |
11340 KB |
Output is correct |
35 |
Correct |
18 ms |
11332 KB |
Output is correct |
36 |
Correct |
7 ms |
11096 KB |
Output is correct |
37 |
Correct |
7 ms |
11348 KB |
Output is correct |
38 |
Correct |
18 ms |
11100 KB |
Output is correct |
39 |
Correct |
6 ms |
11156 KB |
Output is correct |
40 |
Correct |
7 ms |
11352 KB |
Output is correct |
41 |
Correct |
22 ms |
11412 KB |
Output is correct |
42 |
Correct |
7 ms |
11356 KB |
Output is correct |
43 |
Correct |
8 ms |
11368 KB |
Output is correct |
44 |
Correct |
28 ms |
11412 KB |
Output is correct |
45 |
Correct |
8 ms |
11356 KB |
Output is correct |
46 |
Correct |
8 ms |
11356 KB |
Output is correct |
47 |
Correct |
30 ms |
11476 KB |
Output is correct |
48 |
Correct |
9 ms |
11356 KB |
Output is correct |
49 |
Correct |
9 ms |
11356 KB |
Output is correct |
50 |
Correct |
37 ms |
11356 KB |
Output is correct |
51 |
Correct |
10 ms |
11356 KB |
Output is correct |
52 |
Correct |
10 ms |
11504 KB |
Output is correct |
53 |
Correct |
44 ms |
11580 KB |
Output is correct |
54 |
Correct |
11 ms |
11356 KB |
Output is correct |
55 |
Correct |
11 ms |
11576 KB |
Output is correct |
56 |
Correct |
51 ms |
11344 KB |
Output is correct |
57 |
Correct |
16 ms |
11476 KB |
Output is correct |
58 |
Correct |
12 ms |
11484 KB |
Output is correct |
59 |
Correct |
58 ms |
11612 KB |
Output is correct |
60 |
Correct |
14 ms |
11612 KB |
Output is correct |
61 |
Correct |
13 ms |
11612 KB |
Output is correct |
62 |
Correct |
68 ms |
11612 KB |
Output is correct |
63 |
Correct |
66 ms |
11604 KB |
Output is correct |
64 |
Correct |
131 ms |
11592 KB |
Output is correct |
65 |
Correct |
95 ms |
11600 KB |
Output is correct |
66 |
Correct |
76 ms |
11612 KB |
Output is correct |
67 |
Correct |
75 ms |
11636 KB |
Output is correct |
68 |
Correct |
41 ms |
11608 KB |
Output is correct |
69 |
Correct |
33 ms |
11604 KB |
Output is correct |
70 |
Correct |
26 ms |
11456 KB |
Output is correct |
71 |
Correct |
30 ms |
11728 KB |
Output is correct |
72 |
Correct |
22 ms |
11612 KB |
Output is correct |
73 |
Correct |
23 ms |
12124 KB |
Output is correct |
74 |
Correct |
46 ms |
12132 KB |
Output is correct |
75 |
Correct |
43 ms |
12116 KB |
Output is correct |
76 |
Correct |
45 ms |
12112 KB |
Output is correct |
77 |
Correct |
39 ms |
12124 KB |
Output is correct |
78 |
Correct |
47 ms |
11924 KB |
Output is correct |
79 |
Correct |
32 ms |
12120 KB |
Output is correct |
80 |
Correct |
37 ms |
11856 KB |
Output is correct |
81 |
Correct |
43 ms |
11868 KB |
Output is correct |
82 |
Correct |
37 ms |
12124 KB |
Output is correct |
83 |
Correct |
48 ms |
11860 KB |
Output is correct |
84 |
Correct |
43 ms |
11864 KB |
Output is correct |
85 |
Correct |
46 ms |
12060 KB |
Output is correct |
86 |
Correct |
46 ms |
12072 KB |
Output is correct |
87 |
Correct |
46 ms |
12076 KB |
Output is correct |
88 |
Correct |
48 ms |
11864 KB |
Output is correct |
89 |
Correct |
48 ms |
11752 KB |
Output is correct |
90 |
Correct |
50 ms |
11868 KB |
Output is correct |
91 |
Correct |
49 ms |
11856 KB |
Output is correct |
92 |
Correct |
52 ms |
11868 KB |
Output is correct |