POJ-2251-Dungeon Master

原创 2018年04月15日 16:04:33

因为考研一直在复习高数英语,结果连这么简单的题都不会,果然学习算法还是需要不间断的练习,今天把高数上册学完了,就给自己放松一下练练算法题。
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;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/jsmok_xingkong/article/details/79950106

SPI(裸机和驱动)视频教程

-
  • 1970年01月01日 08:00

POJ-2251-Dungeon Master(BFS 优先队列)

B - Dungeon Master Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Su...
  • qq_32680617
  • qq_32680617
  • 2016-06-06 15:52:54
  • 189

POJ-2251-Dungeon Master(BFS)

Description You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is...
  • qq_38712932
  • qq_38712932
  • 2017-09-19 19:40:48
  • 163

poj-2251-Dungeon Master(bfs)

Dungeon Master Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 14820  ...
  • u011558005
  • u011558005
  • 2013-12-24 23:57:20
  • 625

POJ-2251-Dungeon Master 三维BFS

Dungeon Master Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 39067   Accepted: ...
  • JayACM
  • JayACM
  • 2017-11-30 10:15:21
  • 44

poj-2251-Dungeon Master【BFS】

Dungeon Master Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 26621  ...
  • wang907553141
  • wang907553141
  • 2016-07-30 09:03:59
  • 116

POJ-2251-Dungeon Master(BFS)

Dungeon Master Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 27557 Accept...
  • qq_32680617
  • qq_32680617
  • 2016-10-22 11:56:58
  • 175

poj-2251-Dungeon Master(bfs)

Dungeon Master Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 23841  ...
  • tree__water
  • tree__water
  • 2016-02-29 21:08:58
  • 369

POJ-2251-Dungeon Master【BFS】【三维迷宫问题】

2251-Dungeon Master
  • loy_184548
  • loy_184548
  • 2016-04-01 11:15:31
  • 626

poj-2251-Dungeon Master-BFS

题意: 从S到E,一个三维的BFS可以解决,多一维,可以这样去理解,二维相当于一个平面,而三维的相当于一栋楼房,只是在z上多了几个平面而已 另外,第一次用队列写BFS好开心,以前都是模拟队列,也是...
  • RIPwangh
  • RIPwangh
  • 2015-08-18 16:53:20
  • 248
收藏助手
不良信息举报
您举报文章:POJ-2251-Dungeon Master
举报原因:
原因补充:

(最多只允许输入30个字)