点击打开杭电1253
1 3 3 4 20 0 1 1 1 0 0 1 1 0 1 1 1 1 1 1 1 1 0 0 1 0 1 1 1 0 0 0 0 0 1 1 0 0 1 1 0
11
#include<stdio.h> #include<queue> #include<iostream> using namespace std; typedef struct node { int x,y,z; int time; }Node; int X,Y,Z,T,map[55][55][55],Time; int dir[6][3]={{1,0,0},{-1,0,0},{0,1,0},{0,-1,0},{0,0,-1},{0,0,1}}; void BFS(int x,int y,int z) { Node s,q; queue<Node> Q; int e; map[z][y][x]=1; s.x=x; s.y=y; s.z=z; s.time=0; if(s.x==X-1&&s.y==Y-1&&s.z==Z-1&&s.time<=T) { Time=s.time; return ; } Q.push(s); while(!Q.empty()) { q=Q.front(); Q.pop(); for(e=0;e<6;e++) if(dir[e][0]+q.x>=0&&dir[e][0]+q.x<X) if(dir[e][1]+q.y>=0&&dir[e][1]+q.y<Y) if(dir[e][2]+q.z>=0&&dir[e][2]+q.z<Z) if(0==map[dir[e][2]+q.z][dir[e][1]+q.y][dir[e][0]+q.x]&&q.time+1<=T) { if(dir[e][0]+q.x==X-1&&dir[e][1]+q.y==Y-1&&dir[e][2]+q.z==Z-1) { Time=q.time+1; return ; } s.x=dir[e][0]+q.x; s.y=dir[e][1]+q.y; s.z=dir[e][2]+q.z; s.time=q.time+1; if(x-s.x+Y-s.y+Z-s.z-3>T-s.time) { map[s.z][s.y][s.x]=1; continue ; } map[s.z][s.y][s.x]=1; Q.push(s); } } } int main() { int i,j,e,t; scanf("%d",&t); while(t--) { scanf("%d %d %d %d",&Z,&Y,&X,&T); for(e=0;e<Z;e++) for(i=0;i<Y;i++) for(j=0;j<X;j++) scanf("%d",&map[e][i][j]); if(X+Y+Z-3>T||map[Z-1][Y-1][X-1]==1) { printf("-1\n"); continue; } Time=-1; BFS(0,0,0); printf("%d\n",Time); } return 0; }