POJ 2251 Dungeon Master

Posted cynchanpin

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了POJ 2251 Dungeon Master相关的知识,希望对你有一定的参考价值。

来源:http://poj.org/problem?id=2251



Dungeon Master
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 16522   Accepted: 6425

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

Sample Output

Escaped in 11 minute(s).
Trapped!

Source


PS: BFS---标准走迷宫模板题

代码:
#include<iostream>
#include<string>
using namespace std;
const int Max=30000;
int L,R,C,xe,ye,ze;
bool visit[31][30][30];
string str[31][31];
int dir[6][3]={
    {0,0,1},{0,0,-1},
    {-1,0,0},{1,0,0},
    {0,1,0},{0,-1,0}
};
struct Node{
    int xs,ys,zs,time;
    Node()
    {
    xs=0;ys=0;zs=0;time=0;
    };
} map[Max],temp;

int main()
{
    while((cin>>L>>R>>C)&&(L||R||C))
    {
    int i,j,k,flag=1;
    for(i=0;i<L;i++){
    for(j=0;j<R;j++)
    {
    cin>>str[i][j];
    for(k=0;k<C;k++)
    if(str[i][j][k]==‘.‘)
    visit[i][j][k]=true;
    else if(str[i][j][k]==‘#‘)
    visit[i][j][k]=false;
    else if(str[i][j][k]==‘S‘)
    {
     map[0].xs=i;   map[0].ys=j;    map[0].zs=k; visit[i][j][k]=false;
    }
    else if(str[i][j][k]==‘E‘)
    {
    xe=i;    ye=j;ze=k; visit[i][j][k]=true;
    }
    }
    }
    int nodedir=0,exdir=0;
    while(nodedir<=exdir&&exdir<Max&&flag)
    {
        for(int i=0;i<6;i++)
        {
        temp=map[nodedir];  //设置父节点 
        int tx=temp.xs+dir[i][0],ty=temp.ys+dir[i][1],tz=temp.zs+dir[i][2];
            if(tx>=0&&tx<L&&ty>=0&&ty<R&&tz>=0&&tz<C&&visit[tx][ty][tz])
            {
            temp.xs=tx; temp.ys=ty; temp.zs=tz;
            visit[tx][ty][tz]=false;
            temp.time++;
            map[++exdir]=temp;
            if(temp.xs==xe&&temp.ys==ye&&temp.zs==ze)
            {
            cout<<"Escaped in "<<temp.time<<" minute(s)."<<endl;
            flag=0;
            break;
            }
            }
        }
        nodedir++;
    }
    if(flag) cout<<"Trapped!"<<endl;
    }
    return 0;
}





以上是关于POJ 2251 Dungeon Master的主要内容,如果未能解决你的问题,请参考以下文章

POJ 2251 Dungeon Master

Dungeon Master POJ-2251 三维BFS

POJ 2251 Dungeon Master

POJ 2251 Dungeon Master (BFS)

POJ-2251 Dungeon Master

POJ2251 Dungeon Master