uva 532 - Dungeon Master

简介: 点击打开链接 题目意思:给一个三维的地图,然后在地图里面有一个起点S 和终点E ,问是否能够找到一条路径从S到达E。如果能够找到就输出步数,如果不能就输出Trapped!。

点击打开链接


题目意思:给一个三维的地图,然后在地图里面有一个起点S 和终点E ,问是否能够找到一条路径从S到达E。如果能够找到就输出步数,如果不能就输出Trapped!。


解题思路:对于三维的地图和二维的一样,只是我们搜索时候多了两个竖直的方向。就是dir数组为dir[6][3],然后对于求解路径我们一般用BFS,然后用一个flag变量标记是否能够走到E,这样即可.


代码:

//对于求最短路径我们一般是用bfs来做的,对于空间上面的一样的思路
#include <iostream>
#include <cstdio>
#include <queue>
#include <cstring>
using namespace std;
const int MAXN = 50;
//定义一个结构体来存放坐标
struct Point {
    int x;
    int y;
    int z;
};
Point p;//p存储目标的坐标
int l, r, c, sum , flag;//flag判断是否又到E
int mark[MAXN][MAXN][MAXN];//标记数组
char maze[MAXN][MAXN][MAXN];//存放地图的数组
//注意方向数组这时候要三维
int dir[6][3] = {
    {1, 0, 0},
    {-1, 0, 0},
    {0, -1, 0},
    {0, 0, 1},
    {0, 1, 0},
    {0, 0, -1}
};
queue<Point>q;//队列

//Bfs函数
void Bfs() {
    int z , i , j;
    if (q.empty())
        return ;
    while (!q.empty()) {
        Point temppoint = q.front();
        q.pop();
        z = temppoint.z;
        i = temppoint.x;
        j = temppoint.y;
        if(z == p.z && i == p.x && j == p.y){//退出条件
             flag = 1; //更新为1
             return;
        }
        for (int t = 0; t < 6; t++) {
            if (z + dir[t][0] < 0 || z + dir[t][0] >= l)
                continue;
            if (i + dir[t][1] < 0 || i + dir[t][1] >= r)
                continue;
            if (j + dir[t][2] < 0 || j + dir[t][2] >= c)
                continue;
            if (maze[z + dir[t][0]][i + dir[t][1]][j + dir[t][2]] == '#')
                continue;
            if (mark[z + dir[t][0]][i + dir[t][1]][j + dir[t][2]] != 0)
                continue;
            if (maze[z + dir[t][0]][i + dir[t][1]][j + dir[t][2]] == '.' || 'E') {
                Point temppoint;
                temppoint.z = z + dir[t][0];
                temppoint.x = i + dir[t][1];
                temppoint.y = j + dir[t][2];
                mark[temppoint.z][temppoint.x][temppoint.y] = mark[z][i][j] + 1;//利用前一个加一方便计算
                q.push(temppoint);
            }
        }
    }
}
  
//找到对应点的函数(找到S,E的坐标)
void find(){
    int z , i , j;
    for (z = 0; z < l; z++) {
        for (i = 0; i < r; i++) {
            for (j = 0; j < c; j++) {
                if (maze[z][i][j] == 'S'){
                    Point temppoint;
                    temppoint.z = z;
                    temppoint.x = i;
                    temppoint.y = j;
                    q.push(temppoint);
                }
                if (maze[z][i][j] == 'E') {
                    p.z = z;
                    p.x = i;
                    p.y = j;
                }
            }
        }
    }
}

//处理函数
void solve() {
    sum = 0;//初始化为0
    flag = 0;//初始化为0
    memset(mark, 0, sizeof (mark));//初始化为0
    while (!q.empty())//队列的清空
        q.pop();
    find();
    Bfs();
    if(flag)//如果到E点更新sum的值
        sum = mark[p.z][p.x][p.y];
    if (sum == 0)
        printf("Trapped!\n");
    else
        printf("Escaped in %d minute(s).\n", sum);
}

//主函数
int main() {
    int i , z;
    while (scanf("%d %d %d%*c", &l, &r, &c) && l && r && c) {//输入格
        for (z = 0; z < l; z++) {
            for (i = 0; i < r; i++)
                gets(maze[z][i]);//直接读入一行字符串
            getchar();//这里一个getchar用来读‘\n’;
        }
        solve();
    }
    return 0;
}


目录
相关文章
|
5天前
|
Java
HDU-1551-Cable master
HDU-1551-Cable master
7 0
|
11月前
1248:Dungeon Master 2021-01-05
1248:Dungeon Master 2021-01-05
|
10月前
UVa11876 - N + NOD (N)
UVa11876 - N + NOD (N)
43 0
Dungeon Master
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
104 0
洛谷P1825-[USACO11OPEN]Corn Maze S(BFS)
洛谷P1825-[USACO11OPEN]Corn Maze S(BFS)
洛谷P1825-[USACO11OPEN]Corn Maze S(BFS)
CodeForces 377A-Maze(DFS找连通块)
CodeForces 377A-Maze(DFS找连通块)
CodeForces 377A-Maze(DFS找连通块)
洛谷P3855-[TJOI2008]Binary Land(BFS)
洛谷P3855-[TJOI2008]Binary Land(BFS)