https://vjudge.net/problem/HDU-1253
AC代码
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<cmath>
#include<cstring>
using namespace std;
const int N=55;
int a,b,c,t;
int g[N][N][N];
int bk[N][N][N];
int tx[]={1,-1,0,0,0,0};
int ty[]={0,0,1,-1,0,0};
int tz[]={0,0,0,0,1,-1};
struct node{
int x,y,z,step;
}t1,t2;
int main()
{
int n;
scanf("%d",&n);
while(n--)
{
scanf("%d %d %d %d",&a,&b,&c,&t);
memset(g,0,sizeof g);
memset(bk,0,sizeof bk);
for(int i=0;i<a;i++)
for(int j=0;j<b;j++)
for(int k=0;k<c;k++)
scanf("%d",&g[i][j][k]);
queue<node> q;
t1.x=0;
t1.y=0;
t1.z=0;
t1.step=0;
bk[0][0][0]=1;
q.push(t1);
while(q.size())
{
t1=q.front();
q.pop();
int f=0;
for(int i=0;i<6;i++)
{
t2.x=t1.x+tx[i];
t2.y=t1.y+ty[i];
t2.z=t1.z+tz[i];
if(t2.x>=0 && t2.x<a && t2.y>=0 && t2.y<b && t2.z>=0 && t2.z<c && !g[t2.x][t2.y][t2.z] && !bk[t2.x][t2.y][t2.z])
{
t2.step=t1.step+1;
bk[t2.x][t2.y][t2.z]=1;
if(abs(t2.x-a+1)+abs(t2.y-b+1)+abs(t2.z-c+1)+t2.step>t)
continue;
q.push(t2);
if(t2.x==a-1 && t2.y==b-1 && t2.z==c-1 && t2.step<=t)
{
f=1;
break;
}
}
}
if(f)
break;
}
if(q.size())
cout<<t2.step<<endl;
else
cout<<"-1"<<endl;
}
return 0;
}