我是靠谱客的博主 认真绿草,最近开发中收集的这篇文章主要介绍寒假集训 最简单的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数组。

最后

以上就是认真绿草为你收集整理的寒假集训 最简单的bfs模板题Dungeon Master题解的全部内容,希望文章能够帮你解决寒假集训 最简单的bfs模板题Dungeon Master题解所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(45)

评论列表共有 0 条评论

立即
投稿
返回
顶部