poj_2251_Dungeon Master_bfs

                          Dungeon Master
Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u
Submit  Status

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!



  这道题其实就是bfs的水题,2维改成3维了。
#include<iostream>

#include<cstring>
#include<queue>
using namespace std;
const int MAXN=33;
const int INF=0x3f3f3f;
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};
struct Point
{
  int x,y,z;
};
char map[MAXN][MAXN][MAXN];
int dis[MAXN][MAXN][MAXN];
int sx,sy,sz;
int ex,ey,ez;
int high,row,col;
int bfs();
int main()
{
  while(cin>>high>>row>>col)
  {
    if(high==0&&row==0&&col==0)
    break;
    for(int i=0;i<high;i++)
    for(int j=0;j<row;j++)
    {
      char s[50];
      cin>>s;    //刚开始直接cin>>map[j][k][i],,错了。
      for(int k=0;k<col;k++)
      {
        map[j][k][i]=s[k];
        if(map[j][k][i]=='S')
        {
          sx=j;sy=k;sz=i;
        }
        else if(map[j][k][i]=='E')
        {
          ex=j;ey=k;ez=i;
        }
        dis[j][k][i]=INF;
      }
    }
    int ans=bfs();
    if(ans==INF)
      cout<<"Trapped!"<<endl;
    else
      cout<<"Escaped in "<<ans<<" minute(s)."<<endl;
  }
  return 0;
}
int bfs()
{
  queue<Point>que;
  Point start;
  start.x=sx;
  start.y=sy;
  start.z=sz;
  que.push(start);
  dis[sx][sy][sz]=0;
  while(que.size())
  {
    Point p=que.front();
    que.pop();
    if(p.x==ex&&p.y==ey&&p.z==ez)
      break;
    for(int i=0;i<6;i++)
    {
      Point np;
      np.x=p.x+dx[i];
      np.y=p.y+dy[i];
      np.z=p.z+dz[i];
      if(np.x>=0&&np.x<row&&np.y>=0&&np.y<col&&np.z>=0&&np.z<high&&map[np.x][np.y][np.z]!='#'
        &&dis[np.x][np.y][np.z]==INF)
      {
        que.push(np);
        dis[np.x][np.y][np.z]=dis[p.x][p.y][p.z]+1;
      }
    }
  }
  return dis[ex][ey][ez];
}

 





你可能感兴趣的:(master)