Submission #476426

#TimeUsernameProblemLanguageResultExecution timeMemory
476426MorleyMecho (IOI09_mecho)C++14
18 / 100
383 ms65544 KiB
#include <bits/stdc++.h> using namespace std; using ll = long long; using vi = vector<int>; using vl = vector<long long>; using vb = vector<bool>; using vvi = vector<vector<int>>; using vvpi = vector<vector<pair<int,int>>>; using vvb = vector<vector<bool>>; using str = string; using pi= pair<int,int>; using pll = pair<ll,ll>; using vpi = vector<pi>; using ti = tuple<int,int,int>; using vti = vector<ti>; #define pb push_back #define rs resize #define mp make_pair #define FOR(x,y,z) for(int x=y; x<z; x++) #define F0R(x,z) for(int x=0; x<z; x++) #define all(a) a.begin() , a.end() ll mx = 1000000000000000000; int inf = 2000000000; int main() { ios::sync_with_stdio(false); cin.tie(0); int n,s; cin >> n >> s; vector<str> F(n); for(int i=0;i<n;i++) cin >> F[i]; vvi hd(n,vi(n,inf)); queue<ti> Q; for(int i=0;i<n;i++) for(int j=0;j<n;j++) if(F[i][j]=='H') Q.push({i,j,0}); while(!Q.empty()) { ti cur=Q.front(); Q.pop(); int i=get<0>(cur),j=get<1>(cur),d=get<2>(cur); //cout << i << j << d << "\n"; if(d>=hd[i][j]) continue; hd[i][j]=d; if(i>0 && hd[i-1][j]==inf && (F[i-1][j]=='G' || F[i-1][j]=='M')) Q.push({i-1,j,d+1}); if(i<n-1 && hd[i+1][j]==inf && (F[i+1][j]=='G' || F[i+1][j]=='M')) Q.push({i+1,j,d+1}); if(j>0 && hd[i][j-1]==inf && (F[i][j-1]=='G' || F[i][j-1]=='M')) Q.push({i,j-1,d+1}); if(j<n-1 && hd[i][j+1]==inf && (F[i][j+1]=='G' || F[i][j+1]=='M')) Q.push({i,j+1,d+1}); } //for(int i=0;i<n;i++) {for(int j=0;j<n;j++) if(hd[i][j]<inf) cout << hd[i][j]; else cout << "T"; cout << "\n";} int lob=0,upb=2*n; while(lob<upb) { int tst=(lob+upb+1)/2; queue<ti> Qp; for(int i=0;i<n;i++) for(int j=0;j<n;j++) if(F[i][j]=='M') Qp.push({i,j,0}); bool good=false; vvb vis(n,vb(n,false)); while(!Qp.empty()) { ti cur=Qp.front(); Qp.pop(); int i=get<0>(cur),j=get<1>(cur),d=get<2>(cur); //cout << i << j << d << " "; vis[i][j]=true; if(F[i][j]=='D') {good=true; break;} if(tst+d/s>=hd[i][j]) continue; if(i>0 && !vis[i-1][j] && (F[i-1][j]=='G' || F[i-1][j]=='D')) Qp.push({i-1,j,d+1}); if(i<n-1 && !vis[i+1][j] && (F[i+1][j]=='G' || F[i+1][j]=='D')) Qp.push({i+1,j,d+1}); if(j>0 && !vis[i][j-1] && (F[i][j-1]=='G' || F[i][j-1]=='D')) Qp.push({i,j-1,d+1}); if(j<n-1 && !vis[i][j+1] && (F[i][j+1]=='G' || F[i][j+1]=='D')) Qp.push({i,j+1,d+1}); } if(!good) upb=tst-1; else lob=tst; } cout << upb; return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...