C - Dungeon Master

       

 三维迷宫而已啦,其实题目没怎么看,就觉得是三维迷宫,然后根据二维迷宫的思路,注意一下三维空间坐标的处理就AC啦。

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!



下面给出我的代码,解释都在代码里面啦。


#include <iostream>
#include <cstdio>
#include <queue>
#include <memory.h>
using namespace std;
const int maxn = 30+2;
class Point//定义一个记录位置的类先
{
public:
    int x;
    int y;
    int z;
    int step;
    Point(int sx, int sy, int sz, int sstep):x(sx),y(sy),z(sz),step(sstep){}//构造函数用好了后面代码就能少很多
};
char maze[maxn][maxn][maxn];//三维迷宫地图数据
bool flag[maxn][maxn][maxn];//三维迷宫地图标记数据
queue<Point> myqueue;//搜索的队列
int local[2][4];//两个位置的记录
int l, r, c, i, j, k;
void Init()
{
    //初始化清空标记数组,清空列队,读入地图数据
    while(!myqueue.empty())myqueue.pop();
    memset(flag, 0, sizeof(flag));
    for(k=1;k<=l;k++)for(i=1;i<=r;i++)for(j=1;j<=c;j++)
    {
        cin>>maze[i][j][k];
        switch(maze[i][j][k])
        {
            case 'S': local[0][1]=i;local[0][2]=j;local[0][3]=k;break;//捕获的S的位置要强势插入
            case 'E': local[1][1]=i;local[1][2]=j;local[1][3]=k;break;//捕获到E的位置也要强势插入
        }
    }
}
int check(int cx, int cy, int cz)
{
    return (cx<1||cx>r||cy<1||cy>c||cz<1||cz>l||flag[cx][cy][cz]||maze[cx][cy][cz]=='#')?0:1;//判断函数
}
int Bfs(int dx, int dy, int dz)
{
//这题就是根据二维迷宫来的,改一下模板,考虑一下三维空间就OK了,满满的都是套路我都不想解释
    Point point(dx, dy, dz, 0), next(0, 0, 0, 0);
    flag[dx][dy][dz]=true;
    myqueue.push(point);
    while(!myqueue.empty())
    {
        point = myqueue.front();
        myqueue.pop();
        if(point.x==local[1][1]&&point.y==local[1][2]&&point.z==local[1][3])return point.step;//如果S找到了E就return咯
        for(int dcase=1;dcase<=6;dcase++)
        {
            if(dcase==1){next = point;next.x=point.x+1;}//向后搜索
            if(dcase==2){next = point;next.y=point.y+1;}//向右搜索
            if(dcase==3){next = point;next.x=point.x-1;}//向前搜索
            if(dcase==4){next = point;next.y=point.y-1;}//向左搜索
            if(dcase==5){next = point;next.z=point.z+1;}//向下搜索
            if(dcase==6){next = point;next.z=point.z-1;}//向上搜索
            if(check(next.x, next.y, next.z))
            {
                next.step = point.step+1;
                flag[next.x][next.y][next.z]=true;
                myqueue.push(next);
            }
        }
    }
    return 0;
}
int main()
{
    while(cin>>l>>r>>c && l && r && c)
    {
        int go;
        Init();
        go = Bfs(local[0][1], local[0][2], local[0][3]);
        if(go)cout<<"Escaped in "<<go<<" minute(s)."<<endl;
        else cout<<"Trapped!"<<endl;
    }
    return 0;
}