码迷,mamicode.com
首页 > 其他好文 > 详细

POJ 2251 Dungeon Master

时间:2017-05-25 13:42:57      阅读:209      评论:0      收藏:0      [点我收藏+]

标签:let   else   ace   level   rmi   size   evel   [1]   blank   

来源: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

标签:let   else   ace   level   rmi   size   evel   [1]   blank   

原文地址:http://www.cnblogs.com/cynchanpin/p/6903038.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!