#include<bits/stdc++.h>
struct point
{
int x;
int y;
};
int t[1010][1010];
int n,mm,ans;
int main()
{
std::queue <point> q;
point m,l;
scanf("%d %d",&n,&mm);
for(int i=1;i<=n;i++)
{
for(int j=1;j<=mm;j++)
{
scanf("%d",&t[i][j]);
if(t[i][j]==1)
{
m.x=i,m.y=j;
q.push({m.x,m.y});
}
}
}
while(!q.empty())
{
l=q.front();
q.pop();
if(t[l.x-1][l.y]==0&&l.x-1>0) t[l.x-1][l.y]=t[l.x][l.y]+1,q.push({l.x-1,l.y}); //왼쪽 push
if(t[l.x+1][l.y]==0&&l.x+1<=n) t[l.x+1][l.y]=t[l.x][l.y]+1,q.push({l.x+1,l.y}); //오른쪽 push
if(t[l.x][l.y-1]==0&&l.y-1>0) t[l.x][l.y-1]=t[l.x][l.y]+1,q.push({l.x,l.y-1}); //위쪽 push
if(t[l.x][l.y+1]==0&&l.y+1<=mm) t[l.x][l.y+1]=t[l.x][l.y]+1,q.push({l.x,l.y+1}); //아래쪽 push
}
for(int i=1;i<=n;i++) for(int j=1;j<=mm;j++) if(t[i][j]==0) {printf("-1");return 0;} else if(t[i][j]>ans) ans=t[i][j];
printf("%d",ans-1);
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
5708 KB |
Output is correct |
2 |
Correct |
0 ms |
5708 KB |
Output is correct |
3 |
Correct |
0 ms |
5708 KB |
Output is correct |
4 |
Correct |
0 ms |
5708 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
5708 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
5708 KB |
Output is correct |
2 |
Incorrect |
0 ms |
5708 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
5708 KB |
Output is correct |
2 |
Correct |
3 ms |
5708 KB |
Output is correct |
3 |
Correct |
0 ms |
5708 KB |
Output is correct |
4 |
Correct |
0 ms |
5708 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
5 ms |
5708 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
5708 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
30 ms |
5708 KB |
Output is correct |
2 |
Incorrect |
0 ms |
5708 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
55 ms |
5708 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
31 ms |
5708 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
116 ms |
5708 KB |
Output is correct |
2 |
Correct |
129 ms |
5840 KB |
Output is correct |
3 |
Correct |
116 ms |
5708 KB |
Output is correct |
4 |
Incorrect |
47 ms |
5708 KB |
Output isn't correct |