(简单) POJ 2251 Dungeon Master,BFS。

  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?

  一个三维的迷宫问题,和二维没什么区别,直接BFS就好。

代码如下:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>

using namespace std;

bool map1[40][40][40];
bool vis[40][40][40];
int L,R,C;
int Sl,Sr,Sc,El,Er,Ec;

struct state
{
    int l,r,c;
    int num;

    state() {}
    state(int x,int y,int z,int n):l(x),r(y),c(z),num(n) {}
};

bool judge(int x,int y,int z)
{
    if(x<=0||x>L||y<=0||y>R||z<=0||z>C)
        return 0;

    if(map1[x][y][z]==0)
        return 0;

    if(vis[x][y][z])
        return 0;

    vis[x][y][z]=1;
    return 1;
}

int bfs()
{
    queue <state> que;
    state temp;
    int tl,tr,tc;

    que.push(state(Sl,Sr,Sc,0));

    while(!que.empty())
    {
        temp=que.front();
        que.pop();

        if(temp.l==El&&temp.r==Er&&temp.c==Ec)
            return temp.num;

        tl=temp.l;
        tr=temp.r;
        tc=temp.c;

        if(judge(tl-1,tr,tc))
            que.push(state(tl-1,tr,tc,temp.num+1));
        if(judge(tl+1,tr,tc))
            que.push(state(tl+1,tr,tc,temp.num+1));
        if(judge(tl,tr-1,tc))
            que.push(state(tl,tr-1,tc,temp.num+1));
        if(judge(tl,tr+1,tc))
            que.push(state(tl,tr+1,tc,temp.num+1));
        if(judge(tl,tr,tc-1))
            que.push(state(tl,tr,tc-1,temp.num+1));
        if(judge(tl,tr,tc+1))
            que.push(state(tl,tr,tc+1,temp.num+1));
    }

    return -1;
}

int main()
{
    char s[50];
    int ans;

    ios::sync_with_stdio(false);

    while(cin>>L>>R>>C)
    {
        memset(vis,0,sizeof(vis));

        if(!L&&!R&&!C)
            break;

        for(int i=1;i<=L;++i)
            for(int j=1;j<=R;++j)
            {
                cin>>s;
                for(int k=1;k<=C;++k)
                    switch(s[k-1])
                    {
                        case ‘S‘:
                            Sl=i;
                            Sr=j;
                            Sc=k;
                            map1[i][j][k]=1;
                            break;
                        case ‘E‘:
                            El=i;
                            Er=j;
                            Ec=k;
                            map1[i][j][k]=1;
                            break;
                        case ‘.‘:
                            map1[i][j][k]=1;
                            break;
                        case ‘#‘:
                            map1[i][j][k]=0;
                            break;
                    }
            }

        ans=bfs();

        if(ans!=-1)
            cout<<"Escaped in "<<ans<<" minute(s).\n";
        else
            cout<<"Trapped!\n";
    }

    return 0;
}

时间: 2024-10-11 10:12:39

(简单) POJ 2251 Dungeon Master,BFS。的相关文章

POJ 2251 Dungeon Master(bfs)

Dungeon Master 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 dow

POJ 2251:Dungeon Master(三维BFS)

Dungeon Master Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 16178 Accepted: 6268 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 wit

POJ 2251 Dungeon Master --- 三维BFS(用BFS求最短路)

POJ 2251 题目大意: 给出一三维空间的地牢,要求求出由字符'S'到字符'E'的最短路径,移动方向可以是上,下,左,右,前,后,六个方向,每移动一次就耗费一分钟,要求输出最快的走出时间.不同L层的地图,相同RC坐标处是相连通的.(.可走,#为墙) 解题思路:从起点开始分别往6个方向进行BFS(即入队),并记录步数,直至队为空.若一直找不到,则困住. /* POJ 2251 Dungeon Master --- 三维BFS(用BFS求最短路) */ #include <cstdio> #i

BFS POJ 2251 Dungeon Master

题目传送门 1 /* 2 BFS:这题很有意思,像是地下城,图是立体的,可以从上张图到下一张图的对应位置,那么也就是三维搜索,多了z坐标轴 3 */ 4 #include <cstdio> 5 #include <algorithm> 6 #include <cstring> 7 #include <queue> 8 using namespace std; 9 10 const int MAXN = 33; 11 const int INF = 0x3f3

POJ 2251 Dungeon Master(地牢大师)

p.MsoNormal { margin-bottom: 10.0000pt; font-family: Tahoma; font-size: 11.0000pt } h1 { margin-top: 5.0000pt; margin-bottom: 5.0000pt; text-align: left; font-family: 宋体; font-weight: bold; font-size: 24.0000pt } span.10 { font-family: "Times New Rom

POJ - 2251 - Dungeon Master (简单BFS)

Dungeon Master Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 20450   Accepted: 7917 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

POJ 2251 Dungeon Master(三维6方向BFS)

B - Dungeon Master Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Practice POJ 2251 Appoint description:  System Crawler  (2015-03-28) Description You are trapped in a 3D dungeon and need to find the quicke

poj 2251 Dungeon Master(优先队列bfs)

Dungeon Master Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 20867   Accepted: 8090 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

poj 2251 Dungeon Master 3维bfs(水水)

Dungeon Master Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 21230   Accepted: 8261 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