This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <stdio.h>
#include <queue>
#define m(a,b) std::make_pair(a,b)
typedef std::pair<int,int> P;
typedef std::queue<P> Q;
Q q;
int arr[1000][1000];
int dx[4]={1,0,-1,0};
int dy[4]={0,1,0,-1};
int main()
{
int s=0,c=0,p=0,d=0,m,n,nx,ny,i,j;
scanf("%d%d",&m,&n);
for(i=0;i<n;i++)
for(j=0;j<m;j++)
scanf("%d",&arr[i][j]);
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
if(arr[i][j]==1)
{
q.push(m(i,j));
p++;
}
if(arr[i][j]==0)
s++;
}
}
while(!q.empty())
{
for(i=0;i<4;i++)
{
nx=q.front().first+dx[i];
ny=q.front().second+dy[i];
if(0<=nx&&nx<n&&0<=ny&&ny<m&&arr[nx][ny]==0)
{
arr[nx][ny]=1;
q.push(m(nx,ny));
c++;
s--;
}
}
q.pop();
p--;
if(p==0)
{
if(c!=0)
d++;
p=c;
c=0;
}
}
if(s!=0)
printf("-1");
else
printf("%d",d);
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |