ac 代码...
# include<cstdio>
# include<algorithm>
# include<cstdlib>
# include<queue>
# include<string.h>
using namespace std;
int map[60][60][60];
int vis[60][60][60];
int fx[6][3]={
{0,0,1},
{0,0,-1},
{0,1,0},
{0,-1,0},
{1,0,0},
{-1,0,0}
}; //定义了六种方向,通过xyz来控制方向
int A,B,C,time;
struct node{
int x,y,z; // x.y.z 路径 ..坐标
int step; // 走到该方向时的步数,按层搜索,所以同意层六个方向的步数是一样的,就看哪个先出去了
};
int bfs(){
queue<struct node> q;
struct node start,next;
start.x=0;
start.y=0;
start.z=0;
start.step=0;
q.push(start);
while(!q.empty()){
start=q.front();
q.pop();
for(int i=0 ;i<6 ;i++){
next.x=start.x+fx[i][0];
next.y=start.y+fx[i][1];
next.z=start.z+fx[i][2]; // 六个方向搜索
next.step=start.step+1;
if(next.x>=0 && next.x <A && next.y>=0 && next.y<B && next.z>=0 && next.z<C && vis[next.x][next.y][next.z]==0 && map[next.x][next.y][next.z]==0){
vis[next.x][next.y][next.z] = 1; //判断是否满足条件..然后把它标记..不再搜索重复的点了
q.push(next);
if(next.x== A-1 && next.y == B-1 && next.z == C-1){
if(next.step <= time) return next.step; // 如果满足条件就打印..
else return -1; // 因为有一个时间要求..所以打印的情况有两种
}
}
}
}return -1;
}
int main(){
int T;
scanf("%d",&T);
while(T--){
memset(vis,0,sizeof(vis));
memset(map,0,sizeof(map));
scanf("%d %d %d %d",&A,&B,&C,&time);
for(int i=0 ;i<A ;i++)
for(int j=0 ;j<B ;j++)
for(int k=0 ;k<C; k++)
scanf("%d",&map[i][j][k]);
// 三维数组的输入...
//如果是字符型三维数组 ?
printf("%d\n",bfs());
}
}