文章标题 Dungeon Master

来源:互联网 发布:testlink java api 编辑:程序博客网 时间:2024/06/05 21:49

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!

题意:从S能否到E,不可以输出Trapped!,可以输出最短时间

#include<cstdio>#include<cstring>#include<iostream>#include<queue>using namespace std;char map[31][31][31];int vast[31][31][31];int vist[6][3]= {{0,0,1},{0,0,-1},{1,0,0},{-1,0,0},{0,1,0},{0,-1,0}};int L,R,C;struct node{    int x,y,z,time;};int judge(int x,int y,int z)//判断是否可以走{    if(!vast[x][y][z]&&map[x][y][z]!='#'&&x>=0&&x<L&&y>=0&&y<R&&z>=0&&z<C)        return 1;    return 0;}int bfs(int x,int y,int z,int time){    node xy,xz;    xy.x=x,xy.y=y,xy.z=z,xy.time=time;    queue<node>s;    s.push(xy);    while(!s.empty())    {        xy=s.front();        s.pop();        if(map[xy.x][xy.y][xy.z]=='E')        {            return xy.time;        }        for(int i=0; i<6; i++)        {            int xi=xy.x+vist[i][0],yi=xy.y+vist[i][1],zi=xy.z+vist[i][2];            if(judge(xi,yi,zi))            {                vast[xi][yi][zi]=1;                xz.x=xi,xz.y=yi,xz.z=zi;                xz.time=xy.time+1;                s.push(xz);            }        }    }    return 0;}int main(){    while(~scanf("%d%d%d",&L,&R,&C))    {        memset(vast,0,sizeof(vast));        if(L==0&&R==0&&C==0)            break;        int i,j,k,a,b,c;        for(i=0; i<L; i++)        {            for(j=0; j<R; j++)            {                scanf("%s",map[i][j]);                for(k=0; k<C; k++)                {                    if(map[i][j][k]=='S')                        a=i,b=j,c=k;                }            }        }        int t=bfs(a,b,c,0);        if(!t)            printf("Trapped!\n");        else            printf("Escaped in %d minute(s).\n",t);    }}
0 0
原创粉丝点击