POJ-2251,Dungeon Master(三维迷宫BFS)

简介: POJ-2251,Dungeon Master(三维迷宫BFS)

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!


解题思路:


输入由多个地牢组成。每个地牢描述以包含三个整数L、R和C(大小都限制在30以内)的行开头。 L是组成地牢的等级数。 R和C是构成每一层平面图的行数和列数。接下来是L个R行块,每个R行包含C个字符。‘#’是岩石不能走,‘.’是空单元格可以行走,你的起始位置用‘S’表示,而字母‘E’的出口表示。每一级后面都有一个空行。对于L、R和C,输入端以三个零结束。


需要计算的是逃出迷宫需要的最短时间是多少,或者是无法逃出迷宫,按照题目样例输出打印结果即可,根据题意这是一个三维迷宫,一共有六个方向可以行走,这六个点的坐标一直要想清楚(详细的看代码),后面的就直接BFS!!!


程序代码:  


#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
#define maxn 50
char s[maxn][maxn][maxn];
int vis[maxn][maxn][maxn];
int l,r,c;
int sx,sy,sk;
int ex,ey,ek;
int dx[]={1,-1,0,0,0,0};
int dy[]={0,0,1,-1,0,0};
int dk[]={0,0,0,0,1,-1};
struct point
{
  int x,y,k;
};
int bfs(int sx,int sy,int sk)
{
  queue<point> que;
  point t;
  t.x=sx;
  t.y=sy;
  t.k=sk;
  que.push(t);//入队
  while(!que.empty())
  {
    point p=que.front();//取队首
    que.pop();//等弹出队首
    if(p.x==ex&&p.y==ey&&p.k==ek)//如果到达终点
      return vis[ex][ey][ek];
    for(int i=0;i<6;i++)
    {
      int nx=p.x+dx[i];//这三行用来更新位置坐标
      int ny=p.y+dy[i];
      int nk=p.k+dk[i];
      if(nx>=1&&nx<=l&&ny>=1&&ny<=r&&nk>=1&&nk<=c&&//不越界,也不是岩石
        vis[nx][ny][nk]==0&&s[nx][ny][nk]!='#')
      {
        vis[nx][ny][nk]=vis[p.x][p.y][p.k]+1;
        t.x=nx;
        t.y=ny;
        t.k=nk;
        que.push(t);//继续让t入队,p取队首往后搜索
      }
    }
  }
  return vis[ex][ey][ek];
}
int main()
{
  while(cin>>l>>r>>c)
  {
    memset(vis,0,sizeof(vis));//一定要清空!!!
    memset(s,0,sizeof(s));
    if(l==0&&r==0&&c==0)
      break;
    for(int i=1;i<=l;i++)
    {
      for(int j=1;j<=r;j++)
      {
        for(int k=1;k<=c;k++)
        {
          cin>>s[i][j][k];//三维
          if(s[i][j][k]=='S')
          {
            sx=i;
            sy=j;
            sk=k;
          }
          else if(s[i][j][k]=='E')
          {
            ex=i;
            ey=j;
            ek=k;
          }
        }
      }
    }
    int ans=bfs(sx,sy,sk);
    if(ans==0)
      cout<<"Trapped!"<<endl;
    else
      cout<<"Escaped in "<<ans<<" minute(s)."<<endl;
  }
  return 0;
}


相关文章
|
6月前
【每日一题Day244】LCP 41. 黑白翻转棋 bfs dfs
【每日一题Day244】LCP 41. 黑白翻转棋 bfs dfs
54 0
|
6月前
|
存储 算法
使用 BFS 解决走迷宫问题
使用 BFS 解决走迷宫问题
75 1
|
6月前
|
算法 Java C++
走迷宫(BFS)
走迷宫(BFS)
43 0
Dungeon Master(BFS广度优先搜索)
Dungeon Master(BFS广度优先搜索)
40 0
|
存储 定位技术 C++
【31. 走迷宫(BFS)】
## 思路 - 用 `g[n][m] `存储地图,`d[n][m]` 存储起点到 n,m 的距离。 - 从起点开始广度优先遍历地图。 - 当地图遍历完,就求出了起点到各个点的距离,输出`d[n][m]`即可。
292 0
【31. 走迷宫(BFS)】
|
机器学习/深度学习 算法
【刷穿 LeetCode】求「连通图经过所有点的最短路径」的三种方式 :「BFS」&「Floyd + 状压 DP」 &「AStar 算法」
【刷穿 LeetCode】求「连通图经过所有点的最短路径」的三种方式 :「BFS」&「Floyd + 状压 DP」 &「AStar 算法」
|
机器学习/深度学习
POJ-1321,棋盘问题(DFS)
POJ-1321,棋盘问题(DFS)