POJ 2251 Dungeon Master (three-dimensional BFS)

Source: Internet
Author: User
Tags bool
Topic links

poj2251 Topic

Given a three-dimensional map, ' # ' cannot go, '. ' can go to the shortest path to the e of the S to the end. Analysis

The problem is to find the shortest path in the maze, but the maze is three-dimensional, the direction of the array from the original 4 directions into 6 directions. Direct BFS to find the distance.
PS: Although a simple question, but I still wa a few times qaq, because I began to judge whether the lattice can walk the Boolean function is written like this:

BOOL Can (int x,int y,int z)
{
    if (maze[x][y][z]== '. ' | | maze[x][y][z]== ' E ') && (!vis[x][y][z])
        return true;
    return false;
}

This seems to extend the scope of the bounds, but because the topic is a multi-array of arrays, and I every time the character array of the maze is not empty, when the new input maze than the old maze size of the hour, the old maze more out of the part still exist, I this judgment function will cause the extension out of bounds. Therefore, the judgment function of this kind of question is to honestly put the situation of disobedience as well as the list. Code

#include <iostream> #include <cstring> #include <queue> #define MAXN using namespace std; struct Node {int x,y,z,step;}
S
Char MAZE[MAXN][MAXN][MAXN];
BOOL VIS[MAXN][MAXN][MAXN];
int ans,l,r,c; BOOL Can (int x,int y,int z) {if (x<1| | x>r| | y<1| | y>c| | z<1| |
    Z&GT;L)//Determine if the out-of-bounds return false;
    if (maze[x][y][z]== ' # ')//To determine if it is an obstacle return false;
    if (Vis[x][y][z])//To determine whether to access the return false;
return true;
    } bool Can (int x,int y,int z) {if} bool Bfs () {queue<node> Q;
    int d[6][3]={{0,0,1},{0,0,-1},{0,-1,0},{0,1,0},{-1,0,0},{1,0,0}};
    memset (vis,false,sizeof (VIS));
    Vis[s.x][s.y][s.z]=true; while (!
    Q.empty ()) Q.pop ();
    Q.push (s); while (!
        Q.empty ()) {Node temp=q.front ();
        Q.pop ();
            if (maze[temp.x][temp.y][temp.z]== ' E ') {ans=temp.step;
        return true; } for (int i=0;i<=5;i++) {Node Temp2;
            TEMP2.X=TEMP.X+D[I][0];
            TEMP2.Y=TEMP.Y+D[I][1];
            TEMP2.Z=TEMP.Z+D[I][2];
                if (Can (TEMP2.X,TEMP2.Y,TEMP2.Z)) {temp2.step=temp.step+1;
                Vis[temp2.x][temp2.y][temp2.z]=true;
            Q.push (TEMP2);
}}} return false;
    } int main () {int i,j,k;
        while (cin>>l>>r>>c) {if (!l&&!r&&!c) break;
                    for (k=1;k<=l;k++) for (i=1;i<=r;i++) for (j=1;j<=c;j++) {
                    cin>>maze[i][j][k];
                        if (maze[i][j][k]== ' S ') {s.x=i;s.y=j;s.z=k;
                    S.step=0; }} if (Bfs ()) cout<< "escaped in" <<ans<< "minute (s)."
        <<endl; else cout<< "trapped!"
    <<endl; } RETUrn 0;
 }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.