此题水,,只是为了长记性
#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;
int map[100][100][100];
int A,B,C,T;
int dx[10] = {0,0,0,0,1,-1};
int dy[14] = {0,0,1,-1,0,0};
int dz[14] = {1,-1,0,0,0,0};
typedef struct N
{
int x,y,z;
int step;
}Ac;
queue <Ac> s;
void Fz() //控制边界
{
for(int i=0;i<=A+5;i++)
for(int j=0;j<=B+5;j++)
for(int k=0;k<=C+5;k++)
map[i][j][k] = 1;
}
void bfs()
{
while(!s.empty())
{
Ac Node = s.front();
for(int i=0;i<6;i++)
{
int x = Node.x + dx[i];
int y = Node.y + dy[i];
int z = Node.z + dz[i];
if(x==A&&y==B&&z==C)
{
if(Node.step+1<=T&&map[x][y][z]==0)
{
printf("%d\n",Node.step+1);
}
else
puts("-1");
return ;
}
if(map[x][y][z]==0)
{
s.push({x,y,z,Node.step+1});
map[x][y][z] = 1;
}
}
s.pop();
}
puts("-1");
}
int main()
{
int n,m;
int N;
scanf("%d",&N);
while(N--)
{
while(!s.empty())s.pop();
scanf("%d%d%d%d",&A,&B,&C,&T);
Fz(); // 长记性,,一个小时
for(int i=1;i<=A;i++)
for(int j=1;j<=B;j++)
for(int k=1;k<=C;k++)
{
scanf("%d",&map[i][j][k]);
}
s.push({1,1,1,0});
bfs();
}
}