# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
718787 | NintsiChkhaidze | Mecho (IOI09_mecho) | C++17 | 144 ms | 65536 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>
#define pb push_back
#define ll long long
#define f first
#define s second
#define int ll
using namespace std;
const int inf = 1e18,N = 805;
char a[N][N];
int dis[N][N],xm,ym,n,S;
bool fix[N][N];
vector <pair<int,int> > neighbours = {{0,1}, {0,-1},{1,0}, {-1,0}};
bool check(int t){
if (dis[xm][ym] <= t*S) return 0;
for (int i=1;i<=n;i++)
for (int j=1;j<=n;j++)
fix[i][j]=0;
queue <pair <int,pair<int,int> > > q;
while (q.size()) q.pop();
q.push({t*S,{xm,ym}});
fix[xm][ym] = 1;
while (q.size()){
int T = q.front().f,x = q.front().s.f,y = q.front().s.s;
q.pop();
if (a[x][y] == 'D') return 1;
for (auto [dx,dy]: neighbours){
int X = x+dx,Y = y+dy;
if (X < 1 || Y < 1 || X > n || Y > n || fix[X][Y]) continue;
if (a[X][Y] == 'T' || a[X][Y] == 'H') continue;
if (dis[X][Y] <= T + 1) continue;
q.push({T+1,{X,Y}});
}
}
return 0;
}
signed main() {
ios_base::sync_with_stdio(0),cin.tie(NULL),cout.tie(NULL);
cin>>n>>S;
queue <pair<int,pair<int,int> > > q;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++){
cin >> a[i][j];
dis[i][j] = inf;
if (a[i][j] == 'H') dis[i][j] = 0,q.push({0,{i,j}}),fix[i][j] = 1;
if (a[i][j] == 'M') xm = i,ym = j;
}
while (q.size()){
int d = q.front().f,x = q.front().s.f,y = q.front().s.s;
q.pop();
for (auto [dx,dy]: neighbours){
int X = x+dx,Y = y+dy;
if (X < 1 || Y < 1 || X > n || Y > n) continue;
if (fix[X][Y] || (a[X][Y] == 'T' || a[X][Y] == 'D')) continue;
dis[X][Y] = d + S;
fix[X][Y] = 1;
q.push({d+S,{X,Y}});
}
}
int l = 0,r = 1e9,ans=-1;
while (l <= r){
int mid = (l + r)>>1;
if (check(mid)) {
ans = mid,l = mid + 1;
}else{
r = mid - 1;
}
}
cout<<ans;
}
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |