POJ 2251:Dungeon Master
2015-07-31 15:27
549 查看
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
一个Test给你几个地图,给了你一个三维的模型,其实质就是之前人在格子里走路只能是上下左右四个方向,这回如果你在中间的地图里的话,又多了两个方向,就叫成前后吧,其实质是bfs,中规中矩的一个模式而已。
代码:
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 20687 | Accepted: 8004 |
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!
一个Test给你几个地图,给了你一个三维的模型,其实质就是之前人在格子里走路只能是上下左右四个方向,这回如果你在中间的地图里的话,又多了两个方向,就叫成前后吧,其实质是bfs,中规中矩的一个模式而已。
代码:
#include <iostream> #include <algorithm> #include <cmath> #include <vector> #include <string> #include <cstring> #include <queue> using namespace std; int L,R,C; char value[50][50][50]; int bushu[50][50][50]; int bfs(int i,int j,int k) { int flag=0; queue<int>x; queue<int>y; queue<int>z; x.push(i); y.push(j); z.push(k); while(x.size()) { int temp1=x.front(); int temp2=y.front(); int temp3=z.front(); x.pop(); y.pop(); z.pop(); if(temp1-1>=1&&value[temp1-1][temp2][temp3]=='.'&&!bushu[temp1-1][temp2][temp3]) { x.push(temp1-1); y.push(temp2); z.push(temp3); bushu[temp1-1][temp2][temp3]=bushu[temp1][temp2][temp3]+1; } if(temp1+1<=L&&value[temp1+1][temp2][temp3]=='.'&&!bushu[temp1+1][temp2][temp3]) { x.push(temp1+1); y.push(temp2); z.push(temp3); bushu[temp1+1][temp2][temp3]=bushu[temp1][temp2][temp3]+1; } if(temp2-1>=1&&value[temp1][temp2-1][temp3]=='.'&&!bushu[temp1][temp2-1][temp3]) { x.push(temp1); y.push(temp2-1); z.push(temp3); bushu[temp1][temp2-1][temp3]=bushu[temp1][temp2][temp3]+1; } if(temp2+1<=R&&value[temp1][temp2+1][temp3]=='.'&&!bushu[temp1][temp2+1][temp3]) { x.push(temp1); y.push(temp2+1); z.push(temp3); bushu[temp1][temp2+1][temp3]=bushu[temp1][temp2][temp3]+1; } if(temp3-1>=1&&value[temp1][temp2][temp3-1]=='.'&&!bushu[temp1][temp2][temp3-1]) { x.push(temp1); y.push(temp2); z.push(temp3-1); bushu[temp1][temp2][temp3-1]=bushu[temp1][temp2][temp3]+1; } if(temp3+1<=C&&value[temp1][temp2][temp3+1]=='.'&&!bushu[temp1][temp2][temp3+1]) { x.push(temp1); y.push(temp2); z.push(temp3+1); bushu[temp1][temp2][temp3+1]=bushu[temp1][temp2][temp3]+1; } if(value[temp1-1][temp2][temp3]=='E'||value[temp1+1][temp2][temp3]=='E'||value[temp1][temp2-1][temp3]=='E'|| value[temp1][temp2+1][temp3]=='E'||value[temp1][temp2][temp3-1]=='E'||value[temp1][temp2][temp3+1]=='E') { flag=1; return bushu[temp1][temp2][temp3]+1; } } return 0; } void solve() { int i,j,k; for(i=L;i>=1;i--) { for(j=R;j>=1;j--) { for(k=C;k>=1;k--) { if(value[i][j][k]=='S') { int temp=bfs(i,j,k); if(temp==0) cout<<"Trapped!"<<endl; else cout<<"Escaped in "<<temp<<" minute(s)."<<endl; return; } } } } } int main() { int i,j,k; while(scanf("%d%d%d",&L,&R,&C)) { if(L+R+C==0) break; for(i=1;i<=L;i++) { for(j=1;j<=R;j++) { scanf("%s",value[i][j]+1); } } memset(bushu,0,sizeof(bushu)); solve(); } return 0; }
相关文章推荐
- POI-----POI操作Excel-3、单元格
- MFC CtreeCtrl 节点的遍历
- DELPHI中自定义消息的发送和接收
- measure_circuit_width_lines_gauss.hdev检测pcb线宽 相关例程学习
- MySQL之查询性能优化二
- POJ 2251:Dungeon Master
- 常用博客地址搬家
- 手动编译安装LAMP架构,并且实现nagios图形化监控(八)
- list + radio 实现自定义单选
- PAT (Advanced Level) 1093. Count PAT's (25) 一维动态规划
- TCP/IP学习(四)TCP缓冲区大小及限制
- css3画圆
- HBase - Rowkey 设计 | 那伊抹微笑
- web.xml中load-on-startup的作用
- 椭圆拟合算法总结
- 项目管理经验总结
- Transfrom在64bit服务下面无法运行
- 第一次用IIS发布网站时遇到的两个问题
- requireJs详解(二)
- SpringMvc下载excel文件