您的位置:首页 > 其它

三维搜索(bfs)Dungeon Master

2016-02-25 17:14 344 查看
1.学会开三维数组保存空间的状态2.会用bfs搜索空间3.还有学会结构体的构造函数struct node {int vx, vy, vz;node( int x, int y, int y) : vx(x), vy(y), vy(z) { }};DescriptionYou 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. Youcannot move diagonally and the maze is surrounded by solid rock on all sides.Is an escape possible? If yes, how long will it take?InputThe 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 theexit by the letter 'E'. There's a single blank line after each level. Input is terminated by three zeroes for L, R and C.OutputEach maze generates one line of output. If it is possible to reach the exit, print a line of the formEscaped 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 lineTrapped!Sample Input
3 4 5
S....
.###.
.##..
###.#

#####
#####
##.##
##...

#####
#####
#.###
####E

1 3 3
S##
#E#
###

0 0 0
Sample Output
Escaped in 11 minute(s).
Trapped!
#include <iostream>#include <cstdio>#include <cstdlib>#include <cstring>#include <cmath>#include <stack>#include <queue>#include <set>#include <map>#include <algorithm>using namespace std;typedef pair<int, int> P;//#define LOCAL#define INF 0x3f3f3f3f#define MAX_N 31struct 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<node> 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 LOCALfreopen("b:\\data.in.txt", "r", stdin);#endifwhile(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;elsecout << "Trapped!" << endl;}return 0;}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: