POJ2251 Dungeon Master 三维BFS
来源:互联网 发布:鞍山驱逐舰数据 编辑:程序博客网 时间:2024/04/29 08:03
题目大意:在一个三维迷宫中,问你从起点到终点的最小步数。
分析:和HDU1253差不多,简单的三维BFS。
实现代码:
#include <cstdio>#include <iostream>#include <cstring>using namespace std;int dir[6][3]={{1,0,0},{0,1,0},{0,0,1},{-1,0,0},{0,-1,0},{0,0,-1}};int map[55][55][55];bool s[55][55][55];int a,b,c;int cx,cy,cz;int sx,sy,sz,ex,ey,ez;typedef struct node{ int x,y,z,tmp;}point;point que[51*51*51];bool go(int x,int y,int z){ if(x<=0||x>a||y<=0||y>b||z<=0||z>c) return false; else return true;}void bfs(int sx,int sy,int sz){ int x,y,z; int cmp1=1,cmp2=1; que[cmp1].x=sx; que[cmp1].y=sy; que[cmp1].z=sz; que[cmp1].tmp=0; s[sx][sy][sz]=true; while(cmp1<=cmp2&&!s[ex][ey][ez]) { x=que[cmp1].x; y=que[cmp1].y; z=que[cmp1].z; for(int i=0;i<6;i++) if(map[x+dir[i][0]][y+dir[i][1]][z+dir[i][2]]) { cx=x+dir[i][0]; cy=y+dir[i][1]; cz=z+dir[i][2]; if(go(cx,cy,cz)&&!s[cx][cy][cz]) { cmp2++; que[cmp2].x=cx; que[cmp2].y=cy; que[cmp2].z=cz; que[cmp2].tmp=que[cmp1].tmp+1; s[cx][cy][cz]=true; } } cmp1++; } if(cmp1>cmp2) puts("Trapped!"); else printf("Escaped in %d minute(s).\n",que[cmp2].tmp);}int main(){ int cas,i,j,k; char tmp; while(scanf("%d%d%d",&a,&b,&c)) { if(a==0&&b==0&&c==0) break; memset(map,1,sizeof(map)); for(i=1;i<=a;i++) for(j=1;j<=b;j++) for(k=1;k<=c;k++) { cin>>tmp; if(tmp=='#') map[i][j][k]=0; if(tmp=='S'){sx=i;sy=j;sz=k;} if(tmp=='E'){ex=i;ey=j;ez=k;} } memset(s,false,sizeof(s)); bfs(sx,sy,sz); } return 0;}
0 0
- POJ2251 Dungeon Master 三维BFS
- 三维BFS poj2251 Dungeon Master
- POJ2251-Dungeon Master (三维BFS)
- poj2251 Dungeon Master(三维bfs)
- poj2251 Dungeon Master (BFS)
- Dungeon Master(poj2251,bfs)
- POJ2251:Dungeon Master(BFS)
- poj2251 Dungeon Master bfs
- POJ2251:Dungeon Master(BFS)
- poj2251 Dungeon Master(BFS)
- POJ2251 Dungeon Master BFS
- [bfs]poj2251 Dungeon Master
- poj2251 dungeon master【BFS】~
- POJ2251 Dungeon Master(bfs)
- POJ2251 Dungeon Master(BFS)
- POJ2251-Dungeon Master-BFS
- 【POJ2251】Dungeon Master(bfs)
- poj2251-bfs- Dungeon Master
- C++之纯虚函数和抽象类
- C++ 二维动态数组的声明与删除
- 面试题7 用两个栈实现队列
- ios开发-OC中ARC、block和protocol的学习
- 算法学习 - 拼接成最大的数字
- POJ2251 Dungeon Master 三维BFS
- Spring第一天の代码学习(Spring底层依赖注入和控制反转的小例子)
- MVC入门介绍
- volatile有什么作用
- 系统IO 编写copy程序 简析对文件的操作
- 基础算法总结之归并排序
- 第一章 环境搭建
- 汇编语言 INT 21H功能
- Java内部类详解