#include<iostream>
#include<malloc.h>
#include<cstdio>
#include<queue>
/*输入数据的第一行是一个正整数K,表明测试数据的数量.每组测试数据的第一行是四个正整数A,B,C和T
(1<=A,B,C<=50,1<=T<=1000),它们分别代表城堡的大小和魔王回来的时间.
然后是A块输入数据(先是第0块,然后是第1块,第2块......),每块输入数据有B行,每行有C个正整数,
代表迷宫的布局,其中0代表路,1代表墙。*/
using namespace std;
#define N 55
int table[N][N][N];
int masktable[N][N][N];
int move[6][3]={
1,0,0,
-1,0,0,
0,1,0,
0,-1,0,
0,0,1,
0,0,-1
};
int A,B,C,T;
typedef struct location
{
int x;
int y;
int z;
int num;
};
queue<location>xusignal;
int bfs()
{
int k,i,j;
location tmploc;
location newloc;
while(!xusignal.empty())
{
tmploc=xusignal.front();
xusignal.pop();
for(i=0;i<6;i++)
{
if((tmploc.x+move[i][0]<0)||(tmploc.x+move[i][0]>A-1)||(tmploc.y+move[i][1]<0)||(tmploc.y+move[i][1]>B-1)||(tmploc.z+move[i][2]<0)||(tmploc.z+move[i][2]>C-1))
{
continue;
}
if(masktable[tmploc.x+move[i][0]][tmploc.y+move[i][1]][tmploc.z+move[i][2]]==1)
{
continue;
}
if(table[tmploc.x+move[i][0]][tmploc.y+move[i][1]][tmploc.z+move[i][2]]==1)
{
continue;
}
if((tmploc.x+move[i][0]==A-1)&&(tmploc.y+move[i][1]==B-1)&&(tmploc.z+move[i][2]==C-1))
{
if(tmploc.num<T)
{
return tmploc.num;
}
}
masktable[tmploc.x+move[i][0]][tmploc.y+move[i][1]][tmploc.z+move[i][2]]=1;
newloc.x=tmploc.x+move[i][0];
newloc.y=tmploc.y+move[i][1];
newloc.z=tmploc.z+move[i][2];
newloc.num=tmploc.num+1;
xusignal.push(newloc);
}
}
return -1;
}
int main()
{
int K;
cin>>K;
int k,i,j;
while(K!=0)
{
K--;
/read
cin>>A>>B>>C>>T;
for(k=0;k<A;k++)
{
for(i=0;i<B;i++)
{
for(j=0;j<C;j++)
{
cin>>table[k][i][j];
masktable[k][i][j]=0;
}
}
}
///clear
while(!xusignal.empty())
{
xusignal.pop();
}
//bfs
location firstloc;
firstloc.x=firstloc.y=firstloc.z=0;
firstloc.num=1;
masktable[0][0][0]=1;
xusignal.push(firstloc);
cout<<bfs()<<endl;
}
return 0;
}
胜利大逃亡
最新推荐文章于 2017-12-07 21:24:55 发布