Dungeon Master 三维+BFS

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/int1282951082/article/details/52211323

Dungeon Master

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other)
Total Submission(s) : 64   Accepted Submission(s) : 29
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).
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,第一行三个数字分别代表 高z,行x,列y。‘.’为路,‘#’为墙。每点可以朝三维的6个方向走,不走斜角。如果走得出来输出时间(步数),否则。。。

思路:如果你会做二维的BFS,那么三维的其实就比二维多了两个方向。请看代码:

#include<cstdio>
#include<iostream>
#include<cstring>
#include<queue>
using namespace std;

char G[36][36][36];
int p[36][36][36],L,R,C,t;
int dir[6][3]={{1,0,0},{-1,0,0},{0,1,0},{0,-1,0},{0,0,1},{0,0,-1}}; //前后左右上下6个方向
bool flag;

struct data
{
    int x,y,z,s;
};

data first;

void bfs()
{
    queue<data> q;
    q.push(first);
    while(!q.empty())
    {
        data now=q.front();
        q.pop();
        if(G[now.x][now.y][now.z]=='E')
        {
            flag=1;
            t=now.s;
        }
        data next;
        next.s=now.s+1;
        for(int i=0;i<6;i++)
        {
            int x=now.x+dir[i][0],y=now.y+dir[i][1],z=now.z+dir[i][2];
            if((x>=0 && x<L && y>=0 &&y<R && z>=0 && z<C && G[x][y][z]=='.' && p[x][y][z]==0)||(G[x][y][z]=='E'))
            {
                next.x=x,next.y=y,next.z=z;
                q.push(next);
                p[x][y][z]=1;
            }
        }
    }
}
int main()
{
    while(cin>>L>>R>>C,L!=0)
    {
        for(int i=0;i<L;i++)
        {
            for(int j=0;j<R;j++)
            {
                for(int k=0;k<C;k++)
                {
                    cin>>G[i][j][k];
                    p[i][j][k]=0;
                }
            }
        }
        for(int i=0;i<L;i++)
        {
            for(int j=0;j<R;j++)
            {
                for(int k=0;k<C;k++)
                {
                    if(G[i][j][k]=='S')
                    {
                        first.x=i,first.y=j,first.z=k,first.s=0;
                        p[i][j][k]=1;
                    }
                }
            }
        }
        flag=0;
        bfs();
        if(flag) printf("Escaped in %d minute(s).\n",t);
        else printf("Trapped!\n");
    }
    return 0;
}


猜你喜欢

转载自blog.csdn.net/int1282951082/article/details/52211323
今日推荐