搜索多层图

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 , 一圈一圈的搜 , 肯定是先找到最近的路,建立一个记录步数的数组 ,记录到每个点的步数。
注意 : 存图 一定要用 char 来存 !!! 经常写成 int 型的

代码示例 :
/*
 * Author:  ry 
 * Created Time:  2017/10/24 20:30:13
 * File Name: 1.cpp
 */
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <time.h>
using namespace std;
const int eps = 1e6+5;
const double pi = acos(-1.0);
const int inf = 0x3f3f3f3f;
#define Max(a,b) a>b?a:b
#define Min(a,b) a>b?b:a
#define ll long long

char mp[35][35][35];
int dir[6][3] = {{1, 0, 0}, {-1, 0, 0}, {0, 1, 0}, {0, -1, 0}, {0, 0, 1}, {0, 0, -1}};
int l, n, m;
int sx, sy, sz, gx, gy, gz;
int pre[35][35][35];

struct node
{
    int x, y, z;
};

bool check(int x, int y, int z){
    if (x >= 1 && x <= l && y >= 1 && y <= n && z >= 1 && z <= m && pre[x][y][z] == -1 && mp[x][y][z] != '#'){
        return true;
    }
    else return false;
}

void bfs(){
    node ff, t;
    queue<node>que;
    
    ff.x = sx, ff.y = sy, ff.z = sz;
    que.push(ff);
    pre[sx][sy][sz] = 0;
    while(!que.empty()){
        node v = que.front();
        que.pop();
        
        if (v.x == gx && v.y == gy && v.z == gz){
            break;
        }
        for(int i = 0; i < 6; i++){
            t.x = v.x + dir[i][0];
            t.y = v.y + dir[i][1];
            t.z = v.z + dir[i][2];
            if (check(t.x, t.y, t.z)){
                pre[t.x][t.y][t.z] = pre[v.x][v.y][v.z] + 1;
                que.push(t);
            }
        }
    }
}

int main() {
    
    while (~scanf("%d%d%d", &l, &n, &m) && l+n+m){
        memset(pre, -1, sizeof(pre));
        
        for(int i = 1; i <= l; i++){
            for(int j = 1; j <= n; j++){
                scanf("%s", mp[i][j]+1);
            }
        }
        for(int i = 1; i <=l ; i++){
            for(int j = 1; j <= n; j++){
                for(int k = 1; k <= m; k++){
                    if (mp[i][j][k] == 'S') {
                        sx = i; sy = j; sz = k;
                    }
                    if (mp[i][j][k] == 'E'){
                        gx = i; gy = j; gz = k;
                    }
                }
            }
        }
        bfs();
        if (pre[gx][gy][gz] != -1)
            printf("Escaped in %d minute(s).\n", pre[gx][gy][gz]);
        else {
            printf("Trapped!\n");
        }
    }
    
    return 0;
}

 


posted @ 2017-10-24 23:17  楼主好菜啊  阅读(247)  评论(0编辑  收藏  举报