Submission #896733

#TimeUsernameProblemLanguageResultExecution timeMemory
896733LCJLYMecho (IOI09_mecho)C++14
22 / 100
238 ms21504 KiB
#include <bits/stdc++.h> using namespace std; #define int long long #define ld long double #define show(x,y) cout << y << " " << #x << endl; #define show2(x,y,i,j) cout << y << " " << #x << " " << j << " " << #i << endl; #define show3(x,y,i,j,p,q) cout << y << " " << #x << " " << j << " " << #i << " " << q << " " << #p << endl; #define show4(x,y) for(auto it:x) cout << it << " "; cout << #y << endl; typedef pair<int,int>pii; typedef pair<pii,pii>pi2; void solve(){ int n,k; cin >> n >> k; string arr[n]; for(int x=0;x<n;x++){ cin >> arr[x]; } int l=0; int r=n; int best=-1; int mid; pii start; pii done; int dist[n+5][n+5]; memset(dist,-1,sizeof(dist)); queue<pii>q; for(int x=0;x<n;x++){ for(int y=0;y<n;y++){ if(arr[x][y]=='H'){ q.push({x,y}); dist[x][y]=0; } else if(arr[x][y]=='M') start={x,y}; else if(arr[x][y]=='D') done={x,y}; } } pii dir[4]={ {0,1}, {0,-1}, {1,0}, {-1,0}, }; while(!q.empty()){ pii cur=q.front(); q.pop(); for(auto it:dir){ int newx=cur.first+it.first; int newy=cur.second+it.second; if(newx<0||newy<0||newx>=n||newy>=n) continue; if(dist[newx][newy]!=-1) continue; dist[newx][newy]=dist[cur.first][cur.second]+1; q.push({newx,newy}); } } while(l<=r){ mid=(l+r)/2; int dist2[n][n]; memset(dist2,-1,sizeof(dist2)); dist2[start.first][start.second]=k*mid; q.push({start.first,start.second}); bool visited[n+5][n+5]; memset(visited,0,sizeof(visited)); for(int x=0;x<n;x++){ vector<pii>storage; int target=(x+1+mid)*k; while(!q.empty()){ pii cur=q.front(); q.pop(); if((dist2[cur.first][cur.second]+k-1)/k==x+mid+1&&x+mid+1<dist[cur.first][cur.second]){ storage.push_back({cur.first,cur.second}); visited[cur.first][cur.second]=1; } if(dist2[cur.first][cur.second]>=target) continue; for(auto it:dir){ int newx=cur.first+it.first; int newy=cur.second+it.second; if(newx<0||newy<0||newx>=n||newy>=n) continue; if(dist2[newx][newy]!=-1) continue; if(arr[newx][newy]=='T') continue; dist2[newx][newy]=dist2[cur.first][cur.second]+1; q.push({newx,newy}); } } for(auto it:storage) q.push(it); } if(visited[done.first][done.second]){ best=mid; l=mid+1; } else r=mid-1; } cout << best; } int32_t main(){ ios::sync_with_stdio(0); cin.tie(0); //freopen("out.txt", "w", stdout); int t=1; //cin >> t; while(t--){ solve(); } }
#Verdict Execution timeMemoryGrader output
Fetching results...