Dungeon Master POJ - 2251 [kuangbin带你飞]专题一 简单搜索

Posted 九月旧约

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Dungeon Master POJ - 2251 [kuangbin带你飞]专题一 简单搜索相关的知识,希望对你有一定的参考价值。

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!

三维bfs水题,但鉴于自己好久没有做bfs,忘记了vis数组放哪里和取值,最最重要的是中间一行代码写错了,害我debug半天
bfs模板题
#include<iostream>
#include<cstdio>
#include<queue>
#include<cstring>
using namespace std;
int dx[]={1,0,-1,0,0,0},dy[]={0,-1,0,1,0,0},dz[]={0,0,0,0,1,-1};
int t,n,m,sx,sy,sz,ex,ey,ez,vis[50][50][50];
char mapn[50][50][50];
struct node{
    int x,y,z,step;
};
void bfs(){
    node p;
    p.x = sx,p.y = sy,p.z = sz;
    vis[sx][sy][sz] = 1;
    p.step = 0;
    queue<node> q;
    q.push(p);
    while(!q.empty()){
        node tmp = q.front();
        q.pop();
        if(tmp.x == ex && tmp.y == ey && tmp.z == ez){
            cout << "Escaped in " << tmp.step << " minute(s)." << endl;
            return;
        } 
        for(int i=0;i<6;i++){
            int xx = tmp.x + dx[i];
            int yy = tmp.y + dy[i];
            int zz = tmp.z + dz[i];
            if(mapn[xx][yy][zz] != # && xx>0 && xx<=t && yy>0 && yy<=n && zz>0 && zz<=m && !vis[xx][yy][zz]){
                node tp;
                tp.x = xx;
                tp.y = yy;
                tp.z = zz;
                tp.step = tmp.step + 1;//这一行代码,开始写成了tp.step++;
                vis[xx][yy][zz] = 1;
                q.push(tp);
            } 
        }
    }
    cout << "Trapped!" << endl;
}
int main(){
    while(cin >> t >> n >> m){
        if(t == 0 || n == 0 || m == 0){
            break;
        }
        memset(vis,0,sizeof(vis));
        for(int i=1;i<=t;i++){
            for(int j=1;j<=n;j++){
                for(int k=1;k<=m;k++){
                    cin >> mapn[i][j][k];
                    if(mapn[i][j][k] == S){
                        sx = i,sy = j,sz = k;
                    }
                    if(mapn[i][j][k] == E){
                        ex = i,ey = j,ez = k;
                    }
                }
            }
        }
        bfs();
    }
    return 0;
}

 

以上是关于Dungeon Master POJ - 2251 [kuangbin带你飞]专题一 简单搜索的主要内容,如果未能解决你的问题,请参考以下文章

POJ 2251 Dungeon Master (BFS)

POJ-2251 Dungeon Master

POJ2251 Dungeon Master

POJ 2251 Dungeon Master(BFS)

搜索POJ2251:Dungeon Master

[POJ 2251] Dungeon Master