POJ 2551 Dungeon Master

Title Link

Title Meaning

There is a 3D dungeon, which is composed of unit cubes. Now you can walk up, down, left, right, back and forth in six directions. Now I ask if you can escape from the dungeon. If you can escape as soon as possible, it will take a few minutes. If you can't, output Trapped!
Now we give three integers L,n,m, where l represents the order of the dungeon, and we give a map of each order as n*m.

Solving problems

At first, I felt a little confused when I looked at this question, but when I read the question again, I found that it was essentially the same as those extensive search questions that I had done before. It was just that the plan was replaced by a three-dimensional figure to search.
We can open a three-dimensional array to judge, and three-dimensional extensive search can solve the problem.
Let's see the code for specific judgment!

Code part

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <math.h>
#include <queue>
using namespace std;
char maps[35][35][35];
int vis[35][35][35];
int sx,sy,sz,ex,ey,ez;
int L,R,C;
int dir[6][3]={{1,0,0},{-1,0,0},{0,1,0},{0,-1,0},{0,0,1},{0,0,-1}};
struct node
{
    int x,y,z;
    int step;
};
void bfs()
{
    node p,q;
    queue<node>Q;
    memset(vis,0,sizeof(vis));
    p.x=sx;p.y=sy;p.z=sz;
    p.step=0;
    vis[p.x][p.y][p.z]=1;
    Q.push(p);
    while(!Q.empty())
    {
        p=Q.front();
        Q.pop();
        if(p.x==ex&&p.y==ey&&p.z==ez)
        {
            cout<<"Escaped in "<<p.step<<" minute(s)."<<endl;
            return;
        }
        for(int i=0; i<6; i++)
        {
            q.x=p.x+dir[i][0];
            q.y=p.y+dir[i][1];
            q.z=p.z+dir[i][2];
            q.step=p.step+1;
            if(q.x>=0&&q.y>=0&&q.z>=0&&q.x<L&&q.y<R&&q.z<C&&maps[q.x][q.y][q.z]!='#'&&vis[q.x][q.y][q.z]==0)
            {
                vis[q.x][q.y][q.z]=1;
                Q.push(q);
            }
        }
    }
    cout<<"Trapped!"<<endl;
}
int main()
{
    while(~scanf("%d%d%d",&L,&R,&C))
    {
        if(L==0&&R==0&&C==0)
            break;
        for(int i=0; i<L; i++)
        {
            getchar();
            for(int j=0; j<R; j++)
            {
                for(int z=0; z<C; z++)
                {
                    scanf("%c",&maps[i][j][z]);
                    if(maps[i][j][z]=='S')
                    {
                        sx=i;
                        sy=j;
                        sz=z;
                    }
                    if(maps[i][j][z]=='E')
                    {
                        ex=i;
                        ey=j;
                        ez=z;
                    }
                }
                getchar();
            }
        }
        bfs();
    }
    return 0;
}

Posted by rafadc on Mon, 30 Mar 2020 11:06:11 -0700