台州学院ACM:1748: Dungeon Master

1748: Dungeon Master 

时间限制(普通/Java):1000MS/10000MS     内存限制:65536KByte
总提交: 212            测试通过:125

描述

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 movediagonally and the maze is surrounded by solid rock on all sides.  

Is an escape possible? If yes, how long will it take?  

输入

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.

输出

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!

样例输入

3 4 5
S....
.###.
.##..
###.#

#####
#####
##.##
##...

#####
#####
#.###
####E

1 3 3
S##
#E#
###

0 0 0

样例输出

Escaped in 11 minute(s).
Trapped!

题目来源

ULM 1997

题目意思:有一个三维的迷宫,这个迷宫是立体的,有一个入口,有一个出口,有的地方是空地,有的地方放有阻碍前行的大石头,有大石头的地方不能通行,在一个地方只能从他的上下,左右,前后,六个方向行走,每走一格都要花费一秒的时间,求出迷宫的最短时间,如果没有路可以离开迷宫,则输出被困了。否则输出离开迷宫的最短时间。

解题思路:

这是一道广搜题目。

题目代码:

#include<iostream>
#include<string.h>
#include<stdio.h>
#include<queue>
using namespace std;
char cmap[31][31][31];  //迷宫规模,因为长宽高都不会大于30,所以开一个31的三维数组
int dir[6][3]={{-1,0,0},{0,1,0},{1,0,0},{0,-1,0},{0,0,1},{0,0,-1}};  //三维,可以走的六个方向
int row,line,height;   //定义长,宽,高为全局变量
typedef struct   
{
    int x,y,z,time; //横坐标,纵坐标,竖坐标,走到当前位置的时间
}position;  //结构体类型,当前位置的横坐标,纵坐标,竖坐标,以及从入口走到当前位置的时间
position start,eend;   //两个结构体变量,起点,终点
int bfs()  //用广搜,返回值为整型,有解时返回值为最短时间,无解时,返回值为-1
{
    int i;  //循环变量i
    position cur,per;  //两个结构体变量,上次位置和当前位置
    cmap[start.x][start.y][start.z]='#';  //将初始位置置为'#',代表这个位置已经走过了
    start.time=0;                //初始时用的时间置为0
    queue<position>Q;            //定义一个队列,为结构体类型,名字叫做Q
    Q.push(start);             //最初让起点入队,以起点为中心进行广搜
    while(!Q.empty())        //如果队列不是空的,就一直执行循环体
    {
        per=Q.front();    //per为上次位置,取队首元素,就是刚才刚进队的元素
        Q.pop();        //让这个位置出对
        if(per.x==eend.x&&per.y==eend.y&&per.z==eend.z) //如果这个位置就是出口,直接返回从出口走到这个位置所用的时间
            return per.time;
        for(i=0;i<6;i++)   //如果这个位置不是出口,就要以他为中心进行六个方向的搜索
        {
            cur.x=per.x+dir[i][0];   //新的横坐标
            cur.y=per.y+dir[i][1];   //新的纵坐标
            cur.z=per.z+dir[i][2];   //新的竖坐标
            if(cur.x<0||cur.x>row-1||cur.y<0||cur.y>line-1||cur.z<0||cur.z>height-1||cmap[cur.x][cur.y][cur.z]=='#')  //如果这个位置越界,或这个位置有阻碍前行的大石头,直接结束本次循环,
                continue;
            cmap[cur.x][cur.y][cur.z]='#';  //如果这点合法,且这个位置畅通无阻,将其标记为石头,然后当前所用时间等于上一位置所用的时间加1,
            cur.time=per.time+1;
            Q.push(cur);  //让这个位置入队
        }
    }
    return -1;   //无解时返回值为-1,代表困在迷宫中,无法出去
}
int main()
{
    int i,j,k;  //循环变量i,j,k;
    while(~scanf("%d%d%d",&height,&row,&line))
    {
        if(row==0&&line==0&&height==0)  //三者为0结束
            break;
        for(k=0;k<height;k++)
            for(i=0;i<row;i++)
                for(j=0;j<line;j++)
        {
            scanf(" %c",&cmap[i][j][k]);
            if(cmap[i][j][k]=='S')  //记录入口的位置
            {
                start.x=i;
                start.y=j;
                start.z=k;
            }
            if(cmap[i][j][k]=='E')  //记录出口的位置
            {
                eend.x=i;
                eend.y=j;
                eend.z=k;
            }
        }
        int time=bfs();  //要出迷宫的最短时间
        if(time==-1)   //为-1,困在迷宫无路可出
            printf("Trapped!\n");
        else
            printf("Escaped in %d minute(s).\n",time);  //否则输出最短时间
    }
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值