# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
492832 |
2021-12-09T07:05:52 Z |
niloyroot |
Mecho (IOI09_mecho) |
C++14 |
|
1000 ms |
6628 KB |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<ll>;
using pl = pair<ll,ll>;
#define pb push_back
#define form(m,it) for(auto it=m.begin(); it!=m.end(); it++)
#define forp(i,a,b) for(ll i=a; i<=b; i++)
#define forn(i,a,b) for(ll i=a; i>=b; i--)
#define newl '\n'
#define ff first
#define ss second
const ll mod = 1000000007;
void solve(){
ll n,s; cin>>n>>s;
char c[n+1][n+1];
vector<pl> hives;
ll dis[n+1][n+1]; forp(i,1,n){forp(j,1,n){dis[i][j]=INT_MAX;}}
pl start,end;
forp(i,1,n){
forp(j,1,n){
cin>>c[i][j];
if(c[i][j]=='H'){
hives.pb({i,j});
dis[i][j]=0;
}
if(c[i][j]=='T'){
dis[i][j]=0;
}
if(c[i][j]=='M'){
start.ff=i; start.ss=j;
}
if(c[i][j]=='D'){
end.ff=i; end.ss=j;
}
}
}
bool vis[n+2][n+2];
queue<pl> q;
ll i,j;
for(auto p:hives){
memset(vis,0,sizeof(vis));
forp(i,0,n+1){vis[i][0]=1; vis[i][n+1]=1; vis[0][i]=1; vis[n+1][i]=1;}
q.push(p);
vis[p.ff][p.ss]=1;
while(!q.empty()){
i=q.front().ff; j=q.front().ss;
q.pop();
if(!vis[i][j+1] && (c[i][j+1]!='T' && c[i][j+1]!='H' && c[i][j+1]!='D')){
vis[i][j+1]=1;
dis[i][j+1]=min(dis[i][j+1],dis[i][j]+1);
q.push({i,j+1});
}
if(!vis[i+1][j] && (c[i+1][j]!='T' && c[i+1][j]!='H' && c[i+1][j]!='D')){
vis[i+1][j]=1;
dis[i+1][j]=min(dis[i+1][j],dis[i][j]+1);
q.push({i+1,j});
}
if(!vis[i][j-1] && (c[i][j-1]!='T' && c[i][j-1]!='H' && c[i][j-1]!='D')){
vis[i][j-1]=1;
dis[i][j-1]=min(dis[i][j-1],dis[i][j]+1);
q.push({i,j-1});
}
if(!vis[i-1][j] && (c[i-1][j]!='T' && c[i-1][j]!='H' && c[i-1][j]!='D')){
vis[i-1][j]=1;
dis[i-1][j]=min(dis[i-1][j],dis[i][j]+1);
q.push({i-1,j});
}
}
}
ll l=0,r=dis[start.ff][start.ss]-1,mid,len;
queue<pair<pl,ll>> qq;
while(l<r){
mid=(l+r+1)/2;
memset(vis,0,sizeof(vis));
forp(i,0,n+1){vis[i][0]=1; vis[i][n+1]=1; vis[0][i]=1; vis[n+1][i]=1;}
qq.push({start,0});
vis[start.ff][start.ss]=1;
while(!qq.empty()){
i=qq.front().ff.ff; j=qq.front().ff.ss; len=qq.front().ss;
qq.pop();
if(!vis[i][j+1] && dis[i][j+1]>mid + (len+1)/s){
vis[i][j+1]=1;
qq.push({{i,j+1},len+1});
}
if(!vis[i+1][j] && dis[i+1][j]>mid + (len+1)/s){
vis[i+1][j]=1;
qq.push({{i+1,j},len+1});
}
if(!vis[i][j-1] && dis[i][j-1]>mid + (len+1)/s){
vis[i][j-1]=1;
qq.push({{i,j-1},len+1});
}
if(!vis[i-1][j] && dis[i-1][j]>mid + (len+1)/s){
vis[i-1][j]=1;
qq.push({{i-1,j},len+1});
}
}
if(vis[end.ff][end.ss]){
l=mid;
} else {
r=mid-1;
}
}
mid=0;
memset(vis,0,sizeof(vis));
forp(i,0,n+1){vis[i][0]=1; vis[i][n+1]=1; vis[0][i]=1; vis[n+1][i]=1;}
qq.push({start,0});
vis[start.ff][start.ss]=1;
while(!qq.empty()){
i=qq.front().ff.ff; j=qq.front().ff.ss; len=qq.front().ss;
qq.pop();
if(!vis[i][j+1] && dis[i][j+1]>mid + (len+1)/s){
vis[i][j+1]=1;
qq.push({{i,j+1},len+1});
}
if(!vis[i+1][j] && dis[i+1][j]>mid + (len+1)/s){
vis[i+1][j]=1;
qq.push({{i+1,j},len+1});
}
if(!vis[i][j-1] && dis[i][j-1]>mid + (len+1)/s){
vis[i][j-1]=1;
qq.push({{i,j-1},len+1});
}
if(!vis[i-1][j] && dis[i-1][j]>mid + (len+1)/s){
vis[i-1][j]=1;
qq.push({{i-1,j},len+1});
}
}
if(vis[end.ff][end.ss]){
cout<<l<<newl;
} else {
cout<<-1<<newl;
}
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t=1; //cin>>t;
while(t--)solve();
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Execution timed out |
1089 ms |
6604 KB |
Time limit exceeded |
8 |
Correct |
0 ms |
204 KB |
Output is correct |
9 |
Correct |
0 ms |
204 KB |
Output is correct |
10 |
Correct |
0 ms |
204 KB |
Output is correct |
11 |
Correct |
0 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
8 ms |
344 KB |
Output is correct |
14 |
Correct |
9 ms |
352 KB |
Output is correct |
15 |
Correct |
0 ms |
204 KB |
Output is correct |
16 |
Correct |
0 ms |
204 KB |
Output is correct |
17 |
Correct |
0 ms |
204 KB |
Output is correct |
18 |
Correct |
0 ms |
204 KB |
Output is correct |
19 |
Correct |
0 ms |
204 KB |
Output is correct |
20 |
Correct |
0 ms |
204 KB |
Output is correct |
21 |
Correct |
1 ms |
204 KB |
Output is correct |
22 |
Correct |
1 ms |
204 KB |
Output is correct |
23 |
Correct |
1 ms |
332 KB |
Output is correct |
24 |
Correct |
1 ms |
332 KB |
Output is correct |
25 |
Correct |
1 ms |
332 KB |
Output is correct |
26 |
Correct |
1 ms |
332 KB |
Output is correct |
27 |
Correct |
0 ms |
332 KB |
Output is correct |
28 |
Correct |
1 ms |
332 KB |
Output is correct |
29 |
Correct |
1 ms |
332 KB |
Output is correct |
30 |
Correct |
1 ms |
332 KB |
Output is correct |
31 |
Correct |
1 ms |
332 KB |
Output is correct |
32 |
Correct |
1 ms |
332 KB |
Output is correct |
33 |
Correct |
5 ms |
1484 KB |
Output is correct |
34 |
Correct |
7 ms |
1484 KB |
Output is correct |
35 |
Correct |
19 ms |
1520 KB |
Output is correct |
36 |
Correct |
7 ms |
1868 KB |
Output is correct |
37 |
Correct |
9 ms |
1868 KB |
Output is correct |
38 |
Correct |
23 ms |
1900 KB |
Output is correct |
39 |
Correct |
8 ms |
2300 KB |
Output is correct |
40 |
Correct |
12 ms |
2300 KB |
Output is correct |
41 |
Correct |
31 ms |
2304 KB |
Output is correct |
42 |
Correct |
9 ms |
2764 KB |
Output is correct |
43 |
Correct |
14 ms |
2764 KB |
Output is correct |
44 |
Correct |
38 ms |
2764 KB |
Output is correct |
45 |
Correct |
12 ms |
3232 KB |
Output is correct |
46 |
Correct |
16 ms |
3288 KB |
Output is correct |
47 |
Correct |
49 ms |
3292 KB |
Output is correct |
48 |
Correct |
17 ms |
3788 KB |
Output is correct |
49 |
Correct |
19 ms |
3844 KB |
Output is correct |
50 |
Correct |
55 ms |
3840 KB |
Output is correct |
51 |
Correct |
16 ms |
4428 KB |
Output is correct |
52 |
Correct |
27 ms |
4452 KB |
Output is correct |
53 |
Correct |
72 ms |
4468 KB |
Output is correct |
54 |
Correct |
19 ms |
5020 KB |
Output is correct |
55 |
Correct |
29 ms |
5068 KB |
Output is correct |
56 |
Correct |
87 ms |
5120 KB |
Output is correct |
57 |
Correct |
22 ms |
5708 KB |
Output is correct |
58 |
Correct |
32 ms |
5832 KB |
Output is correct |
59 |
Correct |
97 ms |
5956 KB |
Output is correct |
60 |
Correct |
24 ms |
6588 KB |
Output is correct |
61 |
Correct |
37 ms |
6468 KB |
Output is correct |
62 |
Correct |
118 ms |
6592 KB |
Output is correct |
63 |
Execution timed out |
1081 ms |
6476 KB |
Time limit exceeded |
64 |
Execution timed out |
1093 ms |
6476 KB |
Time limit exceeded |
65 |
Execution timed out |
1080 ms |
6588 KB |
Time limit exceeded |
66 |
Execution timed out |
1087 ms |
6604 KB |
Time limit exceeded |
67 |
Execution timed out |
1075 ms |
6492 KB |
Time limit exceeded |
68 |
Execution timed out |
1094 ms |
6592 KB |
Time limit exceeded |
69 |
Execution timed out |
1098 ms |
6476 KB |
Time limit exceeded |
70 |
Execution timed out |
1094 ms |
6584 KB |
Time limit exceeded |
71 |
Execution timed out |
1094 ms |
6596 KB |
Time limit exceeded |
72 |
Execution timed out |
1091 ms |
6588 KB |
Time limit exceeded |
73 |
Execution timed out |
1088 ms |
6604 KB |
Time limit exceeded |
74 |
Execution timed out |
1092 ms |
6604 KB |
Time limit exceeded |
75 |
Execution timed out |
1080 ms |
6604 KB |
Time limit exceeded |
76 |
Execution timed out |
1091 ms |
6604 KB |
Time limit exceeded |
77 |
Execution timed out |
1089 ms |
6604 KB |
Time limit exceeded |
78 |
Execution timed out |
1090 ms |
6604 KB |
Time limit exceeded |
79 |
Execution timed out |
1091 ms |
6604 KB |
Time limit exceeded |
80 |
Execution timed out |
1095 ms |
6604 KB |
Time limit exceeded |
81 |
Execution timed out |
1074 ms |
6604 KB |
Time limit exceeded |
82 |
Execution timed out |
1092 ms |
6604 KB |
Time limit exceeded |
83 |
Execution timed out |
1068 ms |
6604 KB |
Time limit exceeded |
84 |
Execution timed out |
1083 ms |
6604 KB |
Time limit exceeded |
85 |
Execution timed out |
1091 ms |
6604 KB |
Time limit exceeded |
86 |
Execution timed out |
1088 ms |
6604 KB |
Time limit exceeded |
87 |
Execution timed out |
1064 ms |
6604 KB |
Time limit exceeded |
88 |
Execution timed out |
1095 ms |
6628 KB |
Time limit exceeded |
89 |
Execution timed out |
1090 ms |
6612 KB |
Time limit exceeded |
90 |
Execution timed out |
1096 ms |
6604 KB |
Time limit exceeded |
91 |
Execution timed out |
1094 ms |
6604 KB |
Time limit exceeded |
92 |
Execution timed out |
1096 ms |
6612 KB |
Time limit exceeded |