很水,
搜寻还是的细心,一点点的错,就会交不上。。。
注意判断结束是否可走,(只是为了优化)
#include<stdio.h>
#include<queue>
using namespace std;
#define N 55
struct node{ //队列,广搜;(地图大小,地图走向,
int x,y,z,step; // 地图类型,地图初始点,目的,标记走过)
};
int map[N][N][N];
int dir[6][3]={0,0,1, 0,0,-1, 0,1,0, 0,-1,0, 1,0,0, -1,0,0};
int a,b,c,t;
bool judge(int x,int y,int z)
{
if(x>=0 && x<a && y>=0 && y<b && z>=0&&z<c&& map[x][y][z]==0)
return 1;
return 0;
}
int bfs()
{
queue<node>q;
node cur,next;
int k,x,y,z;
cur.x=cur.y=cur.z=cur.step=0;
q.push(cur);
map[0][0][0]=1;
while(!q.empty())
{
cur=q.front();
q.pop();
next.step=cur.step+1;
if(next.step>t)
continue;
for(k=0;k<6;k++)
{
next.x=x=cur.x+dir[k][0];
next.y=y=cur.y+dir[k][1];
next.z=z=cur.z+dir[k][2];
if(judge(x,y,z))
{
if(x==a-1&&y==b-1&&z==c-1)
return next.step;
map[x][y][z]=1;
q.push(next);
}
}
}
return -1;
}
int main ()
{
int i,k,j,T,res;
scanf("%d",&T);
while(T--)
{
scanf("%d%d%d%d",&a,&b,&c,&t);
for(i=0;i<a;i++)
for(k=0;k<b;k++)
for(j=0;j<c;j++)
scanf("%d",&map[i][k][j]);
if(map[a-1][b-1][c-1])
res=-1;
else
res=bfs();
printf("%d\n",res);
}
return 0;
}