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 <cstdio>
#include <queue>
int m,n,h;
int a[101][101][101];
int depth[101][101][101];
int v[101][101][101];
std::queue<int> x,y,z,d;
void f(int i, int j, int k, int dt){
v[i][j][k]=1;
depth[i][j][k]=dt;
x.push(i);
y.push(j);
z.push(k);
d.push(dt);
}
int main(){
// ("input3.txt","r",stdin);
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){
f(i,j,k,0);
}
if(a[i][j][k]==-1){
v[i][j][k]=1;
}
}
}
}
int cx,cy,cz,cd;
while (!x.empty()) {
cx = x.front();
cy = y.front();
cz = z.front();
cd = d.front();
if(cx && !v[cx-1][cy][cz]){
f(cx-1,cy,cz,cd+1);
}
if(cy && !v[cx][cy-1][cz]){
f(cx,cy-1,cz,cd+1);
}
if(cz && !v[cx][cy][cz-1]){
f(cx,cy,cz-1,cd+1);
}
if(cx+1<h && !v[cx+1][cy][cz]){
f(cx+1,cy,cz,cd+1);
}
if(cy+1<n && !v[cx][cy+1][cz]){
f(cx,cy+1,cz,cd+1);
}
if(cz+1<m && !v[cx][cy][cz+1]){
f(cx,cy,cz+1,cd+1);
}
x.pop();
y.pop();
z.pop();
d.pop();
}
int dtmax = 0;
int unvisited = 0;
for (int i=0; i<h; i++) {
for (int j=0; j<n; j++) {
for (int k=0; k<m; k++) {
if(dtmax<depth[i][j][k]) dtmax = depth[i][j][k];
if(v[i][j][k]==0) unvisited = 1;
}
}
}
if(unvisited) printf("-1");
else printf("%d",dtmax);
}
# | 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... |