#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);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
16712 KB |
Output is correct |
2 |
Correct |
0 ms |
16712 KB |
Output is correct |
3 |
Correct |
0 ms |
16712 KB |
Output is correct |
4 |
Correct |
0 ms |
16712 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
16712 KB |
Output is correct |
2 |
Correct |
0 ms |
16712 KB |
Output is correct |
3 |
Correct |
0 ms |
16712 KB |
Output is correct |
4 |
Correct |
0 ms |
16712 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
16712 KB |
Output is correct |
2 |
Correct |
0 ms |
16712 KB |
Output is correct |
3 |
Correct |
0 ms |
16712 KB |
Output is correct |
4 |
Correct |
0 ms |
16712 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
16712 KB |
Output is correct |
2 |
Correct |
4 ms |
16712 KB |
Output is correct |
3 |
Correct |
0 ms |
16712 KB |
Output is correct |
4 |
Correct |
0 ms |
16712 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
16712 KB |
Output is correct |
2 |
Correct |
0 ms |
16712 KB |
Output is correct |
3 |
Correct |
0 ms |
16712 KB |
Output is correct |
4 |
Correct |
4 ms |
16712 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
16712 KB |
Output is correct |
2 |
Correct |
0 ms |
16712 KB |
Output is correct |
3 |
Correct |
0 ms |
16712 KB |
Output is correct |
4 |
Correct |
4 ms |
16712 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
48 ms |
16712 KB |
Output is correct |
2 |
Correct |
20 ms |
16712 KB |
Output is correct |
3 |
Correct |
16 ms |
16712 KB |
Output is correct |
4 |
Correct |
16 ms |
16712 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
52 ms |
16712 KB |
Output is correct |
2 |
Correct |
48 ms |
16712 KB |
Output is correct |
3 |
Correct |
4 ms |
16712 KB |
Output is correct |
4 |
Correct |
8 ms |
16712 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
32 ms |
16712 KB |
Output is correct |
2 |
Correct |
100 ms |
16712 KB |
Output is correct |
3 |
Correct |
12 ms |
16712 KB |
Output is correct |
4 |
Correct |
60 ms |
16712 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
144 ms |
16712 KB |
Output is correct |
2 |
Correct |
140 ms |
16712 KB |
Output is correct |
3 |
Correct |
108 ms |
16712 KB |
Output is correct |
4 |
Correct |
100 ms |
16712 KB |
Output is correct |