• POJ 2251 bfs


    妈哒受不鸟了!!状况百出就是A不了啊shit!!!

    题意: 三维数组,从S走到E,"."是可走的.

    AC代码:

    #include <stdio.h>
    #include <string.h>
    #include <queue>
    #include <algorithm>
    char s[35][35][35];
    int vis[35][35][35];
    const int dx[6]= {-1,1,0,0,0,0};
    const int dy[6]= {0,0,-1,1,0,0};
    const int dz[6]= {0,0,0,0,-1,1};
    using namespace std;
    int ans,n,m,z;
    struct node
    {
        int x;
        int y;
        int z;
        int cnt;
    } ds,de,k;
    int bfs()
    {
        queue<node>q;
        q.push(ds);
        while(!q.empty())
        {
            node s1=q.front();//重命名的错误再一次发生,是时候规范一下写法了;
            q.pop();
            for(int i=0; i<6; i++)
            {
                k.x=s1.x+dz[i];
                k.y=s1.y+dx[i];
                k.z=s1.z+dy[i];
                k.cnt=s1.cnt+1;
                if(k.x==de.x&&k.y==de.y&&k.z==de.z)//搜索的时候一定要想清楚情况,不能想当然.
                {
                    return k.cnt;
                }
                int x1=k.x;
                int y1=k.y;
                int z1=k.z;
                if(x1>=0&&x1<z&&y1>=0&&y1<n&&z1>=0&&z1<m&&s[x1][y1][z1]=='.'&&vis[x1][y1][z1]==0)
                {
                    vis[x1][y1][z1]=1;
                    q.push(k);
                }
            }
        }
        return 0;
    }
    int main()
    {
        while(~scanf("%d%d%d",&z,&n,&m))
        {
            if(n==0&&m==0&&z==0)
            {
                break;
            }
            memset(vis,0,sizeof(vis));
            for(int i=0; i<z; i++)//三维数组,由于输入的限制,必须是层数在最外面.
            {
                for(int j=0; j<n; j++)
                {
                    scanf("%s",s[i][j]);
                    for(int k=0; k<m; k++)
                    {
                        if(s[i][j][k]=='S')
                        {
                            ds.x=i;
                            ds.y=j;
                            ds.z=k;
                            ds.cnt=0;
                        }
                        else if(s[i][j][k]=='E')
                        {
                            de.x=i;
                            de.y=j;
                            de.z=k;
                        }
                    }
                }
            }
            int ans=bfs();
            if(ans)
            {
                printf("Escaped in %d minute(s).
    ",ans);
            }
            else
            {
                printf("Trapped!
    ");
            }
        }
        return 0;
    }
  • 相关阅读:
    Go学习2-切片
    Go学习1-MOD
    lua学习之逻辑运算符not,and,or
    google protobuf c++ 反射
    我要谴责一下,你们复制别人的答案好歹仔细看看
    远程登录redis
    openssl进行RSA加解密(C++)
    linux通过进程名查看其占用端口
    简体字丶冯|服务网关kong-docker安装
    简体字冯|docker-安装docker私有库
  • 原文地址:https://www.cnblogs.com/qioalu/p/4916890.html
Copyright © 2020-2023  润新知