寒假集训 最简单的bfs模板题Dungeon Master题解

其实说是模板题,作为一个萌新我还是改了好久好久,问了好几次大佬,去晚上参考别人的代码好不容易A了,人生第一道bfs题
开心开心。
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!
题目很简单,就是让从起始点走到最后,这个不需要考虑是否为最短路径,因为只要能走到就一定是最短路径。那么上代码
     
     
#include <iostream> #include<queue> #include <stdio.h> #include<algorithm>
using namespace std; //#define N 32 struct position {     int x,y,z,step; };
char mmap[N][N][N]; int xs,ys,zs; int l,r,c;
int dx[6]= {0, 0,1,-1,0, 0}; int dy[6]= {0, 0,0, 0,1,-1}; int dz[6]= {1,-1,0, 0,0, 0}; int bfs(position now) {     queue<position> qq;     qq.push(now);     while(qq.size())     {         now=qq.front();         qq.pop();         if(mmap[now.z][now.x][now.y]=='E')             return now.step;         for(int i=0; i<6; i++)         {             position nex=now;             nex.x+=dx[i];             nex.y+=dy[i];             nex.z+=dz[i];             nex.step++;             if(nex.x>=0&&nex.y>=0&&nex.z>=0&&nex.x<r&&nex.y<c&&nex.z<l&&mmap[nex.z][nex.x][nex.y]!='#')             {                 qq.push(nex);                 if(mmap[nex.z][nex.x][nex.y]=='E')                     return nex.step;                 else                     mmap[nex.z][nex.x][nex.y]='#';             }         }     }     return -1; } int main() {     //freopen("in.txt","r",stdin);     while(1)     {     scanf("%d%d%d",&l,&r,&c);         if(l==0&&r==0&&c==0)             break;     for(int n=0; n<l; n++)         for(int i=0; i<r; i++)         {             scanf("%s",mmap[n][i]);         }     position s;      for(int n=0; n<l; n++)         for(int i=0; i<r; i++)             for(int j=0; j<c; j++)                 if(mmap[n][i][j]=='S')                 {                     s.step=0;                     s.x=i;                     s.y=j;                     s.z=n;                 }     int ans=bfs(s);     if(ans!=-1)         printf("Escaped in %d minute(s).\n",ans);     else         printf("Trapped!\n");     }     return 0; }
其实我的里面有个不好的地方,我没开visit数组,开了就不需要重复去判断了,因为我每走一步就会把之前的点变成#,这就导致我的E点可能会被变成#,所以要多进行一次判断。但其实如果开了vis数组记录这个点走没走过,就不需要再多判断一次了。但是懒得改了!啊好懒,下一道bfs的题马上就会来了,下次尽量用上vis数组。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值