Dungeon Master--POJ 2251
2010-08-20 21:20
351 查看
1、题目类型:模拟、迷宫、BFS。
2、解题思路:BFS的三维Maze[][][]应用,其每步存在前后、左右、上下6个方向的选择。
3、注意事项:BFS入队列的条件判断。
4、实现方法:
2、解题思路:BFS的三维Maze[][][]应用,其每步存在前后、左右、上下6个方向的选择。
3、注意事项:BFS入队列的条件判断。
4、实现方法:
#include<iostream> #include<queue> using namespace std; struct Point { int x,y,z; int step; }; Point start,end; int dir[6][3]={{-1,0,0},{1,0,0},{0,-1,0}, {0,1,0},{0,0,-1},{0,0,1}}; int x,y,z; char Maze[50][50][50]; void Init() { int i,j,k; for(i=0;i<x;i++) { for(j=0;j<y;j++) { for(k=0;k<z;k++) { cin>>Maze[i][j][k]; if(Maze[i][j][k]=='S') { start.x=i; start.y=j; start.z=k; } if(Maze[i][j][k]=='E') { end.x=i; end.y=j; end.z=k; } } } } } void BFS() { queue<Point> Q; int vis[50][50][50]; memset(vis,0,sizeof(vis)); start.step=0; Q.push(start); vis[start.x][start.y][start.z]=1; while(!Q.empty()) { Point p=Q.front(); Q.pop(); if(p.x==end.x&&p.y==end.y&&p.z==end.z) { cout<<"Escaped in "<<p.step<<" minute(s)."<<endl; return ; } for(int i=0;i<6;i++) { int x1,y1,z1; x1=p.x+dir[i][0]; y1=p.y+dir[i][1]; z1=p.z+dir[i][2]; if(x1>=0&&x1<x&&y1>=0&&y1<y&&z1>=0&&z1<z&&!vis[x1][y1][z1]&&Maze[x1][y1][z1]!='#') { Point tmp; tmp.x=x1; tmp.y=y1; tmp.z=z1; tmp.step=p.step+1; vis[x1][y1][z1]=1; Q.push(tmp); } } } cout<<"Trapped!"<<endl; return ; } int main() { while(cin>>x>>y>>z) { if(x==0&&y==0&&z==0) break; Init(); BFS(); } return 0; }
相关文章推荐
- poj2251 Dungeon Master bfs
- poj 2251 Dungeon Master(bfs)
- POJ 2251 Dungeon Master bfs 难度:0
- POJ 2251 Dungeon Master BFS
- poj 2251:Dungeon Master
- Dungeon Master POJ 2251
- POJ - 2251 Dungeon Master BFS
- [POJ](2251)Dungeon Master ---三维bfs
- Dungeon Master&&http://poj.org/problem?id=2251
- Dungeon Master(POJ--2251
- Dungeon Master poj 2251
- Dungeon Master POJ - 2251 BFS
- POJ-2251 Dungeon Master bfs搜索
- POJ 2251 Dungeon Master /UVA 532 Dungeon Master / ZOJ 1940 Dungeon Master(广度优先搜索)
- B - Dungeon Master POJ - 2251(广搜三维)
- POJ 2251 Dungeon Master bfs
- Dungeon Master poj 2251 dfs
- B - Dungeon Master POJ - 2251
- Dungeon Master POJ - 2251
- B - Dungeon Master POJ - 2251