poj 2251 Dungeon Master (BFS)

来源:互联网 发布:网络工商营业执照 编辑:程序博客网 时间:2024/05/20 18:49
Dungeon Master
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 26644 Accepted: 10394

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 5S.....###..##..###.#############.####...###########.#######E1 3 3S###E####0 0 0

Sample Output

Escaped in 11 minute(s).Trapped!

Source

Ulm Local 1997


题意:你被困在一个3D地牢里,想要逃出地牢,你有六个逃跑方向:上、下、左、右、前和后,S为你的起始位置,E为出口,"."为路,"#"为墙,若你能逃出,求出你能逃出的最短时间。

题解:其实和二维思想的基本一样,不过是要建立一个三维数组,求法和二维的一样。-----BFS-----


<pre name="code" class="cpp"><span style="font-family:SimSun;font-size:18px;">#include<stdio.h>#include<string.h>#include<algorithm>#include<queue>using namespace std;int a,b,c,t,vis[33][33][33],sx,sy,sz,ex,ey,ez;char map[33][33][33];struct node{    int x,y,z,step;};int dx[6]={1,-1,0,0,0,0};int dy[6]={0,0,1,-1,0,0};int dz[6]={0,0,0,0,1,-1};int bfs(int x,int y,int z){    int i;    queue<node>Q;    node p,q;    p.x=x;    p.y=y;    p.z=z;    p.step=0;    Q.push(p);    while(!Q.empty())    {        p=Q.front();        Q.pop();        if(p.x==ex&&p.y==ey&&p.z==ez)return p.step;        for(i=0;i<6;i++)        {            q=p;            q.x=p.x+dx[i];            q.y=p.y+dy[i];            q.z=p.z+dz[i];            if(q.x<=0||q.y<=0||q.z<=0||q.x>a||q.y>b||q.z>c)continue;            if(!vis[q.x][q.y][q.z]&&map[q.x][q.y][q.z]!='#')            {                q.step++;                vis[q.x][q.y][q.z]=1;                Q.push(q);            }        }    }    return 0;}int main(){    int i,j,k;       while(scanf("%d%d%d",&a,&b,&c),a||b||c)    {        memset(map,0,sizeof(map));        memset(vis,0,sizeof(vis));        getchar();    for(i=1;i<=a;i++)    {    for(j=1;j<=b;j++)      {      for(k=1;k<=c;k++)      {          scanf("%c",&map[i][j][k]);          if(map[i][j][k]=='S')  {              sx=i;sy=j;sz=k;          }          if(map[i][j][k]=='E')  {              ex=i;ey=j;ez=k;          }      }        getchar();  }  getchar();}       int ans;    vis[sx][sy][sz]=1;    ans=bfs(sx,sy,sz);    if(!ans)printf("Trapped!\n");    else {    printf("Escaped in %d minute(s).\n",ans);    }    }           return 0;}</span>


0 0