Dungeon Master

Problem  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). <br>L is the number of levels making up the dungeon. <br>R and C are the number of rows and columns making up the plan of each level. <br>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 <br><blockquote>Escaped in x minute(s). <ockquote> <br>where x is replaced by the shortest time it takes to escape. <br>If it is not possible to escape, print the line <br><blockquote>Trapped! <ockquote>

Sample  Input:
3 4 5
S....
.###.
.##..
###.#

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

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

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

0 0 0

Sample  Ouput:

Escaped in 11 minute(s).

Trapped!

思路:这道题要用广搜的方法,但是要注意的是这是一个三维立体空间,因此输入字符的时候要看着点儿输。

My  DaiMa:

#include<stdio.h>
#include<iostream>
#include<queue>
using namespace std;
int dir[6][3]={{1,0,0},{-1,0,0},{0,-1,0},{0,1,0},{0,0,1},{0,0,-1}};
int t,n,m;
char flag[32][32][32];
struct position
{
    int x,y,z,step;
}start,eend;
int bfs()
{
    position cur,nxt;
    queue<position>Q;
    start.step=0;
    flag[start.z][start.x][start.y]='#';//将起点标记成#号,即走过
    Q.push(start);
    while(!Q.empty())
    {
        cur=Q.front();
        Q.pop();
        if(cur.x==eend.x&&cur.y==eend.y&&cur.z==eend.z) return cur.step;
        for(int i=0;i<6;i++)
        {
            nxt.x=cur.x+dir[i][0];
            nxt.y=cur.y+dir[i][1];
            nxt.z=cur.z+dir[i][2];
            //if(nxt.x<0||nxt.x>=n||nxt.y<0||nxt.y>=m||nxt.z<0||nxt.z>=t||flag[nxt.z][nxt.x][nxt.y]=='#') continue;//方法一
            if((nxt.x>=0&&nxt.x<n)&&(nxt.y>=0&&nxt.y<m)&&(nxt.z>=0&&nxt.z<t)&&flag[nxt.z][nxt.x][nxt.y] != '#')//方法二
            {
                flag[nxt.z][nxt.x][nxt.y]='#';//将走过的地方都标记成#号
                nxt.step=cur.step+1;
                Q.push(nxt);
            }
        }
    }
    return -1;
}
int main()
{
    while(~scanf("%d%d%d",&t,&n,&m))//t是三维空间中的高代表着z,n即代表着x,m代表着y
    {
        if(t==0&&n==0&&m==0) break;
        getchar();
        for(int i=0;i<t;i++)
        {
            for(int j=0;j<n;j++)
            {
                for(int k=0;k<m;k++)
                {
                    scanf("%c",&flag[i][j][k]);//如果不太清楚为什么是flag[i][j][k]的话,可以调试一下看看
                    if(flag[i][j][k]=='S')
                    {
                        start.x=j;
                        start.y=k;
                        start.z=i;
                    }
                    if(flag[i][j][k]=='E')
                    {
                        eend.x=j;
                        eend.y=k;
                        eend.z=i;
                    }
                }
                getchar();
            }
            getchar();
        }
        int ans = bfs();
        if(ans == -1)
            printf("Trapped!\n");
        else
            printf("Escaped in %d minute(s).\n",ans);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_41181772/article/details/80225822