POJ 2251 Dungeon Master(三维简单广搜)
2016-05-05 18:13
330 查看
Dungeon Master
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 24931 Accepted: 9651
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?
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.
Output
Each maze generates one line of output. If it is possible to reach the exit, print a line of the form
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
Sample Output
Escaped in 11 minute(s).
Trapped!
本题是一个简单的三维广度优先搜索。
只要报细节问题处理好,AC不是问题。
下面是AC代码:
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 24931 Accepted: 9651
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?
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.
Output
Each maze generates one line of output. If it is possible to reach the exit, print a line of the form
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 5 S.... .###. .##.. ###.# ##### ##### ##.## ##... ##### ##### #.### ####E 1 3 3 S## #E# ### 0 0 0
Sample Output
Escaped in 11 minute(s).
Trapped!
本题是一个简单的三维广度优先搜索。
只要报细节问题处理好,AC不是问题。
下面是AC代码:
#include<cstdio> #include<cstring> #include<algorithm> #include<queue> using namespace std; struct node { int x,y,z,step; }; int dir[6][3]={{0,0,1},{0,0,-1},{0,1,0},{0,-1,0},{1,0,0},{-1,0,0}}; int n,m,r,sx,sy,sz,ex,ey,ez; char a[35][35][35]; int book[35][35][35]; int bfs(int sx,int sy,int sz,int ex,int ey,int ez) { queue<node>q; node fr,ne; fr.x=sx,fr.y=sy,fr.z=sz,fr.step=0; book[fr.x][fr.y][fr.z]=1; q.push(fr); while(!q.empty()) { fr=q.front(); q.pop(); if(fr.x==ex&&fr.y==ey&&fr.z==ez) { return fr.step; } for(int i=0;i<6;i++) { ne.x=fr.x+dir[i][0]; ne.y=fr.y+dir[i][1]; ne.z=fr.z+dir[i][2]; if(a[ne.x][ne.y][ne.z]!='#'&&ne.x>=0&&ne.x<n&&ne.y>=0&&ne.y<m&&ne.z>=0&&ne.z<r&&book[ne.x][ne.y][ne.z]==0) { book[ne.x][ne.y][ne.z]=1; ne.step=fr.step+1; q.push(ne); } } } return -1; } int main() { while(~scanf("%d%d%d",&n,&m,&r)) { if(n==0&&m==0&&r==0) { break; } memset(a,'\0',sizeof(a)); memset(book,0,sizeof(book)); for(int i=0;i<n;i++) { for(int j=0;j<m;j++) { scanf("%s",&a[i][j]); for(int k=0;k<r;k++) { if(a[i][j][k]=='S') { sx=i; sy=j; sz=k; } if(a[i][j][k]=='E') { ex=i; ey=j; ez=k; } } } } int re=bfs(sx,sy,sz,ex,ey,ez); if(re==-1) { printf("Trapped!\n"); } else { printf("Escaped in %d minute(s).\n",re); } } return 0; }
相关文章推荐
- Spark集群搭建与测试
- 添加Pods后,import无提示的解决办法
- 点分十进制IP校验、转换,掩码校验
- 如何调整Linux系统为正确时区
- u-boot移植2:支持 nandflash 的读写
- 提高scroll性能
- objective-C之NSTimer
- PHP面试(二)
- 职业生涯人物访谈作业格式
- Android 编程下 Touch 事件的分发和消费机制
- CString中find_first_of和find_last_of()函数
- Android EditText的设置
- android弱引用和软应用的详解
- 如何使用Markdown编辑器写博客
- Win10系统开启Linux Bash命令行
- 【排序算法】——冒泡排序算法
- Spark定制班第3课:通过案例对SparkStreaming 透彻理解三板斧之三:解密SparkStreaming运行机制和架构进阶之Job和容错
- apache的common项目如何导入eclipse中
- 整数分解成不同加数的最大乘积
- Linux常用命令英文全称与汉语解释Linux系统