# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
982894 | doducanh | Mecho (IOI09_mecho) | C++17 | 168 ms | 7004 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 dx[4]={0,1,-1,0};
int dy[4]={1,0,0,-1};
string fields[805];
int beetime[805][805];
int mechotime[805][805];
int n,s;
bool check(int x,int y)
{
return (x>=0&&x<n&&y>=0&&y<n&&(fields[x][y]=='G'||fields[x][y]=='M'));
}
bool is_acc(int x,int y)
{
return ((x/s)<y);
}
int main()
{
cin>>n>>s;
for(int i=0;i<n;i++)cin>>fields[i];
int mechoposx,mechoposy;
int mechohomex,mechohomey;
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
beetime[i][j]=-1;
}
}
queue<pair<int,int>>q;
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
if(fields[i][j]=='M'){
mechoposx=i;
mechoposy=j;
}
if(fields[i][j]=='D'){
mechohomex=i;
mechohomey=j;
}
if(fields[i][j]=='H'){
beetime[i][j]=0;
q.push({i,j});
}
}
}
while(q.size()){
int x=q.front().first;
int y=q.front().second;
q.pop();
for(int i=0;i<4;i++){
int nx=x+dx[i];
int ny=y+dy[i];
if(check(nx,ny)&&beetime[nx][ny]==-1){
beetime[nx][ny]=beetime[x][y]+1;
q.push({nx,ny});
}
}
}
int l=0,r=n*n;
int res=0;
while(l<=r){
int eating_time=(l+r)/2;
q.push({mechoposx,mechoposy});
if(beetime[mechoposx][mechoposy]<=eating_time)q.pop();
bool acc=false;
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
mechotime[i][j]=-1;
}
}
mechotime[mechoposx][mechoposy]=0;
while(q.size()){
int x=q.front().first;
int y=q.front().second;
q.pop();
for(int i=0;i<4;i++)
{
int nx=x+dx[i];
int ny=y+dy[i];
if(check(nx,ny)&&mechotime[nx][ny]==-1&&is_acc(mechotime[x][y]+1,beetime[nx][ny]-eating_time)) {
mechotime[nx][ny]=mechotime[x][y]+1;
q.push({nx,ny});
}
}
}
for(int i=0;i<4;i++){
int nx=mechohomex+dx[i];
int ny=mechohomey+dy[i];
if(check(nx,ny)&&mechotime[nx][ny]!=-1){
acc=true;
break;
}
}
if(acc){
res=eating_time;
l=eating_time+1;
}
else r=eating_time-1;
}
cout<<res;
return 0;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |