
11
很水很水的广搜题目,真的。。。
#include <stdio.h>
#include <string.h>
#include <queue>
using namespace std;
int a,b,c,t;
struct Coor
{
int x,y,z,step;
};
int mapp[51][51][51],dis[6][3]={{1,0,0},{-1,0,0},{0,1,0},{0,-1,0},{0,0,1},{0,0,-1}};
bool vis[51][51][51];
bool judge(int x,int y,int z)
{
if(x<0 || y<0 || z<0 || x>=a || y>=b || z>=c) return 0;
if(vis[x][y][z] || mapp[x][y][z]) return 0;
return 1;
}
int bfs(int x,int y,int z)
{
queue <Coor> q;
Coor pre,next;
int i;
pre.x=x;
pre.y=y;
pre.z=z;
pre.step=0;
vis[x][y][z]=1;
q.push(pre);
while(!q.empty())
{
pre=q.front();
q.pop();
if(pre.x==a-1 && pre.y==b-1 && pre.z==c-1) return pre.step;
if(pre.step>t) return -1;
for(i=0;i<6;++i)
{
next.x=pre.x+dis[i][0];
next.y=pre.y+dis[i][1];
next.z=pre.z+dis[i][2];
if(judge(next.x,next.y,next.z))
{
next.step=pre.step+1;
vis[next.x][next.y][next.z]=1;
q.push(next);
}
}
}
return -1;
}
int main()
{
int i,j,k,sl;
scanf("%d",&sl);
while(sl--)
{
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",&mapp[i][j][k]);
memset(vis,0,sizeof(vis));
printf("%d\n",bfs(0,0,0));
}
return 0;
}
ACM-BFS之胜利大逃亡——hdu1253,布布扣,bubuko.com
原文:http://blog.csdn.net/lttree/article/details/23044951