[POJ](2251)Dungeon Master ---三维bfs
来源:互联网 发布:花都金蝶软件代理商 编辑:程序博客网 时间:2024/05/23 07:24
Dungeon Master
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 37721 Accepted: 14415
Description
You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is composed of unit cubes which may or may not be filled with rock. It takes one minute to move one unit north, south, east, west, up or down. You cannot move diagonally and the maze is surrounded by solid rock on all sides.
Is an escape possible? If yes, how long will it take?
Is an escape possible? If yes, how long will it take?
Input
The input consists of a number of dungeons. Each dungeon description starts with a line containing three integers L, R and C (all limited to 30 in size).
L is the number of levels making up the dungeon.
R and C are the number of rows and columns making up the plan of each level.
Then there will follow L blocks of R lines each containing C characters. Each character describes one cell of the dungeon. A cell full of rock is indicated by a '#' and empty cells are represented by a '.'. Your starting position is indicated by 'S' and the exit by the letter 'E'. There's a single blank line after each level. Input is terminated by three zeroes for L, R and C.
L is the number of levels making up the dungeon.
R and C are the number of rows and columns making up the plan of each level.
Then there will follow L blocks of R lines each containing C characters. Each character describes one cell of the dungeon. A cell full of rock is indicated by a '#' and empty cells are represented by a '.'. Your starting position is indicated by 'S' and the exit by the letter 'E'. There's a single blank line after each level. Input is terminated by three zeroes for L, R and C.
Output
Each maze generates one line of output. If it is possible to reach the exit, print a line of the form
where x is replaced by the shortest time it takes to escape.
If it is not possible to escape, print the line
Escaped in x minute(s).
where x is replaced by the shortest time it takes to escape.
If it is not possible to escape, print the line
Trapped!
Sample Input
3 4 5S.....###..##..###.#############.####...###########.#######E1 3 3S###E####0 0 0
Sample Output
Escaped in 11 minute(s).Trapped!
解题新知:这是一个三维的bfs题,就是在建立搜索模型的时候将它建成三维的就可以。
AC代码:
#include<iostream>#include<cstdio>#include<queue>#include<cstring>using namespace std;typedef struct Node{ int x; int y; int z; int step;}node;node start,now,next;int dir[6][3] = {{0,0,-1},{0,0,1},{0,1,0},{0,-1,0},{-1,0,0},{1,0,0}};char mmap[35][35][35];int vis[35][35][35];int ans;int xx;int yy;int zz;queue<node>q;bool check(){ if(mmap[next.x][next.y][next.z]!='#'&&next.x>=0 && next.x<xx && next.y>=0 && next.y <yy && next.z>=0 && next.z<zz && !vis[next.x][next.y][next.z]) return true; else return false;}void bfs(){ q.push(start); while(!q.empty()) { now = q.front(); q.pop(); if(mmap[now.x][now.y][now.z] == 'E') { ans = now.step; return; } for(int 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(check()) { vis[next.x][next.y][next.z] = 1; next.step = now.step+1; q.push(next); } } }}int main(){ while(~scanf("%d %d %d",&xx,&yy,&zz)&&xx&&yy&&zz) { while(!q.empty()) q.pop(); ans = 0; memset(mmap,0,sizeof(mmap)); memset(vis,0,sizeof(vis)); for(int i=0;i<xx;i++) { for(int j=0;j<yy;j++) { scanf("%s",mmap[i][j]); for(int k=0;k<zz;k++) { if(mmap[i][j][k] == 'S') { start.x = i; start.y = j; start.z = k; start.step = 0; } } } } bfs(); if(ans!=0) printf("Escaped in %d minute(s).\n",ans); else printf("Trapped!\n"); } return 0;}
阅读全文
0 0
- POJ 2251 Dungeon Master (三维BFS)
- POJ 2251:Dungeon Master(三维BFS)
- POJ 2251 Dungeon Master (三维迷宫 BFS)
- POJ 2251 Dungeon Master(三维BFS)
- poj 2251 Dungeon Master 三维bfs
- poj 2251 Dungeon Master(三维BFS)(中等)
- poj 2251 Dungeon Master(BFS三维)
- POJ 2251 Dungeon Master(三维bfs)
- POJ 2251 Dungeon Master(三维bfs)
- <三维BFS搜索> POJ 2251 Dungeon Master
- POJ 2251:Dungeon Master(三维BFS)
- POJ 2251 Dungeon Master(三维BFS)
- POJ 2251 Dungeon Master 三维BFS
- POJ 2251 Dungeon Master(三维bfs)
- POJ 2251 Dungeon Master (三维BFS)
- POJ 2251(WA)Dungeon Master(三维BFS)
- POJ.2251 Dungeon Master (三维BFS)
- poj 2251Dungeon Master(三维bfs)
- Code Composer Studio 7.3.0安装错误。
- Manthan, Codefest 17-E-Salazar Slytherin's Locket(数位DP)
- 八大排序算法
- 仿函数小例子
- 慕课网bootstrap一
- [POJ](2251)Dungeon Master ---三维bfs
- 机器学习中的Bias,Error,Variance的区别
- hdu 2489 Minimal Ratio Tree(prim+dfs)
- android Messenger跨进程通信
- Scroller实现View弹性滑动
- Android dp方式的屏幕适配工具使用(bat批处理方式)
- Java:HashMap、HashTable
- C++的STL之仿函数简单介绍
- 从主函数输入十个不等长的字符串,编写函数,对这些串进行排序,在主调函数中输出排好序的串,指针实现。