#define MAX 100
int queue[3][MAX*MAX*MAX],check[MAX][MAX][MAX],a[MAX][MAX][MAX],top,place,m,n,h,count=1,nmax;
void queueinsert(int x,int y,int z,int c)
{
queue[0][top]=x;
queue[1][top]=y;
queue[2][top++]=z;
check[x][y][z]=c;
}
void put(int x,int y,int z)
{
int c=check[x][y][z];
if(y-1<0||check[x][y-1][z]!=0||a[x][y-1][z]==-1);
else queueinsert(x,y-1,z,c+1);
if(y+1>=n||check[x][y+1][z]!=0||a[x][y+1][z]==-1);
else queueinsert(x,y+1,z,c+1);
if(z-1<0||check[x][y][z-1]!=0||a[x][y][z-1]==-1);
else queueinsert(x,y,z-1,c+1);
if(z+1>=m||check[x][y][z+1]!=0||a[x][y][z+1]==-1);
else queueinsert(x,y,z+1,c+1);
if(x-1<0||check[x-1][y][z]!=0||a[x-1][y][z]==-1);
else queueinsert(x-1,y,z,c+1);
if(x+1>=h||check[x+1][y][z]!=0||a[x+1][y][z]==-1);
else queueinsert(x+1,y,z,c+1);
}
int main()
{
scanf("%d %d %d",&m,&n,&h);
for(int i=0;i<h;i++){
for(int j=0;j<n;j++){
for(int k=0;k<m;k++){
scanf("%d",&a[i][j][k]);
if(a[i][j][k]==1)queueinsert(i,j,k,1);
}
}
}
while(m*n*h!=count){
if(check[queue[0][place]][queue[1][place]][queue[2][place]]!=0&&a[queue[0][place]][queue[1][place]][queue[2][place]]!=-1){
put(queue[0][place],queue[1][place],queue[2][place]);
place++;
}
count++;
}
for(int i=0;i<h;i++){
for(int j=0;j<n;j++){
for(int k=0;k<m;k++){
if(check[i][j][k]==0&&a[i][j][k]==0){
printf("-1");
return 0;
}
if(check[i][j][k]>nmax)nmax=check[i][j][k];
}
}
}
printf("%d",nmax-1);
return 0;
}
Compilation message
cc.c: In function ‘main’:
cc.c:28:5: warning: implicit declaration of function ‘scanf’ [-Wimplicit-function-declaration]
scanf("%d %d %d",&m,&n,&h);
^
cc.c:28:5: warning: incompatible implicit declaration of built-in function ‘scanf’
cc.c:29:5: error: ‘for’ loop initial declarations are only allowed in C99 or C11 mode
for(int i=0;i<h;i++){
^
cc.c:29:5: note: use option -std=c99, -std=gnu99, -std=c11 or -std=gnu11 to compile your code
cc.c:30:9: error: ‘for’ loop initial declarations are only allowed in C99 or C11 mode
for(int j=0;j<n;j++){
^
cc.c:31:13: error: ‘for’ loop initial declarations are only allowed in C99 or C11 mode
for(int k=0;k<m;k++){
^
cc.c:44:13: error: redefinition of ‘i’
for(int i=0;i<h;i++){
^
cc.c:29:13: note: previous definition of ‘i’ was here
for(int i=0;i<h;i++){
^
cc.c:44:5: error: ‘for’ loop initial declarations are only allowed in C99 or C11 mode
for(int i=0;i<h;i++){
^
cc.c:45:9: error: ‘for’ loop initial declarations are only allowed in C99 or C11 mode
for(int j=0;j<n;j++){
^
cc.c:46:13: error: ‘for’ loop initial declarations are only allowed in C99 or C11 mode
for(int k=0;k<m;k++){
^
cc.c:48:21: warning: implicit declaration of function ‘printf’ [-Wimplicit-function-declaration]
printf("-1");
^
cc.c:48:21: warning: incompatible implicit declaration of built-in function ‘printf’
cc.c:55:5: warning: incompatible implicit declaration of built-in function ‘printf’
printf("%d",nmax-1);
^