POJ2251 Dungeon Master

来源:互联网 发布:打吊针庞麦郎 知乎 编辑:程序博客网 时间:2024/04/24 21:36
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 6095 Accepted: 2424

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

#include<iostream>
using namespace std;
char map[31][31][31];
struct
{
 int x,y,z,step;
}a[30*30*31];
bool flag;
int i,j,k,cnt,beg,end,l,r,c;
int dir[6][3]={{1,0,0},{-1,0,0},{0,1,0},{0,-1,0},{0,0,1},{0,0,-1}};

bool ok(int x,int y,int z)
{
 if(x>0&&x<=r&&y>0&&y<=c&&z>0&&z<=l&&map[z][x][y]=='.') return true;
 if(map[z][x][y]=='E') return true;
 return false;
}

int main()
{
 freopen("in.txt","r",stdin);
 freopen("out.txt","w",stdout);
    while(scanf("%d%d%d",&l,&r,&c))
 {
  int si,sj,sk;
  if(l==0&&r==0&&c==0) break;
  for(i=1;i<=l;i++)
    for(j=1;j<=r;j++)
      for(k=1;k<=c;k++)
       {
     cin>>map[i][j][k];
     if(map[i][j][k]=='S')
          si=i,sj=j,sk=k;
    }
     a[0].x=sj,a[0].y=sk,a[0].z=si,a[0].step=0;
     cnt=1;
     beg=0;end=1;
     flag=false;
     while(!flag&&beg<end)
     {
   for(i=beg;i<end&&!flag;i++)
   {
    for(j=0;j<6&&!flag;j++)
    if(ok(a[i].x+dir[j][0],a[i].y+dir[j][1],a[i].z+dir[j][2]))
    {
         if(map[a[i].z+dir[j][2]][a[i].x+dir[j][0]][a[i].y+dir[j][1]]=='E')
         {
       printf("Escaped in %d minute(s)./n",a[i].step+1);
                            flag=true;
      }
      a[cnt].x=a[i].x+dir[j][0];
      a[cnt].y=a[i].y+dir[j][1];
      a[cnt].z=a[i].z+dir[j][2];
      a[cnt++].step=a[i].step+1;
      map[a[i].z+dir[j][2]][a[i].x+dir[j][0]][a[i].y+dir[j][1]]='#';
    }
   }
   beg=end;
   end=cnt;
   }
  if(!flag)    printf("Trapped!/n");
 } 
 return 0;
}

原创粉丝点击