# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
956821 | DeltaStruct | Mecho (IOI09_mecho) | C++17 | 181 ms | 11816 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
int main(){
#define int long long
int n,m,inf=numeric_limits<int>::max(); cin >> n >> m; int sx,sy,gx,gy;
vector<string> A(n); for (auto& a:A) cin >> a;
queue<pair<int,int>> que; vector hd(n,vector<int>(n,inf));
for (int i(0);i < n;++i) for (int k(0);k < n;++k){
if (A[i][k]=='M') sx=i,sy=k;
if (A[i][k]=='D') gx=i,gy=k;
if (A[i][k]=='H') que.emplace(i,k),hd[i][k] = 0;
}
bool psf = true; auto can = [&psf,&A,&n](int x,int y){
if (x==-1||x==n||y==-1||y==n) return false;
if (A[x][y]=='T') return false;
if (psf&&A[x][y]=='D') return false;
return true;
};
while(!que.empty()){
auto [i,k] = que.front(); que.pop();
if (can(i+1,k)&&hd[i+1][k]==inf) hd[i+1][k]=hd[i][k]+m,que.emplace(i+1,k);
if (can(i-1,k)&&hd[i-1][k]==inf) hd[i-1][k]=hd[i][k]+m,que.emplace(i-1,k);
if (can(i,k+1)&&hd[i][k+1]==inf) hd[i][k+1]=hd[i][k]+m,que.emplace(i,k+1);
if (can(i,k-1)&&hd[i][k-1]==inf) hd[i][k-1]=hd[i][k]+m,que.emplace(i,k-1);
}
psf = false; int l = -1,r = 1e9,mid;
while(abs(l-r)>1){
mid = (l+r)/2;
vector dist(n,vector<int>(n,inf)); dist[sx][sy] = mid*m,que.emplace(sx,sy);
while(!que.empty()){
auto [i,k] = que.front(); que.pop();
if (can(i+1,k)&&dist[i][k]+1<hd[i+1][k]&&dist[i+1][k]==inf) dist[i+1][k] = dist[i][k]+1,que.emplace(i+1,k);
if (can(i-1,k)&&dist[i][k]+1<hd[i-1][k]&&dist[i-1][k]==inf) dist[i-1][k] = dist[i][k]+1,que.emplace(i-1,k);
if (can(i,k+1)&&dist[i][k]+1<hd[i][k+1]&&dist[i][k+1]==inf) dist[i][k+1] = dist[i][k]+1,que.emplace(i,k+1);
if (can(i,k-1)&&dist[i][k]+1<hd[i][k-1]&&dist[i][k-1]==inf) dist[i][k-1] = dist[i][k]+1,que.emplace(i,k-1);
}
if (dist[gx][gy]==inf) r = mid;
else l = mid;
}
cout << l << endl;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |