Dungeon Master

                                             Dungeon Master

                                           Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other)
Problem 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
PKU
 

题意:在一个立体的囚牢里,我们可以把其看作是一个L*R*C的立方体,其中每一个小位置的体积是1*1*1,这样,S表示起始位置,E代表出口,在这样的立方体中,问是否存在一条通路使的可以从S通到E。若能,输出走的步数,不能则输出“Trapped!”。

思路:广搜,注意记录已经访问过的位置。

源代码:

 

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
struct move
{
    int l,r,c;
    int min;     //记录到该步走的步数  
}s,e,mov[30000];
char map[40][40][40];
int l,r,c,ans;     
bool vis[40][40][40];   //记录访问的情况
int str[6][3]={{1,0,0},{-1,0,0},{0,1,0},{0,-1,0},{0,0,1},{0,0,-1}};    //走的方向
int bfs()
{
    int L,R,C;
    int front=0,rear=1;
    mov[front].c=s.c;
    mov[front].l=s.l;
    mov[front].r=s.r;
    vis[s.l][s.r][s.c]=true;
    while(front!=rear)
    {
        for(int i=0;i<6;i++)    //六个方向 广搜
        {
            L=mov[front].l+str[i][0];
            R=mov[front].r+str[i][1];
            C=mov[front].c+str[i][2];
            if(L>=0&&L<l&&R>=0&&R<r&&C>=0&&C<c&&!vis[L][R][C]&&map[L][R][C]!='#')    //判断是否是通路
            {
                if(L==e.l&&R==e.r&&C==e.c)   //结束条件
                {
                    ans=mov[front].min+1;
                    return 1;
                }
                mov[rear].l=L;
                mov[rear].r=R;
                mov[rear].c=C;
                vis[L][R][C]=1;
                mov[rear].min=mov[front].min+1;
                rear++;
            }
        }
        front++;
    }
    return 0;
}

int main()
{
    int i,j,k;
    while(cin>>l>>r>>c,c+l+r)
    {
        memset(vis,false,sizeof(vis));    
        for(i=0;i<l;i++)
          for(j=0;j<r;j++)
            for(k=0;k<c;k++)       //输入
            {
                cin>>map[i][j][k];
                if(map[i][j][k]=='S')    //寻找 S
                {
                    s.l=i;
                    s.r=j;
                    s.c=k;
                }
                if(map[i][j][k]=='E')   //寻找 E
                {
                    e.l=i;
                    e.r=j;
                    e.c=k;
                }
            }
        if(bfs())
           cout<<"Escaped in "<<ans<<" minute(s)."<<endl;
        else
           cout<<"Trapped!"<<endl;
    }
}

 

 

 

 

 

 

 

 

posted on 2013-10-05 15:28  天梦Interact  阅读(440)  评论(0编辑  收藏  举报