Submission #870671

#TimeUsernameProblemLanguageResultExecution timeMemory
870671Essa2006Mecho (IOI09_mecho)C++14
19 / 100
238 ms9132 KiB
#include<bits/stdc++.h> using namespace std; #define ll long long #define endl '\n' #define FF first #define SS second #define all(a) a.begin(), a.end() #define mod (ll)(1000000007) const int inf=1e9; int n, s, Md, can; array<int, 2>Str, End; vector<string>A; vector<vector<int>>Pri; queue<array<int, 2>>q; queue<array<int, 4>>qu; int dx[]={0, 0, 1, -1}; int dy[]={1, -1, 0, 0}; void pre(){ A.clear(), Pri.clear(); A.resize(n), Pri.resize(n, vector<int>(n, inf)); } bool valid(int i, int j){ return !(i<0 || i>=n || j<0 || j>=n); } void bfs(){ while(!q.empty()){ array<int, 2>u=q.front(); q.pop(); int i=u[0], j=u[1]; for(int f=0;f<4;f++){ int ii=i+dx[f], jj=j+dy[f]; if(valid(ii, jj) && A[ii][jj]!='T' && Pri[i][j]+1<Pri[ii][jj]) Pri[ii][jj]=Pri[i][j]+1, q.push({ii, jj}); } } } void check(){ array<int, 2>a={inf, inf}; vector<vector<array<int, 2>>>Cur(n, vector<array<int, 2>>(n, a)); Cur[Str[0]][Str[1]]={Md, -s}; while(!qu.empty()){ array<int, 4>u=qu.front(); qu.pop(); int i=u[0], j=u[1], cur=u[2], moves=u[3]; if(cur>=Pri[i][j]) continue; if(i==End[0] && j==End[1]) can=1; for(int f=0;f<4;f++){ int ii=i+dx[f], jj=j+dy[f]; if(valid(ii, jj)){ array<int, 2>b; if(moves){ b={cur, -(moves-1)}; } else{ b={cur+1, -s}; } if(b<Cur[ii][jj]){ Cur[ii][jj]=b; qu.push({ii, jj, b[0], -b[1]}); } } } } } int main(){ ios_base::sync_with_stdio(0);cin.tie(0); cin>>n>>s; pre(); for(int i=0;i<n;i++){ cin>>A[i]; } for(int i=0;i<n;i++){ for(int j=0;j<n;j++){ if(A[i][j]=='H') q.push({i, j}), Pri[i][j]=0; else if(A[i][j]=='M') Str[0]=i, Str[1]=j; else if(A[i][j]=='D') End[0]=i, End[1]=j; else if(A[i][j]=='T') Pri[i][j]=0; } } bfs(); int l=0, r=n*n, ans=-1; while(l<=r){ int md=(l+r)/2; Md=md; can=0; qu.push({Str[0], Str[1], md, s}); check(); if(can) ans=md, l=md+1; else r=md-1; } cout<<ans; }
#Verdict Execution timeMemoryGrader output
Fetching results...