三维bfs Dungeon Master POJ - 2251

三维bfs Dungeon Master POJ - 2251

题目链接
一直莫名其妙的错误 。。。
两种方法
一种用到队列 另一种其实也和队列差不多
不用队列

#include<stdio.h>
#include<string.h>
#include<iostream>
#include<queue>
using namespace std;
struct node{
int x;
int y;
int z;
int step;
}ll[40000];
int vis[33][33][33]={0},s,ans=0;int base[6][3] = { {-1,0,0},{1,0,0},{0,-1,0},{0,1,0},{0,0,-1},{0,0,1} };    int head=1,tail=1;
int h,n,m;
char p[33][33][33];
int bfs()
{
    int cnt=1;
    head=1;tail=1;
    memset(vis,0,sizeof(vis));
    int lx,ly,lz,i,j;
    while(head<=tail){
        int Lx=ll[head].x;
        int Ly=ll[head].y;
        int Lz=ll[head].z;
        vis[Lx][Ly][Lz]=1;
        for(i=0;i<6;i++)
        {
            lx=Lx+base[i][0];
            ly=Ly+base[i][1];
            lz=Lz+base[i][2];
            if(lx>=1&&lx<=h&&ly>=1&&ly<=n&&lz>=1&&lz<=m&&vis[lx][ly][lz]==0&&p[lx][ly][lz]=='.')
            {
                vis[lx][ly][lz]=1;
                tail++;
                ll[tail].x=lx;
                ll[tail].y=ly;
                ll[tail].z=lz;
                ll[tail].step=ll[head].step+1;
            }
            if(p[lx][ly][lz]=='E')
            {
                return ll[head].step;
            }
        }

        head++;
    }
    return 0;
}
int main()
{
    int i,j,k,z,x,c;
    while(~scanf("%d%d%d",&h,&n,&m)){
        if(h==0&&n==0&&m==0)
            return 0;
    memset(p,0,sizeof(p));
    memset(ll,0,sizeof(ll));
    for(i=1;i<=h;i++)
    {
        getchar();
        for(j=1;j<=n;j++)
        {

            for(k=1;k<=m;k++)
            {
                scanf("%c",&p[i][j][k]);
                if(p[i][j][k]=='S')
                {
                    ll[1].x=i;
                    ll[1].y=j;
                    ll[1].z=k;
                    ll[1].step=1;
                }
            }
            getchar();
        }
    }
    int k=bfs();
    if(k)
        printf("Escaped in %d minute(s).\n",k);
    else
        printf("Trapped!\n");
    }
    return 0;
}


用队列的:

#include<stdio.h>
#include<string.h>
#include<iostream>
#include<queue>
using namespace std;
char ll[30][30][30];        //记录节点信息
int vis[30][30][30];        //标记是否访问
int base[6][3] = { {-1,0,0},{1,0,0},{0,-1,0},{0,1,0},{0,0,-1},{0,0,1} };
int L, R, C;
struct Piont
{
    int x, y, z;            //位置坐标
    int step;                //出发点到该点的步数
};
struct Piont s;                //起点
struct Piont curp;            //跳出循环时的节点
int bfs()
{
    memset(vis,0,sizeof(vis));
    struct Piont next;
    queue<Piont>q;
    q.push(s);
    while (!q.empty())
    {
        curp = q.front();
        q.pop();
        if (ll[curp.x][curp.y][curp.z]=='E')
            return curp.step;
        else
        {
            vis[curp.x][curp.y][curp.z] = 1;
            for (int i = 0; i < 6; i++)
            {
                next.x = curp.x + base[i][0];
                next.y = curp.y + base[i][1];
                next.z = curp.z + base[i][2];
                if ((next.x >= 0) && (next.x < L) && (next.y >= 0) && (next.y < R) && (next.z >= 0) && (next.z < C) && (!vis[next.x][next.y][next.z]) && (ll[next.x][next.y][next.z] == '.' || ll[next.x][next.y][next.z] == 'E'))//if (check(next.x, next.y, next.z))        //扩展队列
                {
                    next.step = curp.step + 1;
                    vis[next.x][next.y][next.z] = 1;
                    q.push(next);
                }
             }
        }
    }
    return 0;
}
int main()
{
    while (scanf("%d%d%d", &L, &R, &C))
    {
        if((L == 0) && (R == 0) && (C == 0))
            return 0;
        for (int i = 0; i < L; i++) {
            getchar();
            for (int j = 0; j < R; j++) {
                for (int k = 0; k < C; k++)
                {
                    scanf("%c", &ll[i][j][k]);
                    if (ll[i][j][k] == 'S') {
                        s.x = i;
                        s.y = j;
                        s.z = k;
                        s.step = 0;
                    }
                }
                getchar();
            }
        }
        int w=bfs();
        if (w)
            printf("Escaped in %d minute(s).\n", curp.step);
        else
            printf("Trapped!\n");
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_43559193/article/details/88622659
0条评论
添加一条新回复
  
今日推荐