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<bits/stdc++.h>
using namespace std;
struct data{
int x,y,z,lev;
};
queue<data> que;
int m,n,h,a[110][110][110],visit[110][110][110],cnt,tot;
int dr[]={1,-1,0,0,0,0},dc[]={0,0,1,-1,0,0},du[]={0,0,0,0,1,-1};
void push(int x,int y,int z,int level){
if(x<1||y<1||z<1||x>n||y>m||z>h||visit[x][y][z]||a[x][y][z]==-1)return;
visit[x][y][z]=1;
cnt++;
que.push({x,y,z,level});
}
int bfs(){
data t;
while(!que.empty()){
t=que.front();
que.pop();
for(int i=0;i<6;i++){
push(t.x+dr[i],t.y+dc[i],t.z+du[i],t.lev+1);
}
}
return t.lev;
}
int main(){
scanf("%d%d%d",&m,&n,&h);
tot=m*n*h;
for(int k=1;k<=h;k++){
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
scanf("%d",&a[i][j][k]);
if(a[i][j][k]==1)push(i,j,k,0);
if(a[i][j][k]==-1)tot--;
}
}
}
int ans=bfs();
if(tot!=cnt)printf("-1");
else printf("%d",ans);
}
Compilation message (stderr)
cc.cpp: In function 'int main()':
cc.cpp:30:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d%d",&m,&n,&h);
~~~~~^~~~~~~~~~~~~~~~~~~
cc.cpp:35:22: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d",&a[i][j][k]);
~~~~~^~~~~~~~~~~~~~~~~~
cc.cpp: In function 'int bfs()':
cc.cpp:26:14: warning: 't.data::lev' may be used uninitialized in this function [-Wmaybe-uninitialized]
return t.lev;
^~~
# | 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... |