POJ-2251-Dungeon Master

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/jsmok_xingkong/article/details/79950106

因为考研一直在复习高数英语,结果连这么简单的题都不会,果然学习算法还是需要不间断的练习,今天把高数上册学完了,就给自己放松一下练练算法题。
Dungeon Master
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 43911 Accepted: 16567

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

分析:宽度搜索题,只是将原先的是二维,这道题是三维,方向变为6个了,
ps:貌似求迷宫的最短路径一般都是用宽搜

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <queue>
using namespace std;

const int MAXN = 35;
const int INF = 0x3f3f3f3f;

int dir[6][3] = {{1,0,0},{-1,0,0},{0,1,0},{0,-1,0},{0,0,1},{0,0,-1}};
struct Index
{
    int x;
    int y;
    int z;
};
char map[MAXN][MAXN][MAXN];
int dis[MAXN][MAXN][MAXN]; //储存步数

Index end;

int bfs(Index start)
{
    queue<Index> que;
    memset(dis, INF, sizeof(dis)); //初始化所有位置 默认全部都是无法到达的(INF)
    que.push(start); //将起点压入队列,并将初始步数设为0
    dis[start.z][start.x][start.y] = 0;
    while (que.size()) {
        Index tempI = que.front(); que.pop(); //从队列的最前端取出元素
        if (tempI.z == end.z && tempI.x == end.x && tempI.y == end.y) { //如果取出的状态已经是终点则结束搜索
            break;
        }
        Index loc; //移动后的位置记为loc
        for (int i = 0; i < 6; i++) { //六个方向的循环
            loc.z = tempI.z + dir[i][0];
            loc.x = tempI.x + dir[i][1];
            loc.y = tempI.y + dir[i][2];
            //因为判断条件太多所以用两个if,判断是否已经访问过了(若未访问dis[loc.z][loc.x][loc.y] == INF)
            if (loc.x >= 0 && loc.x < r && loc.y >= 0 && loc.y < c && loc.z >= 0 && loc.z < l) {
                if (map[loc.z][loc.x][loc.y] != '#' && dis[loc.z][loc.x][loc.y] == INF) {
                    //若可以移动的话,则加入队列,并且距离+1(因为你走了一步)
                    que.push(loc);
                    dis[loc.z][loc.x][loc.y] = dis[tempI.z][tempI.x][tempI.y] + 1;
                }
            }
        }
    }
    return dis[end.z][end.x][end.y];
}

int main()
{
    int l, r, c;
    while (scanf("%d%d%d", &l, &r, &c) != EOF) {
        if (l==0 && r==0 && c==0) {
            break;
        }
        int step_num; //起点到终点的步数如果step_num==INF则表示终点无法到达
        Index start;
        for (int i = 0; i < l; i++) {
            for (int j = 0; j < r; j++) {
                for (int k = 0; k < c; k++) {
                    cin >> map[i][j][k];  //用c++输入比用c输入,可以很好的处理字符串输入问题
                    if (map[i][j][k] == 'S') {
                        start.x = j;
                        start.y = k;
                        start.z = i;
                    }
                    if (map[i][j][k] == 'E') {
                        end.x = j;
                        end.y = k;
                        end.z = i;
                    }
                }
            }
        }
//      printf("%d%d%d\n", end.z, end.x, end.y);
        step_num = bfs(start); 
        if (step_num == INF) { 
            printf("Trapped!\n");
        }
        else {
            printf("Escaped in %d minute(s).\n", step_num);
        }
    }
    return 0;
}
阅读更多

没有更多推荐了,返回首页