B - Dungeon Master

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!

题目大意:

就是给定一个三维的矩阵,给定起始点,终点,#代表不能走,“.”代表可以走,然后问能否走出,若走出,输出最短时间,若不能走出,输出“Trapped!”。

思路:

首先创建一个三维的矩阵输入,然后记录下起始点,方便BFS的开始,然后再开始BFS,在判断方向时,可以将所有的方向打进数组里,通过对数组的操作实现方向的变化,注意BFS第一个找到的点就是路程最短的,这个可以根据BFS的原理推出。

一开始的时候只是打了一个数组,并不知道如何存在BFS的过程中,各个坐标的变化,这个时候看到别人的代码,知道这个地方可以用结构体来存。还有,在打方向的数组时,注意没一个单独的数组是用"{"和"}"来存入的,不是用"("和")"。。。。。。。。

代码如下:



#include<iostream>
#include<cstring>
#include<queue>
using namespace std;
int num,flag;
int visited[35][35][35];
char a[35][35][35];
int l,r,c;
struct node
{
    int x,y,z,step;
};
int f[3][6]= {{1,-1,0,0,0,0},{0,0,1,-1,0,0},{0,0,0,0,1,-1}};
bool Judge(int s1,int s2,int s3)
{
    return (s1>=1&&s1<=l&&s2>=1&&s2<=r&&s3>=1&&s3<=c);
}
void BFS(int w1,int w2,int w3)
{
    queue<node>q;
    struct node c,d;
    c.x=w1;
    c.y=w2;
    c.z=w3;
    c.step=0;
    d.step=0;
    q.push(c);
    while(!q.empty())
    {
        d=q.front();
        q.pop();
        c.step=d.step+1;
        for(int i=0; i<6; i++)
        {
            c.x=d.x+f[0][i];
            c.y=d.y+f[1][i];
            c.z=d.z+f[2][i];
            if(a[c.x][c.y][c.z]!='#'&&visited[c.x][c.y][c.z]==0&&Judge(c.x,c.y,c.z))
            {
                if(a[c.x][c.y][c.z]=='E')
                {
                    flag=1;
                    num=c.step;
                    break;
                }//这个地方最好直接在for循环里面判断,防止走多余的过程
                visited[c.x][c.y][c.z]=1;
                q.push(c);
            }
        }
    }
}
int main()
{
    while(cin>>l>>r>>c)
    {
    flag=0;
    num=0;
        memset(visited,0,sizeof(visited));
        int t1,t2,t3;
        for(int i=1; i<=l; i++)
        {
            for(int j=1; j<=r; j++)
            {
                for(int k=1; k<=c; k++)
                {
                    cin>>a[i][j][k];
                    if(a[i][j][k]=='S')
                    {
                        t1=i;
                        t2=j;
                        t3=k;
                    }
                }
            }
        }
        BFS(t1,t2,t3);
        if(flag==1)
            cout<<"Escaped in "<<num<<" minute(s)."<<endl;
        else
            cout<<"Trapped!"<<endl;
    }
    return 0;
}


猜你喜欢

转载自blog.csdn.net/let_life_stop/article/details/80502237