Dungeon Master
2015-08-09 14:25
197 查看
Dungeon Master
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 20995 Accepted: 8150
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
where x is replaced by the shortest time it takes to escape.
If it is not possible to escape, print the line
Sample Input
3 4 5
S….
.###.
.##..
S##
Sample Output
Escaped in 11 minute(s).
Trapped!
Source
Ulm Local 1997
三维的搜索,开始少算了时间复杂度,用dfs超时,果断的改成bfs
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 20995 Accepted: 8150
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 3S##
E
#
0 0 0Sample Output
Escaped in 11 minute(s).
Trapped!
Source
Ulm Local 1997
三维的搜索,开始少算了时间复杂度,用dfs超时,果断的改成bfs
#include <map> #include <list> #include <cmath> #include <queue> #include <stack> #include <string> #include <cstdio> #include <cstring> #include <cstdlib> #include <iostream> #include <algorithm> using namespace std; #define eps 1e-9 #define LL long long #define PI acos(-1.0) #define INF 0x3f3f3f3f #define CRR fclose(stdin) #define CWW fclose(stdout) #define RR freopen("input.txt","r",stdin) #define WW freopen("output.txt","w",stdout) const int MAX = 10000; struct node { int x; int y; int z; }; struct point { int num; node a; }; int L,R,C; int Dir[][3]= {{0,1,0},{0,-1,0},{1,0,0},{-1,0,0},{0,0,1},{0,0,-1}}; char Map[35][35][35]; node s,e; bool vis[35][35][35]; int Max; bool Judge(int x,int y,int z) { if(x<R&&x>=0&&y<C&&y>=0&&z<L&&z>=0&&vis[z][x][y]==false&&Map[z][x][y]!='#') { return true; } return false; } void bfs() { point b,c; queue<point>Q; memset(vis,false,sizeof(vis)); b.a.x=s.x; b.a.y=s.y; b.a.z=s.z; b.num=0; vis[s.z][s.x][s.y]=true; Q.push(b); while(!Q.empty()) { c=Q.front(); Q.pop(); if(c.a.x==e.x&&c.a.y==e.y&&c.a.z==e.z) { Max=c.num; return ; } for(int i=0;i<6;i++) { b.a.x=c.a.x+Dir[i][0]; b.a.y=c.a.y+Dir[i][1]; b.a.z=c.a.z+Dir[i][2]; b.num=c.num+1; if(Judge(b.a.x,b.a.y,b.a.z)) { vis[b.a.z][b.a.x][b.a.y]=true; Q.push(b); } } } } int main() { while(~scanf("%d %d %d",&L,&R,&C)) { if(L==0&&R==0&&C==0) { break ; } for(int i=0; i<L; i++) { for(int j=0; j<R; j++) { scanf("%s",Map[i][j]); for(int k=0; k<C; k++) { if(Map[i][j][k]=='S') { s.x=j; s.y=k; s.z=i; } if(Map[i][j][k]=='E') { e.x=j; e.y=k; e.z=i; } } } } Max = INF; bfs(); if(Max<INF) { printf("Escaped in %d minute(s).\n",Max); } else printf("Trapped!\n"); } return 0; }
相关文章推荐
- Dungeon Master 分类: 搜索 POJ 2015-08-09 14:25 4人阅读 评论(0) 收藏
- Androd WebView中的图片浏览及缩放
- 19 擅长排列的小明【dfs】
- oc中的工厂设计模式
- Spring基于自定义注解的用户认证
- Spring实现定时任务
- 线程小结
- Android中使用友盟时用Ant进行多渠道打包及相关问题
- block深入学习
- 尝试使用枚举类存储静态变量
- 如何在需求不断改变的情况下愉快的写代码
- Dexpress 中 grid的使用
- Services
- 最短路专题-1001 Constructing Roads
- Mysql常用函数
- 浅析Java中的final关键字
- HDU5353.Average(多校第六场)
- HDU - 1702 - ACboy needs your help again!(栈&&队列)
- 并查集详解
- HUD Problem - 1251 统计难题