zoukankan      html  css  js  c++  java
  • PAT (Advanced Level) 1091. Acute Stroke (30)

    BFS求连通块。递归会爆栈。

    #include<cstdio>
    #include<cstring>
    #include<cmath>
    #include<algorithm>
    #include<map>
    #include<queue>
    #include<stack>
    #include<vector>
    using namespace std;
    
    int M,N,L,T;
    int A[70][1300][133];
    bool f[70][1300][133];
    
    int dir[10][5]={
        {0,0,1},
        {0,0,-1},
        {0,1,0},
        {0,-1,0},
        {1,0,0},
        {-1,0,0}
    };
    
    struct Point
    {
        int a,b,c;
        Point (int aa,int bb,int cc)
        {
            a=aa; b=bb; c=cc;
        }
    };
    
    int Find(int a,int b,int c)
    {
        int res=0;
        queue<Point>q;
    
        Point p(a,b,c);
        q.push(p);
        f[a][b][c]=1;
    
        while(!q.empty())
        {
            res++;
            Point head=q.front(); q.pop();
            for(int i=0;i<6;i++)
            {
                int na=head.a+dir[i][0];
                int nb=head.b+dir[i][1];
                int nc=head.c+dir[i][2];
    
                if(na>L||na<1) continue;
                if(nb>M||nb<1) continue;
                if(nc>N||nc<1) continue;
    
                if(A[na][nb][nc]==0) continue;
                if(f[na][nb][nc]==1) continue;
    
                Point p(na,nb,nc);
                q.push(p);
                f[na][nb][nc]=1;
            }
        }
        return res;
    }
    
    int main()
    {
        memset(A,0,sizeof A);
        memset(f,0,sizeof f);
        scanf("%d%d%d%d",&M,&N,&L,&T);
        for(int k=1;k<=L;k++)
            for(int i=1;i<=M;i++)
                for(int j=1;j<=N;j++)
                    scanf("%d",&A[k][i][j]);
        int sum=0;
        for(int k=1;k<=L;k++)
            for(int i=1;i<=M;i++)
                for(int j=1;j<=N;j++)
                {
                    if(A[k][i][j]==0) continue;
                    if(f[k][i][j]==1) continue;
                    int res=Find(k,i,j);
                    if(res>=T) sum=sum+res;
                }
        printf("%d
    ",sum);
    
        return 0;
    }
  • 相关阅读:
    洛谷 题解 P5595 【【XR-4】歌唱比赛】
    洛谷 题解 CF1151D 【Stas and the Queue at the Buffet】
    洛谷 题解 CF299A 【Ksusha and Array】
    仙人掌找环
    2-SAT
    带花树
    帮我背单词
    csp2019退役祭
    P5284 [十二省联考2019]字符串问题 题解
    【网络流24题】魔术球问题
  • 原文地址:https://www.cnblogs.com/zufezzt/p/5641645.html
Copyright © 2011-2022 走看看