炒鸡简单的bfs

Dungeon Master

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!
你被困在3D地牢里,需要找到最快的出路!地牢是由单位立方体组成的,可以填满也可以不填满岩石。向北,向南,向东,向西,向上或向下移动一个单位需要一分钟。你不能沿着对角线移动,迷宫的四周都是坚硬的岩石。有可能逃脱吗?如果可以,需要多长时间?
输入:输入由许多地牢组成。每个地牢描述都以一行包含三个整数L, R和C开始(每个整数的大小都限制在30)。L是组成地牢的关卡数。R和C是组成每个级别计划的行和列的数量。然后会有L块R行每个包含C个字符。每个角色描述一个地牢的细胞。充满岩石的单元格用“#”表示,空单元格用“。”表示。你的起始位置用“S”表示,出口用字母“E”表示。每一层后面都有一行空白。对于L, R和C,输入以3个0结束。
输出:每个迷宫生成一行输出。如果可以到达出口,请打印“Escaped in x minute(s). ”,用最短的时间替换x。如果无法到达,则打印“Trapped!”。
说是炒鸡简单,其实我也才搞懂(尴尬),所以博主尽量将过程写详细一点。罕见的没用邻接表的一个题,可以直接用邻接矩阵。关键要记录每一个地方是否走过了,否则可能会陷入循环(博主第一次就是没考虑这个,等了半天死活没输出),还有要用一点STL里的队列queue,先进先出,否则会比较麻烦。

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <queue>
#include <algorithm>
using namespace std;
int k,n,m,sx,sy,sz,ex,ey,ez;//要定义全局变量,因为下面的自定义函数要用
char ss[31][31][31];//三维地牢
int vis[31][31][31];//记录这一格有没有被走过
int add[6][3]={{0,0,1},{1,0,0},{0,1,0},{0,0,-1},{-1,0,0},{0,-1,0}};//6种方向可以走
typedef struct
{
    int x,y,z,step;
}STU;
int check(int x,int y,int z)//若不能走,则返回1
{
    if(x<0||y<0||z<0||x>=k||y>=n||z>=m) return 1;
    else if(ss[x][y][z]=='#') return 1;
    else if(vis[x][y][z]) return 1;
    else return 0;
}
int bfs()
{
    STU a,next;
    int i;
    queue<STU>que;//定义STU类型的队列
    a.x=sx;
    a.y=sy;
    a.z=sz;
    a.step=0;
    que.push(a);//将a推入队列
    vis[sx][sy][sz]=1;//走过留记号
    while(!que.empty())
    {
        a=que.front();//a是队列首位(队列在不断更新,a也在不断变化)
        que.pop();//踢出队列首位
        if(a.x==ex&&a.y==ey&&a.z==ez) return a.step;//找到出口,返回
        for(i=0;i<6;i++)
        {
            next=a;
            next.x=a.x+add[i][0];
            next.y=a.y+add[i][1];
            next.z=a.z+add[i][2];
            if(check(next.x,next.y,next.z)) continue;//返回1,说明不能走
            vis[next.x][next.y][next.z] = 1;//走过留记号
            next.step=a.step+1;
            que.push(next);//将next推入队列
        }
    }
    return 0;
}
int main()
{
    int i,j,p;
    while(1)
    {
        scanf("%d%d%d",&k,&n,&m);
        if(k+n+m==0) break;
        for(i=0;i<k;i++)
        {
            for(j=0;j<n;j++)
            {
                scanf("%s",ss[i][j]);
                for(p=0;p<m;p++)
                {
                    if(ss[i][j][p]=='S')//找起点
                    {
                        sx=i;
                        sy=j;
                        sz=p;
                    }
                    else if(ss[i][j][p]=='E')//找终点
                    {
                        ex=i;
                        ey=j;
                        ez=p;
                    }
                }
            }
        }
        memset(vis,0,sizeof(vis));//每次清空“足迹”
        int ans;
        ans=bfs();
        if(ans) printf("Escaped in %d minute(s).\n",ans);
        else printf("Trapped!\n");//无ans,即走不到出口
    }
    return 0;
}
                

猜你喜欢

转载自blog.csdn.net/soul_mingling/article/details/87210527