题目分析:{1,0,0}写成{0,1,0},,,囧呀 找了半天错误,,,,,
#include<iostream>
#include<cstdio>
#include<queue>
using namespace std;
int maze[55][55][55],k,a,b,c,t,flag,ans;
int move[6][3]={{0,0,1},{0,1,0},{1,0,0},
{0, 0,-1},{0,-1,0},{-1,0,0}};//下 右 前 上 左 后{0,1,0}!!!!!!!!!!!!!
struct node{
int x;
int y;
int z;
int t;
}begin,hash[55][55][55];
int IN(int x,int y,int z)
{
if(x<=0||x>a ||y<=0||y>b ||z<=0||z>c)
return 0;
else
return 1;
}
void BFS()
{
int i;
node cur,temp;
queue<node>q;
begin.x=1,begin.y=1,begin.z=1;
begin.t=0;
maze[begin.x][begin.y][begin.z]=1;//起点忘标记了
q.push(begin);
while(!q.empty())
{
cur=q.front();
q.pop();
/*if((cur.x+cur.y+cur.z-a-b-c)-(t-cur.t)>0)//错过
continue; //return; 剪枝*/
for(i=0;i<6;i++)
{
temp.x=cur.x+move[i][0];
temp.y=cur.y+move[i][1];
temp.z=cur.z+move[i][2];
temp.t=cur.t+1;
if(temp.x==a&&temp.y==b&&temp.z==c)
{
flag=1;
ans=temp.t;
return;
}
if(IN(temp.x,temp.y,temp.z) && maze[temp.x][temp.y][temp.z]==0)
{
maze[temp.x][temp.y][temp.z]=1;//走过的节点 都被标记 免得重走
q.push(temp);
}
}
}
}
int main()
{
int i,j,m;
scanf("%d",&k);
while(k--)
{
scanf("%d %d %d %d",&a,&b,&c,&t);
for(i=1;i<=a;i++)
for(j=1;j<=b;j++)
for(m=1;m<=c;m++)
scanf("%d",&maze[i][j][m]);//搞成 abm了
/***for(i=1;i<=a;i++)
{
for(j=1;j<=b;j++)
{
for(m=1;m<=c;m++)
printf("%d ",maze[i][j][m]);
printf("\n");
}
printf("\n");
}
cout<<endl<<endl;*/
flag=0,ans=0;
if(maze[a][b][c]==1||a+b+c-t>0)
printf("-1\n");
else
{
BFS();
if(flag==0||ans>t)
printf("-1\n");
else
printf("%d\n",ans);
}
}
return 0;
}