zoukankan      html  css  js  c++  java
  • NC201613 Jelly

    三维\(bfs\)模板

    const int N=110;
    struct Node
    {
        int x,y,z;
    };
    char g[N][N][N];
    int dist[N][N][N];
    int n;
    
    inline bool check(int x,int y,int z)
    {
        return x>=0 && x<n && y>=0 && y<n && z>=0 && z<n;
    }
    
    int bfs()
    {
        memset(dist,-1,sizeof dist);
        queue<Node> q;
        dist[0][0][0]=1;
        q.push({0,0,0});
    
        while(q.size())
        {
            Node t=q.front();
            q.pop();
    
            int x=t.x,y=t.y,z=t.z;
            if(x == n-1 && y == n-1 && z == n-1) return dist[x][y][z];
            for(int i=0;i<6;i++)
            {
                int a=x+dx3[i],b=y+dy3[i],c=z+dz3[i];
                if(g[a][b][c] == '*') continue;
                if(check(a,b,c) && dist[a][b][c] == -1)
                {
                    dist[a][b][c]=dist[x][y][z]+1;
                    q.push({a,b,c});
                }
            }
        }
        return -1;
    }
    
    int main()
    {
        cin>>n;
    
        for(int i=0;i<n;i++)
            for(int j=0;j<n;j++)
                scanf("%s",g[i][j]);
    
        int t=bfs();
        cout<<t<<endl;
    
        //system("pause");
    }
    
  • 相关阅读:
    J
    I题
    H
    G
    F题
    E题
    D题
    C题
    B题
    A题
  • 原文地址:https://www.cnblogs.com/fxh0707/p/13747507.html
Copyright © 2011-2022 走看看