3D dungeon(三维bfs)

        3D dungeon

题目链接 :点这里

#include<iostream>
#include<stdio.h>
#include<queue>
#include<string.h>

using namespace std;
struct LNode
{
    int x,y,z;
    int step ;
    LNode (int tx,int ty,int tz,int s)
    {
        x = tx;
        y = ty;
        z = tz;
        step = s;
    }
};
int dirx[6] = {0,0,0,0,-1,1};
int diry[6] = {0,0,1,-1,0,0};
int dirz[6] = {1,-1,0,0,0,0};
char mp[32][32][32];
int vis[32][32][32];
int l,r,c;//x  y z 轴范围
int ex,ey,ez;
int sx,sy,sz;
int bfs()
{
    LNode node(sx,sy,sz,0);
    queue<LNode> Q;
    Q.push(node);
    while(!Q.empty())
    {
        LNode now = Q.top();
        Q.pop();
        vis[now.x][now.y][now.z] = 1;
        if(now.x == ex && now.y == ey && now.z == ez) return now.step;
        for(int i =0;i<6;i++)
        {
            int x = now.x + dirx[i];
            int y = now.y + diry[i];
            int z = now.z + dirz[i];
            if(x < 0 || x >= l || y < 0 || y >= r || z  < 0 || z >= c)
                continue;
            if(!vis[x][y][z] && mp[x][y][z] != '#')
            {
                vis[x][y][z] = 1;
                LNode next(x,y,z,now.step + 1);
                Q.push(next);
            }
        }
    }
    return -1;
}
int main()
{
    while(~scanf("%d%d%d",&l,&r,&c) && l)
    {
        memset(vis,0,sizeof(vis));
        for(int i =0;i<l;i++)
        {
            for(int j =0;j<r;j++)
            {
                scanf("%s",mp[i][j]);
                for(int h = 0;h<c;h++)
                {
                    if(mp[i][j][h] == 'S')
                        sx = i,sy = j ,sz  = h;
                    if(mp[i][j][h] =='E')
                    {
                        ex = i,ey = j ,ez  = h;
                    }
                }
            }
        }
        int step = bfs();
        step == -1 ? printf("Trapped!\n") : printf("Escaped in %d minute(s).\n",step);
    }
    return 0;

}

猜你喜欢

转载自blog.csdn.net/qq_35104140/article/details/80055203