#include<cstdio>
#include<queue>
using namespace std;
struct info{
int x,y,d;
info(){}
info(int x,int y,int d){ this->x=x; this->y=y; this->d=d; }
};
queue<info> Q;
int N,M,box[1010][1010],D=0,R;
int main(){
int i,j;
scanf("%d%d",&M,&N);
R=M*N;
for(i=0;i<N;++i){
for(j=0;j<M;++j){
scanf("%d",&box[i][j]);
if(box[i][j]==-1) --R;
else if(box[i][j]==1){
Q.push(info(i-1,j,1));
Q.push(info(i+1,j,1));
Q.push(info(i,j-1,1));
Q.push(info(i,j+1,1));
--R;
}
}
}
while(!Q.empty()){
info I=Q.front(); Q.pop();
if(I.x>=0 && I.x<N && I.y>=0 && I.y<M && box[I.x][I.y]==0){
D=I.d;
box[I.x][I.y]=1; --R;
Q.push(info(I.x-1,I.y,I.d+1));
Q.push(info(I.x+1,I.y,I.d+1));
Q.push(info(I.x,I.y-1,I.d+1));
Q.push(info(I.x,I.y+1,I.d+1));
}
}
if(R) printf("-1");
else printf("%d",D);
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
5564 KB |
Output is correct |
2 |
Correct |
0 ms |
5564 KB |
Output is correct |
3 |
Correct |
0 ms |
5564 KB |
Output is correct |
4 |
Correct |
0 ms |
5564 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
5564 KB |
Output is correct |
2 |
Correct |
0 ms |
5564 KB |
Output is correct |
3 |
Correct |
0 ms |
5564 KB |
Output is correct |
4 |
Correct |
0 ms |
5564 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
5564 KB |
Output is correct |
2 |
Correct |
0 ms |
5564 KB |
Output is correct |
3 |
Correct |
0 ms |
5564 KB |
Output is correct |
4 |
Correct |
0 ms |
5564 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5696 KB |
Output is correct |
2 |
Correct |
6 ms |
5956 KB |
Output is correct |
3 |
Correct |
1 ms |
5564 KB |
Output is correct |
4 |
Correct |
0 ms |
5564 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
5564 KB |
Output is correct |
2 |
Correct |
0 ms |
5564 KB |
Output is correct |
3 |
Correct |
7 ms |
5564 KB |
Output is correct |
4 |
Correct |
4 ms |
5564 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
5564 KB |
Output is correct |
2 |
Correct |
0 ms |
5564 KB |
Output is correct |
3 |
Correct |
0 ms |
5564 KB |
Output is correct |
4 |
Correct |
0 ms |
5564 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
38 ms |
5692 KB |
Output is correct |
2 |
Correct |
33 ms |
5564 KB |
Output is correct |
3 |
Correct |
15 ms |
5696 KB |
Output is correct |
4 |
Correct |
9 ms |
5564 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
56 ms |
5824 KB |
Output is correct |
2 |
Correct |
49 ms |
5956 KB |
Output is correct |
3 |
Correct |
7 ms |
5564 KB |
Output is correct |
4 |
Correct |
14 ms |
5564 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
43 ms |
5692 KB |
Output is correct |
2 |
Correct |
118 ms |
5564 KB |
Output is correct |
3 |
Correct |
14 ms |
5696 KB |
Output is correct |
4 |
Correct |
83 ms |
5564 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
144 ms |
6088 KB |
Output is correct |
2 |
Correct |
142 ms |
7012 KB |
Output is correct |
3 |
Correct |
134 ms |
5564 KB |
Output is correct |
4 |
Correct |
110 ms |
5564 KB |
Output is correct |