您的位置:首页 > 其它

[POJ] 2251 Dungeon Master

2017-11-18 21:49 225 查看
Dungeon Master Time Limit: 1000MS Memory Limit: 65536K Total

Submissions: 38847 Accepted: 14802 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! Source

Ulm Local 1997

注意几点:

搜索是从S开始而不是1,1,1

多组输入要memset一下

因为方向问题调了好久。。x,y,z一视同仁就挺好

确实加深了对bfs的认识

/* Writer:GhostCai && His Yellow Duck */
#include <cstdio>
#include <iostream>
#include <cstring>
#include <queue>
#define MAXN 31
using namespace std;

bool flag;

struct point {
int x, y, z;
} node, r;

char    a[MAXN][MAXN][MAXN];
bool    vis[MAXN][MAXN][MAXN];
point   pre[MAXN][MAXN][MAXN];
int h, m, n;
int mx[6] = { 1, 0, -1, 0, 0, 0 };
int my[6] = { 0, 1, 0, -1, 0, 0 };
int mz[6] = { 0, 0, 0, 0, 1, -1 };
void print( point x )
{
flag = 1;
int cnt = 0;
point   u   = pre[x.x][x.y][x.z];
while ( u.x != 0 && u.y != 0 && u.z != 0 )
{
u = pre[u.x][u.y][u.z];
cnt++;
}
printf( "Escaped in %d minute(s).\n", cnt ); /* \n! */
return;
}

void bfs( int x, int y, int z )
{
node.x      = 0;
node.y      = 0;
node.z      = 0;
vis[x][y][z]    = 1;
pre[x][y][z]    = node;
node.x      = x; node.y = y; node.z = z;
queue<point> Q;
Q.push( node );
while ( !Q.empty() && !flag )
{
r = Q.front();
Q.pop();
int i, j;
int nx, ny, nz;
for ( i = 0; i <= 5; i++ )
{
nx  = r.x + mx[i];
ny  = r.y + my[i];
nz  = r.z + mz[i];
if ( nx < 1 || nx > m || ny < 1 || ny > n || nz < 1 || nz > h )
continue;
if ( a[nx][ny][nz] == '#' || vis[nx][ny][nz] )
continue;
/*              cout<<nx<<" "<<ny<<" "<<nz<<endl; */
node.x  = nx;
node.y  = ny;
node.z  = nz;
Q.push( node );
pre[nx][ny][nz] = r;
vis[nx][ny][nz] = 1;
if ( a[nx][ny][nz] == 'E' )
{
/* cout<<"SUCCESS"; */
print( node );
}
}
}
}

int main()
{
while ( cin >> h >> m >> n )
{
int sx, sy, sz;
if ( h == 0 && m == 0 && n == 0 )
return(0);
flag = 0;
memset( a, 0, sizeof(a) );
memset( vis, 0, sizeof(vis) );
memset( pre, 0, sizeof(pre) );
int i, j, k;
for ( i = 1; i <= h; i++ )
{
for ( j = 1; j <= m; j++ )
{
for ( k = 1; k <= n; k++ )
{
cin >> a[j][k][i]; /* -------------- */
if ( a[j][k][i] == 'S' )
{
sx  = j;
sy  = k;
sz  = i;
}
}
}
}
bfs( sx, sy, sz );
if ( !flag )
cout << "Trapped!\n";
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  poj 宽搜