# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
54675 | okaybody10 | Mecho (IOI09_mecho) | C++98 | 242 ms | 52292 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 pair<int,int> pii;
typedef pair<pii,int> piii;
int dx[4]={-1,0,1,0},dy[4]={0,1,0,-1};
int pre[2000][2000];
char mark[2000][2000];
int homex,homey;
int s,sx,sy,N;
bool test(int time)
{
bool reach[2005][2005];
memset(reach,0,sizeof(reach));
if(time * s >= pre[sx][sy]) return false;
queue<piii> q;
q.push(make_pair(make_pair(sx,sy),time*s));
reach[sx][sy]=1;
while(!q.empty())
{
int x=q.front().first.first;
int y=q.front().first.second;
int dis=q.front().second;
if(mark[x][y]=='D') return true;
q.pop();
for(int c=0;c<4;c++)
{
int cx=x+dx[c],cy=y+dy[c];
if(cx<0 || cx>=N || cy<0 || cy>=N || pre[cx][cy]<=dis+1 || mark[cx][cy]=='T' || reach[cx][cy]) continue;
reach[cx][cy]=true;
q.push(make_pair(make_pair(cx,cy),dis+1));
}
}
return false;
}
int main()
{
memset(pre,-1,sizeof(pre));
scanf("%d %d",&N,&s);
queue<pii> que;
for(int i=0;i<N;i++)scanf("%s",mark[i]);
for(int i=0;i<N;i++){
for(int j=0;j<N;j++)
{
if(mark[i][j]=='M') sx=i,sy=j,mark[i][j]='G';
if(mark[i][j]=='D') homex=i,homey=j;
if(mark[i][j]=='H') que.push(make_pair(i,j)),pre[i][j]=0;
}
}
pre[homex][homey]=N*N*s;
while(!que.empty())
{
int x=que.front().first;
int y=que.front().second;
int dis=pre[x][y];
que.pop();
for(int c=0;c<4;c++)
{
int cx=x+dx[c],cy=y+dy[c];
if(cx<0 || cx>=N || cy<0 || cy>=N || pre[cx][cy]!=-1 || mark[cx][cy]!='G') continue;
pre[cx][cy]=dis+s;
que.push(make_pair(cx,cy));
}
}
int low=0,high=N*N*2,ans=-1;
for(;low<high;)
{
int mid=(low+high)/2;
if(test(mid)) ans=max(ans,mid),low=mid+1;
else high=mid;
}
return !printf("%d",ans);
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |