1. 程式人生 > >Dungeon Master 三維+BFS

Dungeon Master 三維+BFS

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;
}