poj 2251
来源:互联网 发布:仓廪实而知礼节 编辑:程序博客网 时间:2024/05/29 04:01
最短路径一定要用BFS,
DFS求最短路径不及BFS系列
DFS求最短路径不及BFS系列
点击(此处)折叠或打开
- #include <iostream>
- #include <stdio.h>
- #include <cstring>
- #include <queue>
- using namespace std;
- #define MAX_V (40+1)
- struct point{
- int x;
- int y;
- int z;
- int l;
- };
- char T[MAX_V][MAX_V][MAX_V];
- char V[MAX_V][MAX_V][MAX_V];
- int l,r,c;
- int si,sj,sk;
- struct move{
- int x;
- int y;
- int z;
- }MOVE[]={ {-1,0,0},{1,0,0},{0,-1,0},{0,1,0},{0,0,-1},{0,0,1} };
- int ml = sizeof(MOVE) /sizeof(move);
- int bfs()
- {
-
- struct point Q[50000];
- Q[0].x=si;
- Q[0].y=sj;
- Q[0].z=sk;
- Q[0].l=1;
- int h=0,t=1;
- memset(V,0,sizeof(V));
- V[si][sj][sk]=1;
- while(h<t)
- {
- //point p=Q.front();
- //Q.pop();
- point p=Q[h];
- h++;
- //printf("(%d,%d,%d)=%d\n",p.x,p.y,p.z,p.l);
- //V[x][y][z]=1; //习惯性的再这里加访问标志
//如果不是求最小生成树等BFS变种,其实一个元素放到队列里,就是已经访问了。 - for(int m=0;m<ml;m++)
- {
- int x=MOVE[m].x + p.x;
- int y=MOVE[m].y + p.y;
- int z=MOVE[m].z + p.z;
- if( x < 1 || y < 1 || z < 1)
- continue;
- if(x > l || y > r || z > c)
- continue;
- if(V[x][y][z] != 0)
- continue;
- if(T[x][y][z] == 'E')
- return p.l;
- if(T[x][y][z] == '.')
- {
- Q[t].x=x;
- Q[t].y=y;
- Q[t].z=z;
- Q[t].l=p.l+1;
- t++;
- V[x][y][z]=1; //所以BFS的visit应该放在这里设标志。这样可以减少很多出队入队操作,也避免重复
- }
- }
-
- }
- return -1;
- }
- int main()
- {
- while(cin>>l>>r>>c)
- {
- if(l <= 0)
- return 0;
- for(int i=1;i<=l;i++)
- {
- for(int j=1;j<=r;j++)
- {
- for(int k=1;k<=c;k++)
- {
- char c;
- cin>>c;
- T[i][j][k]=c;
- //printf("read (%d,%d,%d)=%c\n",i,j,k,(char)T[i][j][k]);
- if( T[i][j][k] == 'S')
- {
- si=i;
- sj=j;
- sk=k;
- }
- }
- }
- }
- int ret = bfs();
- if(ret < 0)
- cout<<"Trapped!"<<endl;
- else
- cout<<"Escaped in "<<ret<<" minute(s)."<<endl;
- }
- }
0
上一篇:poj 3083
下一篇:算法导论 DP 矩阵链乘法
相关热门文章
- linux 常见服务端口
- 【ROOTFS搭建】busybox的httpd...
- 什么是shell
- linux socket的bug??
- linux的线程是否受到了保护?...
- cpu %和load average是怎样的...
- 如何确定线程使用的端口...
- mysql整形数据类型插入0001数...
- weblogic集群下启动服务有一个...
- Ajax 权限验证
给主人留下些什么吧!~~
评论热议
- POJ-2251
- POJ 2251
- poj 2251
- poj 2251
- POJ 2251
- poj 2251
- POJ 2251
- POJ 2251
- poj 2251
- poj-2251
- poj 2251
- POJ 2251
- poj 2251
- poj 2251
- poj 2251
- poj 2251
- 2251POJ
- poj-2251
- poj 1321
- poj 3009 - Curling 2.0
- poj 3083
- HDU 4403 A very hard Aoshu problem
- jquery & js 获取浏览器、屏幕高度
- poj 2251
- 算法导论 DP 矩阵链乘法
- jquery控制动态生成的gridview中多列checkbox的全选反选及自动判断是否全选状态
- (apple官方文档译文)表视图样式和扩展视图-Table View Styles and Accessory Views
- UIViewController的跳转方式
- CodeForces 141B - Hopscotch
- poj 3624 最基本01背包问题
- 边看边写(判断一个序列是否为出栈序列)
- centos 修改系统时间
原创粉丝点击
热门IT博客
热门问题
老师的惩罚
人脸识别
我在镇武司摸鱼那些年
重生之率土为王
我在大康的咸鱼生活
盘龙之生命进化
天生仙种
凡人之先天五行
春回大明朝
姑娘不必设防,我是瞎子
dji大疆
大疆手机
大疆phantom4
大疆旗舰店
大疆 精灵4
大疆 航拍
大疆a2飞控
大疆多少钱
大疆 云台
dji大疆创新
大疆2
大疆ronin
大疆 s1000
大疆汪滔
大疆 待遇
大疆 论坛
大疆飞行模拟器
大疆航拍设备
大疆百旺招聘
西疆
大疆精灵2多少钱
大疆iosd
大疆待遇怎么样
大疆航拍云台
大疆osd
大疆公司地址
精灵3
无人飞机
dji
无人飞机价格
大白穴
水煮大白虾
大白
大白图片
刮大白
陈大白
大白乳
浮一大白
大白鹅
大白腿
刘大白