Dungeon Master
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 25379   Accepted: 9856

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!

以前看这题很多次,都是不敢做,今天感觉有点灵感,毕竟SPFA刷了挺多的水题+图的遍历还是会一点的,因此我又来了,以前没做另外一个原因是智障地以为结构体自带了重载运算符号,结果一编译卧槽?怎么一大堆错。然后就不想搞了。最近机智了一点还是自己写点结构体重载吧。对于我这种DP和搜索不行的人来说1A还是不错的。果然BFS一般比DFS简单……结构体多加一个步数,这样就可以方便得得到最后的步数了。这个方法在BFS里感觉很好用。

代码:

#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<sstream>
#include<cstring>
#include<cstdio>
#include<string>
#include<deque>
#include<stack>
#include<cmath>
#include<queue>
#include<set>
#include<map>
using namespace std;
#define INF 0x3f3f3f3f
#define MM(x) memset(x,0,sizeof(x))
#define MMINF(x) memset(x,INF,sizeof(x))
typedef long long LL;
const double PI=acos(-1.0);
int l,n,m;
const int N=50;
struct info
{
int x;
int y;
int z;
int step;
};
info S,E;
info direct[6]={{1,0,0,1},{-1,0,0,1},{0,1,0,1},{0,-1,0,1},{0,0,1,1},{0,0,-1,1}};//方向数组
char pos[N][N][N];
int vis[N][N][N];
info operator+(const info &a,const info &b)
{
info c;
c.x=a.x+b.x;
c.y=a.y+b.y;
c.z=a.z+b.z;
c.step=a.step+b.step;
return c;
}
bool operator==(const info &a,const info &b)
{
return (a.x==b.x&&a.y==b.y&&a.z==b.z);
}
inline bool check(const info &Q)
{
if((pos[Q.x][Q.y][Q.z]=='.'||pos[Q.x][Q.y][Q.z]=='E')&&(!vis[Q.x][Q.y][Q.z]))//这里要记得算上终点
return true;
return false;
}
int main(void)
{
int i,j,k;
while (~scanf("%d%d%d",&l,&n,&m)&&(l||n||m))
{
S.step=0;
MM(vis);
MM(pos);
for (i=0; i<l; i++)
{
for (j=0; j<n; j++)
{
for (k=0; k<m; k++)
{
cin>>pos[i][j][k];//cin会略过空格和回车,这样比较方便判断S和E的位置。
if(pos[i][j][k]=='S')
{
S.x=i;
S.y=j;
S.z=k;
}
else if(pos[i][j][k]=='E')
{
E.x=i;
E.y=j;
E.z=k;
}
}
}
}
int r=-1;
queue<info>Q;
Q.push(S);
vis[S.x][S.y][S.z]=1;
while (!Q.empty())
{
info now=Q.front();
Q.pop();
if(now==E)
{
r=now.step;//步数给答案
break;
}
for (i=0; i<6; i++)
{
info v=now+direct[i];
if(check(v))
{
vis[v.x][v.y][v.z]=1;
Q.push(v);
}
}
}
r==-1?puts("Trapped!"):printf("Escaped in %d minute(s).\n",r);
}
return 0;
}

POJ——2251Dungeon Master(三维BFS)的更多相关文章

  1. POJ:Dungeon Master(三维bfs模板题)

    Dungeon Master Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 16748   Accepted: 6522 D ...

  2. POJ 2251 Dungeon Master --- 三维BFS(用BFS求最短路)

    POJ 2251 题目大意: 给出一三维空间的地牢,要求求出由字符'S'到字符'E'的最短路径,移动方向可以是上,下,左,右,前,后,六个方向,每移动一次就耗费一分钟,要求输出最快的走出时间.不同L层 ...

  3. POJ 2251 Dungeon Master (三维BFS)

    题目链接:http://poj.org/problem?id=2251 Dungeon Master Time Limit: 1000MS   Memory Limit: 65536K Total S ...

  4. ZOJ 1940 Dungeon Master 三维BFS

    Dungeon Master Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Desc ...

  5. POJ 2251-Dungeon Master (三维空间求最短路径)

    Description You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is co ...

  6. Dungeon Master(三维bfs)

    You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is composed of un ...

  7. POJ.2251 Dungeon Master (三维BFS)

    POJ.2251 Dungeon Master (三维BFS) 题意分析 你被困在一个3D地牢中且继续寻找最短路径逃生.地牢由立方体单位构成,立方体中不定会充满岩石.向上下前后左右移动一个单位需要一分 ...

  8. POJ 2049— Finding Nemo(三维BFS)10/200

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/u013497151/article/details/29562915 海底总动员.... 这个题開始 ...

  9. hdu 1240:Asteroids!(三维BFS搜索)

    Asteroids! Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total ...

随机推荐

  1. iphone之打开pdf、doc、xls文件用UIWebView

    //文件名字及类型 NSString *path=[[NSBundle mainBundle]pathForResource:@"xls1" ofType:@"xls&q ...

  2. Java中常见编码格式及乱码解决方法

    一:设置编码格式 1.JSP文件 charset=UTF-8 的作用是指定JSP向客户端输出的编码方式为"UTF-8",pageEncoding="UTF-8" ...

  3. java代码(ascii与字母互转)

    package test; /** * Java中将一个字符与对应Ascii码互转 * 1 byte = 8bit 可以表示 0-127 */ public class GetCharAscii { ...

  4. Linux Device Driver 学习(1)

    Linux Device Driver 学习(1) 一.搭建虚拟机开发环境 1.选择虚拟机VirtualBox,官网下载.deb包安装: VirtualBox Linux 5.1.6 下载fedora ...

  5. STL:string类中size()与length()的区别

    结论是:两者没有任何区别 解释: C++Reference中对于两者的解释: 两者的具体解释都一模一样: 理解: length是因为C语言的习惯而保留下来的,string类最初只有length,引进S ...

  6. Java中System.setProperty()

    Java中System.setProperty()用法 <转抄> // Daysafter :Integer中 getInteger( String s); getInteger( Str ...

  7. Evaluate|GC content|Phred|BAC|heterozygous single nucleotide polymorphisms|estimate genome size|

    (Evaluate):检查reads,可使用比对软件:使用SOAPaligner重新排列:采用massively parallel next-generation sequencing technol ...

  8. springmvc导出excel(POI)

    /** * 导出excel表格 */ @RequestMapping(value = "/doExportData", method = {RequestMethod.POST, ...

  9. viewDidLoad、viewWillAppear、viewWillDisappear

    - (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil viewDidLo ...

  10. React初识整理(二)--生命周期的方法

    React生命周期主要有7中: 1. componentWillMount() :组件将要挂载时触发 ,只调用1次 2. componentDidMount() :组件挂载完成时触发,只调用1次 3. ...