#include<stdio.h>
#define MAX 500000
int arr[1000][1000],front=0,rear=0,cnt=2,m=0,n=0,day=0;
typedef struct data{
int i;
int j;
};
data queue[MAX];
int main()
{
int to_i[4]={-1,0,1,0};
int to_j[4]={0,-1,0,1};
int M,N;
scanf("%d %d",&M,&N);
for(int i=0;i<N;i++)
{
for(int j=0;j<M;j++)
{
scanf("%d",&arr[i][j]);
if(arr[i][j]==1)
{
rear=(rear+1)%MAX;
queue[rear].i=i;
queue[rear].j=j;
m++;
}
}
}
while(rear!=front)
{
front=(front+1)%MAX;
for(int k=0;k<4;k++)
{
if(queue[front].i+to_i[k]<N && queue[front].i+to_i[k]>=0 && queue[front].j+to_j[k]<M && queue[front].j+to_j[k]>=0)
{
if(!arr[queue[front].i+to_i[k]][queue[front].j+to_j[k]])
{
arr[queue[front].i+to_i[k]][queue[front].j+to_j[k]]=cnt;
rear=(rear+1)%MAX;
queue[rear].i=queue[front].i+to_i[k];
queue[rear].j=queue[front].j+to_j[k];
n++;
}
}
}
m--;
queue[front].i=queue[front].j=0;
if(m==0)
{
cnt++;
m=n;
n=0;
day++;
}
}
for(int i=0;i<N;i++)
{
for(int j=0;j<M;j++)
{
if(arr[i][j]==0)
{
printf("-1");
return 0;
}
}
}
printf("%d",day-1);
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
8900 KB |
Output is correct |
2 |
Correct |
0 ms |
8900 KB |
Output is correct |
3 |
Correct |
0 ms |
8900 KB |
Output is correct |
4 |
Correct |
0 ms |
8900 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
8900 KB |
Output is correct |
2 |
Correct |
0 ms |
8900 KB |
Output is correct |
3 |
Correct |
0 ms |
8900 KB |
Output is correct |
4 |
Correct |
0 ms |
8900 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
8900 KB |
Output is correct |
2 |
Correct |
0 ms |
8900 KB |
Output is correct |
3 |
Correct |
0 ms |
8900 KB |
Output is correct |
4 |
Correct |
0 ms |
8900 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
8900 KB |
Output is correct |
2 |
Correct |
4 ms |
8900 KB |
Output is correct |
3 |
Correct |
0 ms |
8900 KB |
Output is correct |
4 |
Correct |
0 ms |
8900 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
8900 KB |
Output is correct |
2 |
Correct |
0 ms |
8900 KB |
Output is correct |
3 |
Correct |
4 ms |
8900 KB |
Output is correct |
4 |
Correct |
4 ms |
8900 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
8900 KB |
Output is correct |
2 |
Correct |
4 ms |
8900 KB |
Output is correct |
3 |
Correct |
0 ms |
8900 KB |
Output is correct |
4 |
Correct |
0 ms |
8900 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
44 ms |
8900 KB |
Output is correct |
2 |
Correct |
16 ms |
8900 KB |
Output is correct |
3 |
Correct |
12 ms |
8900 KB |
Output is correct |
4 |
Correct |
12 ms |
8900 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
52 ms |
8900 KB |
Output is correct |
2 |
Correct |
48 ms |
8900 KB |
Output is correct |
3 |
Correct |
8 ms |
8900 KB |
Output is correct |
4 |
Correct |
8 ms |
8900 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
32 ms |
8900 KB |
Output is correct |
2 |
Correct |
96 ms |
8900 KB |
Output is correct |
3 |
Correct |
12 ms |
8900 KB |
Output is correct |
4 |
Correct |
64 ms |
8900 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
124 ms |
8900 KB |
Output is correct |
2 |
Correct |
136 ms |
8900 KB |
Output is correct |
3 |
Correct |
116 ms |
8900 KB |
Output is correct |
4 |
Correct |
92 ms |
8900 KB |
Output is correct |