POJ 2251 Dungeon Master ( BFS )
2016-07-28 17:51
323 查看
Dungeon Master
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
Source
Ulm Local 1997
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 26388 | Accepted: 10263 |
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!
Source
Ulm Local 1997
#include<stdio.h> #include<queue> #include<string.h> #include<algorithm> using namespace std; int dis[6][3]={{1,0,0},{-1,0,0},{0,1,0},{0,-1,0},{0,0,-1},{0,0,1}}; struct node { int x,y,z,cost; }p[33000]; int vis[35][35][35]; int map[35][35][35]; int ex,ey,ez,dx,dy,dz; int bfs(int k,int n,int m) { queue<node>q; node temp,f; temp.x=ex; temp.y=ey; temp.z=ez; temp.cost=0; q.push(temp); memset(vis,0,sizeof(vis)); vis[ez][ex][ey]=1; while(!q.empty()) { temp=q.front(); q.pop(); for(int i=0;i<6;i++) { int sx=temp.x+dis[i][0]; int sy=temp.y+dis[i][1]; int sz=temp.z+dis[i][2]; if(sx==dx&&sy==dy&&sz==dz) { return temp.cost+1; } if(sx>=0&&sx<n&&sy>=0&&sy<m&&sz>=0&&sz<k&&map[sz][sx][sy]!=1&&vis[sz][sx][sy]==0) { vis[sz][sx][sy]=1; f.x=sx; f.y=sy; f.z=sz; f.cost=temp.cost+1; q.push(f); } } } return -1; } int main() { int k,n,m,i,j,l; char str[45]; while(scanf("%d%d%d",&l,&n,&m),l+n+m) { for(k=0;k<l;k++) { for(i=0;i<n;i++) { scanf("%s",str); for(j=0;j<m;j++) { if(str[j]=='#') map[k][i][j]=1; else if(str[j]=='.') map[k][i][j]=0; else if(str[j]=='S') { ez=k; ex=i; ey=j; } else if(str[j]=='E') { dx=i; dy=j; dz=k; } } } } int ans=bfs(l,n,m); if(ans==-1) printf("Trapped!\n"); else printf("Escaped in %d minute(s).\n",ans); } return 0; }
相关文章推荐
- Android调用WEBSERVER发送信息到服务器
- 商业流程中的traversedpath
- C#对GZIP压缩和解压
- linux删除或隐藏命令历史记录history
- Java中return和finally的执行顺序
- 每日总结 2016年七月28日一个地方的异常会引发其他地方的异常
- Linux 内存管理:缺页异常的几种原因
- 因为多余jar包,所报的错
- PayPal-PHP-SDK(V1.7.4)支付接口实现
- bartender打印错误
- UIScrollView下拉实现图片放大或缩小
- 【分类】SVM算法学习
- 堆排序的Java代码实现
- Html简单介绍
- 【Java学习】java线程-多线程理解
- PB的message用法之message.stringparm
- 嘿,这将是全新的开始,挑战自己,加油!——hello,小凡
- poj1321棋盘问题DFS
- linux /etc/security/limits.conf的相关说明
- Android 读取CPU信息方法