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<cstdio>
int d[1000][1000];
int a[1000][1000];
int qx[1000000];
int qy[1000000];
int qn;
int px[4]={1,-1,0,0};
int py[4]={0,0,1,-1};
int main()
{
int x,y,dest=0;
int i,j,n,m;
scanf("%d%d",&m,&n);
for(i=0;i<n;i++)for(j=0;j<m;j++)scanf("%d",&a[i][j]);
for(i=0;i<n;i++)for(j=0;j<m;j++)
{
if(a[i][j]==1)
{
d[i][j]=1;
qx[qn]=i;
qy[qn]=j;
qn++;
}
if(a[i][j]>=0)
dest++;
}
for(i=0;i<qn;i++)
{
x=qx[i];
y=qy[i];
for(j=0;j<4;j++)
{
x+=px[j];
y+=py[j];
if(x>=0&&x<n&&y>=0&&y<m&&a[x][y]==0&&!d[x][y])
{
d[x][y]=d[qx[i]][qy[i]]+1;
qx[qn]=x;
qy[qn]=y;
qn++;
}
x-=px[j];
y-=py[j];
}
}
if(qn<dest)puts("-1");
else printf("%d",d[qx[qn-1]][qy[qn-1]]-1);
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |