http://acm.hdu.edu.cn/showproblem.php?pid=1253
//这题是简单的三维搜索,将而为的坐标改为三维
//其他的按照正常的bfs框架
// 984 ms 1544 kb
#include <stdio.h>
#include
<stdlib.h>
#include
<memory.h>
#include
<iostream>
#include
<queue>
using namespace std;

int map[55][55][55];
int T;//魔王到达的时间
int a,b,c;
int direction[6][3]={ 0, 0, 1,
0, 0,-1,
0, 1, 0,
0,-1, 0,
1, 0, 0,
-1, 0, 0};
typedef
struct node{
int a,b,c;
int time;
}NODE;
int bfs(int mark[][55][55]){
mark[
0][0][0]=1;
queue
<NODE> q;
NODE d,g;
d.a
=0;
d.b
=0;
d.c
=0;
d.time
=0;
q.push(d);
while(!q.empty()){
g
=q.front();
q.pop();

if(g.time<=T&&a-1==g.a&&b-1==g.b&&c-1==g.c){
cout
<<g.time<<endl;
return 1;
}
for(int i=0;i<6;i++){
d.a
=g.a+direction[i][0];
d.b
=g.b+direction[i][1];
d.c
=g.c+direction[i][2];
if( d.a>=0&&d.a<a&&d.b>=0&&d.b<b
&&d.c>=0&&d.c<c
&&0==mark[d.a][d.b][d.c]
&&0==map[d.a][d.b][d.c]){
mark[d.a][d.b][d.c]
=1;
d.time
=g.time+1;
q.push(d);
}
}
}
return 0;
}
int main(){
int k;
int flag;
scanf(
"%d",&k);
while(k--){
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",&map[i][j][k]);
int mark[55][55][55];
memset(mark,
0,sizeof(mark));
flag
=bfs(mark);
if(0==flag)
printf(
"-1\n");
}
}
posted on 2011-05-17 15:12  敌敌  阅读(146)  评论(0编辑  收藏  举报