HDU - 1253
#include<cstdio>
#include<queue>
#include<cstring>
using namespace std;
int a,b,c,t;
int mp[57][57][57];
struct data
{
int x,y,z;
int cost;
data(){cost=0;}
data(int a,int b,int c,int d)
{
z=a;
x=b;
y=c;
cost=d;
}
}d[1007];
queue<data>que;
bool ok(int x,int y,int z)
{
if(x<0||x>=b||y<0||y>=c||z<0||z>=a||mp[z][x][y])
{
return false;
}
return true;
}
bool vis[57][57][57];
int dx[]={0,1,0,-1,0,0};
int dy[]={1,0,-1,0,0,0};
int dz[]={0,0,0,0,1,-1};
int bfs()
{
memset(vis,0,sizeof vis);
while(!que.empty())
que.pop();
vis[0][0][0]=1;
data st=data(0,0,0,0);
que.push(st);
while(!que.empty())
{
data now=que.front();
que.pop();
if(now.z==a-1&&now.x==b-1&&now.y==c-1)
{
return now.cost;
}
for(int i=0;i<6;i++)
{
int zz,xx,yy;
xx=now.x+dx[i];
yy=now.y+dy[i];
zz=now.z+dz[i];
if(ok(xx,yy,zz)&&!vis[zz][xx][yy])
{
vis[zz][xx][yy]=1;
que.push(data(zz,xx,yy,now.cost+1));
}
}
}
return 0x3f3f3f3f;
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d%d%d%d",&a,&b,&c,&t);
for(int i=0;i<a;i++)
{
for(int j=0;j<b;j++)
{
for(int k=0;k<c;k++)
{
scanf("%d",&mp[i][j][k]);
}
}
}
// for(int i=0;i<a;i++)
// {
// for(int j=0;j<b;j++)
// {
//
// for(int k=0;k<c;k++)
// {
// printf("%d",mp[i][j][k]);
// }
// }
// puts("");
// }
int tmp=bfs();
if(tmp>t)
{
puts("-1");
}
else
printf("%d\n",tmp);
}
return 0;
}