POJ 2251Dungeon Master

POJ 2251Dungeon 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 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.

Ouput

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

这里写图片描述

Sample Output

这里写图片描述

解题思路

1.将搜索扩充到三维上,在二维的基础上增加层数,相当于增加了一个分量,基本思路同二维上的搜索,但要注意以下几点(纪念自己WA了N次)
2.深搜会超时,要采用记忆化搜索,所以不如直接用广搜
3.题目的数据量给小了,讨论区里有的数组开到105才AC
4.S点并不一定是第一层最左上角的点
5.bfs时,一点要把队列清空,因为存在下一组数据(所以初始化和清空很重要,有点内存管理的意思,反而忽略了,活该WA了这么多次)

#include<iostream>
#include<queue>
#include<cstring>
#include<string>
#include<cstdio>
#include<memory.h>
using namespace std;
struct Node {//使用结构体便于bfs将点加入队列
    int x,y;//点的横纵坐标
    int floot;//层数
    int setp;//走到当前点的步数
} node,nodes;
queue<struct Node>q;
int a[200][200][200];//存储每层,注意数据范围给小了,尽量开的大一点
bool book[200][200][200];//标记数组
int nexts[6][3]= {{1,0,0},{-1,0,0},{0,0,1},{0,0,-1},{0,1,0},{0,-1,0}};//控制方向
int main() {
    int n,m,l;
    char c;
    while(cin>>l>>n>>m) {
        getchar();
        if(l==0&&n==0&&m==0) {
            break;
        }
        memset(a,0,sizeof(a));//初始化
        memset(book,false,sizeof(book));
        for(int i=1; i<=l; i++) {//输入
            for(int j=1; j<=n; j++) {
                for(int k=1; k<=m; k++) {
                    cin>>c;
                    if(c=='.') {
                        a[i][j][k]=1;
                    }
                    else if(c=='E') {
                        a[i][j][k]=2;
                    }
                    else if(c=='S') {
                        a[i][j][k]=3;
                    }
                }
                getchar();
            }
            getchar();//注意吸收空行
        }
        int flagg=0;
        for(int i=1; i<=l; i++) {//寻找S点,并将其加入队列,开始bfs
            for(int j=1; j<=n; j++) {
                for(int k=1; k<=m; k++) {
                    if(a[i][j][k]==3) {
                        node.x=j;
                        node.y=k;
                        node.floot=i;
                        node.setp=0;
                        q.push(node);
                        book[i][j][k]=true;//注意标记
                        break;
                    }
                }
            }
        }
        while(!q.empty()) {
            if(flagg==1) {
                break;
            }
            node=q.front();
            q.pop();
            for(int f=0; f<6; f++) {//遍历6个方向
                int floots=node.floot+nexts[f][0];
                int xs=node.x+nexts[f][1];
                int ys=node.y+nexts[f][2];
                int setps=node.setp+1;
                if(a[floots][xs][ys]==2) {//已经到达E
                    cout<<"Escaped in "<<setps<<" minute(s)."<<endl;
                    flagg=1;
                    break;
                }
                else if(a[floots][xs][ys]==1&&book[floots][xs][ys]==false) {//此点可走
                    nodes.floot=floots;
                    nodes.x=xs;
                    nodes.y=ys;
                    nodes.setp=setps;
                    q.push(nodes);
                    book[floots][xs][ys]=true;//注意标记,否则将超时
                }
            }
        }
        while(!q.empty()) {//一定记得清空队列,不然WA到怀疑人生
            q.pop();
        }
        if(flagg==0) {
            cout<<"Trapped!"<<endl;
        }
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值