您的位置:首页 > 其它

UVA 532 & POJ 2251 Dungeon Master (BFS)

2014-05-21 18:48 330 查看
http://poj.org/problem?id=2251

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=105&page=show_problem&problem=473

Dungeon Master

Time Limit: 1000MSMemory Limit: 65536K
Total Submissions: 15809Accepted: 6138
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
给出一个三维(L×R×C)的地图,求出从S到E的最少步数,其中'#' 是障碍,'.'可走。

直接BFS,注意标记访问

#include<cstdio>
#include<iostream>
#include<cstdlib>
#include<algorithm>
#include<ctime>
#include<cctype>
#include<cmath>
#include<string>
#include<cstring>
#include<stack>
#include<queue>
#include<list>
#include<vector>
#include<map>
#include<set>
#define sqr(x) ((x)*(x))
#define LL long long
#define INF 0x3f3f3f3f
#define PI 3.1415926535897932384626
#define eps 1e-10
#define mm

using namespace std;

int vis[33][33][33];
int fx,fy,fz,tx,ty,tz;
int L,R,C;
int f,r;

int dx[]={0,0,0,0,1,-1};
int dy[]={0,0,1,-1,0,0};
int dz[]={1,-1,0,0,0,0};

struct record
{
    int x,y,z,t;
    record(int xx=0,int yy=0,int zz=0,int tt=0)
    {
        x=xx;y=yy;z=zz;t=tt;
    }
    record(const record &r)
    {
        x=r.x;y=r.y;z=r.z;t=r.t;
    }
}q[33333];

int BFS()
{
    while (f<=r)
    {
        record n(q[f++]);

        //printf("%d %d %d %d\n",n.x,n.y,n.z,n.t);

        if (n.x==tx && n.y==ty && n.z==tz)   return n.t;

        int x,y,z;

        for (int i=0;i<6;i++)
        {
            x=n.x+dx[i];
            y=n.y+dy[i];
            z=n.z+dz[i];

            if (vis[x][y][z]!=-1)
            {
                vis[x][y][z]=-1;
                q[++r]=record(x,y,z,n.t+1);
            }
        }
    }

    return -1;
}

int main()
{
    #ifndef ONLINE_JUDGE
        freopen("/home/fcbruce/文档/code/t","r",stdin);
    #endif // ONLINE_JUDGE

    while (scanf("%d%d%d",&L,&R,&C),L||R||C)
    {
        char c;
        memset(vis,-1,sizeof(vis));
        for (int i=1;i<=L;i++)
        {
            getchar();
            for (int j=1;j<=R;j++)
            {
                for (int k=1;k<=C;k++)
                {
                    c=getchar();
                    if (c=='#') continue;
                    vis[i][j][k]=0;
                    if (c=='S')
                    {
                        fx=i;fy=j;fz=k;
                    }
                    if (c=='E')
                    {
                        tx=i;ty=j;tz=k;
                    }
                }
                getchar();
            }
        }

        //printf("%d %d %d %d\n",fx,fy,fz,0);

        q[f=r=0]=record(fx,fy,fz,0);
        vis[fx][fy][fz]=-1;
        int k=BFS();
        if (~k)
        {
            printf("Escaped in %d minute(s).\n",k);
        }
        else
        {
            printf("Trapped!\n");
        }

    }

    return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: