• Uva532(三维搜索)


    题目链接https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=473

    = =这题就是就是给你一个三维的图,然后给你一个起点,一个终点,中间有障碍= =问你是否能够从起点走到终点,如果能最少的步数是多少

    最优解= =用bfs,因为是三维,所以用三个方向数组来来确定下一步走哪里

    #include<stdio.h>
    #include<string.h>
    #include<iostream>
    #include<queue> 
    #include<algorithm>
    #define N 33
    using namespace std;
    int f[N][N][N];
    int x[6]={1,-1,0,0,0,0},y[6]={0,0,1,-1,0,0},z[6]={0,0,0,0,-1,1};
    struct node
    {
        int x,y,z,step;
    };
    int sx,sy,sz,ex,ey,ez;
    int n,m,k;
    int flag;
    int bfs()
    {
        queue<node>q;
        node in,out;
        in.x=sx,in.y=sy,in.z=sz;
        f[sx][sy][sz]=1;
        in.step=0;
        q.push(in);    
        while(!q.empty())
        {
            in=q.front();
            q.pop(); 
            if(in.x==ex&&in.y==ey&&in.z==ez)
            {
            flag=1;
            return in.step;
            }
            for(int i=0;i<6;i++)
            {
                out.x=in.x+x[i];
                out.y=in.y+y[i];
                out.z=in.z+z[i];
                out.step=in.step; 
                if(out.x>=0&&out.x<n&&out.y>=0&&out.y<m&&out.z>=0&&out.z<k&&!f[out.x][out.y][out.z])
                { 
                f[out.x][out.y][out.z]=1; 
                out.step++;   
                q.push(out); 
                } 
            }
        }
    }
    int main()
    {
        while(scanf("%d%d%d",&k,&n,&m)!=EOF&&n&&m&&k)
        {
            flag=0;
            char s[100];
            memset(f,0,sizeof(f));
            for(int i=0;i<k;i++)
            {
                for(int j=0;j<n;j++)
                {
                    scanf("%s",s);
                    for(int l=0;l<m;l++)
                    {
                        if(s[l]=='S')
                        {
                        sx=j;
                        sy=l;
                        sz=i;
                        }
                        else if(s[l]=='E')
                        {
                        ex=j;
                        ey=l;
                        ez=i;
                        }
                        else if(s[l]=='#')
                        f[j][l][i]=1;
                    }
                }
            }
            int ans=bfs();
            if(flag)
            printf("Escaped in %d minute(s).
    ",ans);
            else
            printf("Trapped!
    ");
        }
        return 0;
    }
  • 相关阅读:
    团队冲刺第五天
    每日学习
    团队冲刺第四天
    团队冲刺第三天
    每日学习
    2021.4.12
    2021.4.10
    2021.3.18
    2021.3.15
    2021.3.14
  • 原文地址:https://www.cnblogs.com/NaCl/p/4787485.html
Copyright © 2020-2023  润新知