您的位置:首页 > 其它

hdu 1253 胜利大逃亡

2015-08-08 16:21 344 查看
hdu 1253 胜利大逃亡

解题思路: bfs 有一个有那么吊的剪枝,在代码上已经有解释。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<cmath>
#define N 55
using namespace std;
int T,a,b,c,t,visit

;
int Map

,mx,my,mz,sum,ans,flage;
int dir[6][3]= {{1,0,0},{-1,0,0},{0,1,0},{0,-1,0},{0,0,1},{0,0,-1}};
struct node
{
int x,y,z,time;
};
int bfs(int m)
{
node now,next;
int i;
queue<node>q;
now.x=0;
now.y=0;
now.z=0;
now.time=0;
q.push(now);
memset(visit,0,sizeof(visit));
visit[now.x][now.y][now.z]=1;
while(!q.empty())
{
now=q.front();
q.pop();
if(now.time>m)
return -1;
if(now.x==a-1&&now.y==b-1&&now.z==c-1)
return now.time;
for(i=0; i<6; i++)
{
next.x=now.x+dir[i][0];
next.y=now.y+dir[i][1];
next.z=now.z+dir[i][2];
if(next.x>=0&&next.x<a&&next.y>=0&&next.y<b&&next.z>=0&&next.z<c&&Map[next.x][next.y][next.z]!=1&&!visit[next.x][next.y][next.z])
{
next.time=now.time+1;
visit[next.x][next.y][next.z]=1;
if(abs(next.x-a+1)+abs(next.y-b+1)+abs(next.z-c+1)+next.time>m) // 一个吊渣天的剪枝
continue;
q.push(next);
}
}

}
return -1;
}
int main()
{
scanf("%d",&T);
while(T--)
{
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 z=0; z<c; z++)
{
scanf("%d",&Map[i][j][z]);
}
}
}
flage=0;
ans=bfs(t);
if(!flage)
printf("%d\n",ans);
else
printf("-1\n");
}
return 0;
}
//  感觉一般的bfs 和 优先队列的 bfs 还是有很大的区别的。


下图是 优先队列的 bfs 。在输入的时候还是大不一样的,不过总体思路还是不怎么变的。

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: