其他分享
首页 > 其他分享> > 【POJ - 2251】Dungeon Master (bfs+优先队列)

【POJ - 2251】Dungeon Master (bfs+优先队列)

作者:互联网

Dungeon Master


 

 Descriptions:

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

Sample Output

Escaped in 11 minute(s).

Trapped!

题目链接:

https://vjudge.net/problem/POJ-2251

 

最短路bfs,和二维的基本一样,就是原来4个方向,现在6个方向,原来数组是二维,现在是三维,也相当于模板题了

AC代码

#include <iostream>
#include <cstdio>
#include <fstream>
#include <algorithm>
#include <cmath>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <cstring>
#include <map>
#include <stack>
#include <set>
#define mod 1000000007
#define ll long long
#define INF 0x3f3f3f3f
using namespace std;
int sl,sx,sy;
int el,ex,ey;
char mp[35][35][35];//记录地图
int vis[35][35][35];//标记是否走过
int base[6][3] = { {-1,0,0},{1,0,0},{0,-1,0},{0,1,0},{0,0,-1},{0,0,1} };//行走方式
int l,r,c;
struct node
{
    int f,x,y;//位置
    int step;//步数
    friend bool operator<(node a,node b)//步数小的现出来,即时间少
    {
        return a.step>b.step;//优先队列,步数小的先访问
    }
};
priority_queue<node>q;
/*************************bfs***************************/
void bfs()
{
    node p;
    p.f=sl;
    p.x=sx;
    p.y=sy;
    p.step=0;
    vis[sl][sx][sy]=1;
    q.push(p);
    while(!q.empty())
    {
        node s=q.top();
        q.pop();
        if(s.f==el&&s.x==ex&&s.y==ey)
        {
            printf("Escaped in %d minute(s).\n",s.step);
            return;
        }
            for(int i=0; i<6; i++)
            {
                int tl=s.f+base[i][0];
                int tx=s.x+base[i][1];
                int ty=s.y+base[i][2];
                if(mp[tl][tx][ty]!='#'&&tl>=0&&tl<l&&tx>=0&&tx<r&&ty>=0&&ty<c&&!vis[tl][tx][ty])//判断是否能走
                {
                    node e;
                    e.f=tl;
                    e.x=tx;
                    e.y=ty;
                    e.step=s.step+1;
                    vis[e.f][e.x][e.y]=1;
                    q.push(e);
                }
            }
    }
    cout<<"Trapped!"<<endl;
}
/**********************************主函数*********************************/
int main()
{
    while(cin >> l >> r >>c,l+r+c)
    {
        for(int i=0; i<l; i++)
        {
            for(int j=0; j<r; j++)
            {
                cin >> mp[i][j];
                for(int k=0; k<c; k++)
                {
                    if(mp[i][j][k]=='S')
                        {
                            sl=i;
                            sx=j;
                            sy=k;
                        }
                    if(mp[i][j][k]=='E')
                    {
                        el=i;
                        ex=j;
                        ey=k;
                    }
                }
            }
        }
        memset(vis,0,sizeof(vis));//每次都要初始化
        bfs();
    }
}

 

标签:Dungeon,dungeon,int,35,bfs,Master,&&,line,include
来源: https://www.cnblogs.com/sky-stars/p/10963418.html