POJ 2251
发布日期:2021-06-30 15:31:13 浏览次数:2 分类:技术文章

本文共 3155 字,大约阅读时间需要 10 分钟。

Dungeon Master

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 50345   Accepted: 18954

Description

Youare 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 5S.....###..##..###.#############.####...###########.#######E1 3 3S###E####0 0 0

Sample Output

Escaped in 11 minute(s).Trapped!

Source

大致题意:

给出一三维空间的地牢,要求求出由字符’S’到字符’E’的最短路径

移动方向可以是上,下,左,右,前,后,六个方向

每移动一次就耗费一分钟,要求输出最快的走出时间。

不同L层的地图,相同RC坐标处是连通的

解题思路:

我越看这题就越觉得是 XX地下城 = = 水题一道,求最短路问题,直接BFS得了 开三维数组,

每次搜索方向由二维的4个方向增加到6个,但是方法还是那个方法 没难度

注意若果三维数组恰好开到极限的30*30*30是会RE的,别替人家电脑省空间,想AC就开大点。

值得一提的是。。。这题竟然被那篇经典的 POJ分类 文章归纳到DFS。。。网上发现几个同学还在郁闷地DFS。。。。 这里就提示一下大家,凡是看到求最短路,用DFS一定很难做出来,一定要BFS 

代码:

#include
#include
using namespace std;typedef class{ public: int l,r,c; int depth; //树深(分钟)}SE;SE s,e;bool maze[40][40][40];int shortminute;bool BFS(int k,int i,int j){ bool vist[40][40][40]={false}; SE queue[30000]; int head,tail; queue[head=0].l=k; queue[tail=0].r=i; queue[0].c=j; queue[tail++].depth=1; vist[k][i][j]=true; while(head
>L>>R>>C) { if(!L && !R && !C) break; memset(maze,false,sizeof(maze)); for(k=1;k<=L;k++) for(i=1;i<=R;i++) for(j=1;j<=C;j++) { char temp; cin>>temp; if(temp=='.') maze[k][i][j]=true; if(temp=='S') { maze[k][i][j]=true; s.l=k; s.r=i; s.c=j; } if(temp=='E') { maze[k][i][j]=true; e.l=k; e.r=i; e.c=j; } } if(BFS(s.l,s.r,s.c)) cout<<"Escaped in "<
<<" minute(s)."<

 

转载地址:https://joycez.blog.csdn.net/article/details/83386039 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:POJ 1416
下一篇:POJ 1426

发表评论

最新留言

哈哈,博客排版真的漂亮呢~
[***.90.31.176]2024年04月16日 11时43分11秒