三维搜索(bfs)Dungeon Master

1.学会开三维数组保存空间的状态

2.会用bfs搜索空间

3.还有学会结构体的构造函数

struct node {

int vx, vy, vz;

node( int x, int y, int y) : vx(x), vy(y), vy(z) {  }

};

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 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;
typedef pair P;
//#define LOCAL
#define INF 0x3f3f3f3f
#define MAX_N 31

struct node {
    int vx, vy, vz;
    node(int x, int y, int z):vx(x), vy(y), vz(z){}
};

char maze[MAX_N][MAX_N][MAX_N];
int ans[MAX_N][MAX_N][MAX_N];
int vis[MAX_N][MAX_N][MAX_N];
int L, R, C;//空间的高,行,列
int sx, sy, sz, ex, ey, ez;//起点终点坐标
int xyz[6][3] = {{0,1,0}, {1,0,0}, {0,-1,0}, {-1,0,0}, {0,0,1}, {0,0,-1}};

bool bfs()
{
    bool ans2 = 0;
    queue que;
    que.push(node(sx, sy, sz));
    ans[sx][sy][sz] = 0;
    vis[sx][sy][sz] = INF;

    while(!que.empty())
    {
//        system("pause");
        node now = que.front();
        que.pop();
//        cout << now.vx << " " << now.vy << " " << now.vz << endl;
        if(now.vx == ex && now.vy == ey && now.vz == ez) {
            ans2 = 1;
            break;
        }

        for(int i = 0; i < 6; i++) {
            int nx = now.vx + xyz[i][0], ny = now.vy + xyz[i][1], nz = now.vz + xyz[i][2];//转移
            if(nx >= 0 && nx < R && ny >= 0 && ny < C && nz >= 0 && nz < L && maze[nx][ny][nz] != '#' && vis[nx][ny][nz] != INF) {
                que.push(node(nx, ny, nz));
                vis[nx][ny][nz] = INF;
                ans[nx][ny][nz] = ans[now.vx][now.vy][now.vz] + 1;
            }
        }
    }
    return ans2;
}

int main()
{
	#ifdef LOCAL
		freopen("b:\\data.in.txt", "r", stdin);
	#endif
    while(scanf("%d%d%d", &L, &R, &C))
    {
        if(!L && !R && !C)
            break;
        for(int z = 0; z < L; z++)
        for(int x = 0; x < R; x++)
        for(int y = 0; y < C; y++) {
            cin >> maze[x][y][z];
            if(maze[x][y][z] == 'S') {
                sx = x, sy = y, sz = z;
            }
            if(maze[x][y][z] == 'E') {
                ex = x, ey = y, ez = z;
            }
        }
//        cout << sx << " " << sy << " " << sz << endl;
//        cout << ex << " " << ey << " " << ez << endl;

//        for(int z = 0; z < L; z++)
//        for(int x = 0; x < R; x++)
//        for(int y = 0; y < C; y++)
//        {
//            cout << maze[x][y][z] << " ";
//            if( y == C - 1)
//                cout << endl;
//        }
        memset(ans, 0, sizeof(ans));
        memset(vis, 0, sizeof(vis));
        int can = bfs();
        if(can)
            cout << "Escaped in " << ans[ex][ey][ez] << " minute(s)."<< endl;
        else
            cout << "Trapped!" << endl;
    }
    return 0;
}


你可能感兴趣的:(ACM)