Submission #1179

# Submission time Handle Problem Language Result Execution time Memory
1179 2013-06-29T11:48:36 Z gs13068 토마토 (KOI13_tomato) C++
16 / 16
144 ms 16712 KB
#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
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
# Verdict Execution time Memory 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
# Verdict Execution time Memory 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
# Verdict Execution time Memory 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
# Verdict Execution time Memory 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
# Verdict Execution time Memory 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
# Verdict Execution time Memory 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
# Verdict Execution time Memory 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
# Verdict Execution time Memory 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
# Verdict Execution time Memory 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