# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
519445 | AdamGS | Mecho (IOI09_mecho) | C++17 | 188 ms | 6604 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;
typedef long long ll;
typedef long double ld;
#define rep(a, b) for(int a = 0; a < (b); ++a)
#define st first
#define nd second
#define pb push_back
#define all(a) a.begin(), a.end()
const int LIM=807, INF=1e9+7;
int dx[]={1, 0, -1, 0}, dy[]={0, 1, 0, -1};
string T[LIM];
int odl[LIM][LIM], wolne[LIM][LIM], n, s;
bool pole(int a, int b) {
return 0<=a && a<n && 0<=b && b<n;
}
bool ok(int x) {
queue<pair<int,int>>q;
rep(i, n) rep(j, n) {
if(T[i][j]=='M') {
q.push({i, j});
odl[i][j]=x*s;
} else odl[i][j]=INF;
}
while(!q.empty()) {
int a=q.front().st, b=q.front().nd; q.pop();
if(odl[a][b]>=wolne[a][b]) continue;
if(T[a][b]=='D') return true;
rep(i, 4) if(pole(a+dx[i], b+dy[i])) {
if((T[a+dx[i]][b+dy[i]]=='G' || T[a+dx[i]][b+dy[i]]=='D') && odl[a+dx[i]][b+dy[i]]==INF) {
odl[a+dx[i]][b+dy[i]]=odl[a][b]+1;
q.push({a+dx[i], b+dy[i]});
}
}
}
return false;
}
int main() {
ios_base::sync_with_stdio(0); cin.tie(0);
cin >> n >> s;
queue<pair<int,int>>q;
rep(i, n) {
cin >> T[i];
rep(j, n) {
if(T[i][j]=='H') q.push({i, j});
else wolne[i][j]=INF;
}
}
while(!q.empty()) {
int a=q.front().st, b=q.front().nd; q.pop();
rep(i, 4) if(pole(a+dx[i], b+dy[i])) {
if((T[a+dx[i]][b+dy[i]]=='G' || T[a+dx[i]][b+dy[i]]=='M') && wolne[a+dx[i]][b+dy[i]]==INF) {
wolne[a+dx[i]][b+dy[i]]=wolne[a][b]+s;
q.push({a+dx[i], b+dy[i]});
}
}
}
if(!ok(0)) {
cout << -1 << '\n';
return 0;
}
int p=0, k=LIM*LIM;
while(p<k) {
int sr=(p+k+1)/2;
if(ok(sr)) p=sr; else k=sr-1;
}
cout << p << '\n';
}
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |