Submission #335753

#TimeUsernameProblemLanguageResultExecution timeMemory
335753Christopher_RdzMecho (IOI09_mecho)C++14
100 / 100
256 ms9196 KiB
#include <bits/stdc++.h> using namespace std; struct node{ int i; int j; node(){} node(int I,int J){ i=I; j=J; } }; queue<node> Q; char forest[805][805]; int bees[805][805]; int dist[805][805]; int energy[805][805]; int dx[4]={1,-1,0,0}; int dy[4]={0,0,1,-1}; int n,s; void bfsBees(){ for (int i = 1; i <= n; i++){ for (int j = 1; j <= n; j++){ bees[i][j]=INT_MAX; if(forest[i][j]=='H'){ Q.push(node(i,j)); bees[i][j]=0; } } } while(!Q.empty()){ node u=Q.front(); Q.pop(); for(int mov=0;mov<4;mov++){ int i=u.i+dx[mov]; int j=u.j+dy[mov]; if(i<1 || i>n) continue; if(j<1 || j>n) continue; if(forest[i][j]=='T' || forest[i][j]=='D') continue; if(bees[i][j]==INT_MAX){ bees[i][j]=bees[u.i][u.j]+1; Q.push(node(i,j)); } } } } node mecho; node home; bool bfsMecho(int init){ for (int i = 1; i <= n; i++){ for (int j = 1; j <= n; j++){ dist[i][j]=INT_MAX; energy[i][j]=0; }} if(bees[mecho.i][mecho.j]<=init) return false; dist[mecho.i][mecho.j]=init; energy[mecho.i][mecho.j]=0; Q.push(mecho); while(!Q.empty()){ node u=Q.front(); Q.pop(); if(bees[u.i][u.j]==dist[u.i][u.j] && energy[u.i][u.j]==0) continue; for(int mov=0;mov<4;mov++){ int i=u.i+dx[mov]; int j=u.j+dy[mov]; if(i<1 || i>n) continue; if(j<1 || j>n) continue; if(forest[i][j]=='T') continue; if(dist[i][j]==INT_MAX){ int change=(energy[u.i][u.j]>0)? 0 : 1; dist[i][j]=dist[u.i][u.j]+change; if(bees[i][j]<dist[i][j]) continue; energy[i][j]=(energy[u.i][u.j]>0)? energy[u.i][u.j]-1 : s-1; Q.push(node(i,j)); } } } return dist[home.i][home.j]!=INT_MAX; } int binarySearch(){ int k=-1; int L=0,R=3*n; while(L<R){ int mid=(L+R)/2; if(bfsMecho(mid)){ k=mid; L=mid+1; } else{ R=mid; } } return k; } int binarySearch2(){ int k=-1; for(int b=5000;b>=1;b/=2){ while(bfsMecho(b+k)) k+=b; } return k; } int main() { ios_base::sync_with_stdio(0);cin.tie(0); cin>>n>>s; for(int i=1;i<=n;i++){ for(int j=1;j<=n;j++){ cin>>forest[i][j]; if(forest[i][j]=='M'){ mecho=node(i,j); } else if(forest[i][j]=='D'){ home=node(i,j); } } } bfsBees(); int ans=binarySearch2(); cout<<ans<<endl; return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...