您的位置:首页 > 其它

POJ——2251Dungeon Master(三维BFS)

2016-06-27 18:37 274 查看
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

;
int vis

;
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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  BFS poj