POJ 2251 Dungeon Master(BFS)

来源:互联网 发布:json格式错误怎么办 编辑:程序博客网 时间:2024/06/07 17:12

Dungeon Master

Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 27768 Accepted: 10860

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!

题意:一个三维的迷宫,判断是否可以从S到达E,如果可以到达就输出步数,否则输出Trapped!

题解:这个题目是非常重要的迷宫题,所以要重点聊聊,1、找到结束点,标记结束点;

2、定义一个记录走过的步的数组div;

3、把开始点放入栈内;开始进行走迷宫;

4、将栈内的点出栈,判断这个点的六个方向是否有路可走,如果有路可走就进栈并标记走过,如果这个点是结束点就结束;

5、每次保证在规定的范围内,通过不断的进栈和出栈,就可以得到所求的路;


#include <iostream>#include <stdio.h>#include <string.h>#include <queue>using namespace std;struct node{  int x, y, z;  int step;};int Map[35][35][35];int div[35][35][35];int main(){  int c, n, m, i, j, k;  char e[35];  while(cin>>c>>n>>m)    {      if(c==0&&n==0&&m==0)        break;      memset(div, 0, sizeof(div));      node sta;      queue<node>q;      for(i=0; i<c; i++)        {          for(j=0; j<n; j++)            {              cin>>e;              for(k=0; k<m; k++)                {                  if(e[k]=='S')                    {                      sta.x=i;                      sta.y=j;                      sta.z=k;                      sta.step=0;                      Map[i][j][k]=2;                      div[i][j][k]=1;                    }                  else if(e[k]=='E')                    {                      Map[i][j][k]=3;                    }                  else if(e[k]=='#')                    Map[i][j][k]=0;                  else if(e[k]=='.')                    Map[i][j][k]=1;                }            }        }      q.push(sta);      int  flag=0;      while(!q.empty())        {          node e=q.front();          q.pop();          if(Map[e.x][e.y][e.z]==3)            {              flag=1;              printf("Escaped in %d minute(s).\n", e.step);              break;            }          e.step++;          node e2;          if(e.x>0)            {              e2=e;              e2.x--;              if(Map[e2.x][e2.y][e2.z]!=0&&div[e2.x][e2.y][e2.z]==0)                {                  div[e2.x][e2.y][e2.z]=1;                  q.push(e2);                }            }          if(e.x<c-1)            {              e2=e;              e2.x++;              if(Map[e2.x][e2.y][e2.z]!=0&&div[e2.x][e2.y][e2.z]==0)                {                  div[e2.x][e2.y][e2.z]=1;                  q.push(e2);                }            }          if(e.y>0)            {              e2=e;              e2.y--;              if(Map[e2.x][e2.y][e2.z]!=0&&div[e2.x][e2.y][e2.z]==0)                {                  div[e2.x][e2.y][e2.z]=1;                  q.push(e2);                }            }          if(e.y<n-1)            {              e2=e;              e2.y++;              if(Map[e2.x][e2.y][e2.z]!=0&&div[e2.x][e2.y][e2.z]==0)                {                  div[e2.x][e2.y][e2.z]=1;                  q.push(e2);                }            }          if(e.z>0)            {              e2=e;              e2.z--;              if(Map[e2.x][e2.y][e2.z]!=0&&div[e2.x][e2.y][e2.z]==0)                {                  div[e2.x][e2.y][e2.z]=1;                  q.push(e2);                }            }          if(e.z<m-1)            {              e2=e;              e2.z++;              if(Map[e2.x][e2.y][e2.z]!=0&&div[e2.x][e2.y][e2.z]==0)                {                  div[e2.x][e2.y][e2.z]=1;                  q.push(e2);                }            }        }      if(flag==0)        printf("Trapped!\n");    }  return 0;}


0 0
原创粉丝点击