hdu 1253 胜利大逃亡
2015-08-08 16:21
344 查看
hdu 1253 胜利大逃亡
解题思路: bfs 有一个有那么吊的剪枝,在代码上已经有解释。
下图是 优先队列的 bfs 。在输入的时候还是大不一样的,不过总体思路还是不怎么变的。
解题思路: 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 。在输入的时候还是大不一样的,不过总体思路还是不怎么变的。
相关文章推荐
- 干净清爽界面---资源浏览器形式的TXT阅读软件
- UVA 1423 Guess 【拓扑排序】
- Could not resolve archetype org.apache.maven.archetypes:maven-archetype-quickstart
- 自学perl 系列1
- 二分回顾
- 【Obective-C编程开发】Objective-C之BOOL一清二楚
- C语言中关于.h文件和.c文件详细解析
- C.Stripe(Codeforces Beta Round #18 (Div. 2 Only))
- 用户'sa'登录失败(DataGridView)
- MySQL连接查询
- 部分华为校园招聘机试试题及答案(C语言版本)
- 文件描述符及其重定向的学习笔记
- Input设备驱动之事件类型
- 设计模式--组合模式
- NSDatepicker
- iOS程序支持64位以及project.pbxproj文件的介绍
- socket长连接所用到的八大技术
- Thinkphp 3.2命名空间版如何使用PHPExcel导入Excel文件
- POJ3278.Catch That Cow
- 【POJ3270】【Cow Sorting】