poj2251 Dungeon Master
来源:互联网 发布:巴基斯坦核试验数据 编辑:程序博客网 时间:2024/04/26 03:47
链接:http://poj.org/problem?id=2251
三维的搜索题,之前也没写过。用BFS。借鉴了网上的代码,写的不错,拿来用一下。这个题目的输入很有考验性,然后是三维的BFS的应用,都值得学习。
//又犯了一个低级错误,变量c1和c一开始重名了#include<iostream>#include<cstring>using namespace std;int maze[35][35][35];//三维的矩形空间int vist[35][35][35];//记录是否被访问int dis[35][35][35];//记录消耗的时间(走过的距离)int q[30000];//队列,用数组实现int l,r,c;int dx[]={1,-1,0,0,0,0};//一共是6个方向int dy[]={0,0,0,0,-1,1};int dz[]={0,0,-1,1,0,0};int BFS(int x,int y,int z){int front=0,rear=0,d,u;//头指针和尾指针都为0vist[x][y][z]=1;//起始点,被访问dis[x][y][z]=0;//初始走过的距离为0u=x*r*c+y*c+z;//这个方法我觉得很棒,以前从没见过q[rear++]=u;//起始点入队while(front<rear){ u=q[front++];//队头出队 x=u/(r*c); y=(u%(r*c))/c; z=(u%(r*c))%c;//得到x、y、z for(d=0;d<6;d++)//6个方向 {int nx=x+dx[d];int ny=y+dy[d];int nz=z+dz[d];if(nx>=0&&nx<l&&ny>=0&&ny<r&&nz>=0&&nz<c&&maze[nx][ny][nz]&&!vist[nx][ny][nz]) //如果没有越界且这个点可通行且未被访问,则入队{ int v=nx*r*c+ny*c+nz; q[rear++]=v; //入队 vist[nx][ny][nz]=1; //标记,已访问 dis[nx][ny][nz]=dis[x][y][z]+1; //距离加1 if(maze[nx][ny][nz]==2) //到达E点 return dis[nx][ny][nz];} }}return 0; //不能到达,返回零}int main(){int i,j,k;int a,b,c1,time;char s[40];while(cin>>l>>r>>c){ if(!l&&!r&&!c) return 0; getchar(); memset(maze,0,sizeof(maze)); memset(vist,0,sizeof(vist)); for(i=0;i<l;i++) { for(j=0;j<r;j++) { gets(s); for(k=0;k<c;k++) { if(s[k]=='S'){ a=i; b=j; c1=k;}else if(s[k]=='.')maze[i][j][k]=1;else if(s[k]=='E')maze[i][j][k]=2; } } getchar(); } time=BFS(a,b,c1); if(time) cout<<"Escaped in "<<time<<" minute(s)."<<endl; else cout<<"Trapped!"<<endl;} return 0;}
0 0
- POJ2251 Dungeon Master
- POJ2251 Dungeon Master
- poj2251 Dungeon Master (BFS)
- poj2251 - Dungeon Master
- poj2251 Dungeon Master
- POJ2251:Dungeon Master
- Dungeon Master(poj2251,bfs)
- Dungeon Master(poj2251)
- poj2251 Dungeon Master
- POJ2251:Dungeon Master(BFS)
- poj2251 Dungeon Master bfs
- POJ2251:Dungeon Master(BFS)
- poj2251 Dungeon Master(BFS)
- POJ2251 Dungeon Master BFS
- poj2251 Dungeon Master
- POJ2251---Dungeon Master
- [bfs]poj2251 Dungeon Master
- POJ2251----Dungeon Master
- 寒假训练--dfs+bfs--找朋友
- 守护进程
- 【ios游戏开发】之UDID和OpenUDID扫盲
- 面向对象编程风格 VS 基于对象编程风格(boost::bind/function)
- 设计模式- 主动对象(Active Object)
- poj2251 Dungeon Master
- Visual C++ 技巧:如何合并多个静态库(*.lib)
- ural 1 Simple Calculations 题解
- 好的博客汇总
- Mybatis使用多个参数查询
- NYOJ 20 吝啬的国度 (搜索)
- Hadoop专业解决方案-第12章 为Hadoop应用构建企业级的安全解决方案
- 寒假训练---训练赛2--Fighting
- a和&a的区别