这个题目还是没有太大难度的,如果想成是二维的话 那么就很简答了完全没有难度的bfs
三维了仅仅是数组多开了一维而已。
还有用一个数组 mintime标记一下到达某个点的最小时间 如果当前比最小时间要小才更新
#include<iostream>
#include<cstdio>
#include<queue>
using namespace std;
const int maxn=51;
const int inf=1005;
int map[maxn][maxn][maxn];
int mintime[maxn][maxn][maxn];
int dir[6][3]={0,0,1,0,0,-1,1,0,0,-1,0,0,0,1,0,0,-1,0};
int a,b,c,t;
struct Node
{
int x,y,z,time;
};
int bfs()
{
Node node,no;
node.x=node.y=node.z=node.time=0;
mintime[0][0][0]=0;
queue<Node> q;
q.push(node);
int i,j;
while(!q.empty())
{
node=q.front();
q.pop();
for(i=0;i<6;++i)
{
no.x=node.x+dir[i][0];
no.y=node.y+dir[i][1];
no.z=node.z+dir[i][2];
no.time=node.time+1;
if(mintime[no.x][no.y][no.z]>no.time && no.x>=0 && no.x<a && no.y>=0 && no.y<b && no.z>=0 && no.z<c && !map[no.x][no.y][no.z])
{
mintime[no.x][no.y][no.z]=no.time;
q.push(no);
}
}
}
return mintime[a-1][b-1][c-1];
}
int main()
{
int T;
scanf("%d",&T);
int i,j,k,ans;
while(T--)
{
scanf("%d %d %d %d",&a,&b,&c,&t);
for(i=0;i<a;++i)
{
for(j=0;j<b;++j)
{
for(k=0;k<c;++k)
{
scanf("%d",&map[i][j][k]);
mintime[i][j][k]=inf;
}
}
}
ans=bfs();
if(ans>t)
printf("-1\n");
else
printf("%d\n",ans);
}
return 0;
}