The Largest Generation (25)(BFS)(PAT甲级)

#include<bits/stdc++.h>
using namespace std;
int n,m,l,t;
int a[1307][137][67];
int vis[1307][137][67];
typedef struct{
    int x,y,z;
}node;
int xx[6]={0,0,0,0,1,-1};
int yy[6]={0,0,1,-1,0,0};
int zz[6]={1,-1,0,0,0,0};
node p;
int bfs(int x,int y,int z){
    queue<node>q;
    vis[x][y][z]=1;
    p.x=x;
    p.y=y;
    p.z=z;
    q.push(p);
    int sum=0;
    while(!q.empty()){
        node tmp=q.front();
        q.pop();
        sum++;
        for(int i=0;i<6;++i){
            int tx=tmp.x+xx[i];
            int ty=tmp.y+yy[i];
            int tz=tmp.z+zz[i];
            if(tx>0&&tx<=n&&ty>0&&ty<=m&&tz>0&&tz<=l&&a[tx][ty][tz]&&!vis[tx][ty][tz]){
                p.x=tx;
                p.y=ty;
                p.z=tz;
                q.push(p);
                vis[tx][ty][tz]=1;
            }
        }
    }
    if(sum>=t)
        return sum;
    return 0;
}
int main(){
    std::ios::sync_with_stdio(false);
    cin>>n>>m>>l>>t;
    for(int i=1;i<=l;++i)
        for(int j=1;j<=n;++j)
            for(int k=1;k<=m;++k)
                cin>>a[j][k][i];
    int ans=0;
    for(int i=1;i<=l;++i)
        for(int j=1;j<=n;++j)
            for(int k=1;k<=m;++k)
                if(a[j][k][i]&&!vis[j][k][i])
                    ans+=bfs(j,k,i);
    cout<<ans;
    return 0;
}

原文地址:https://www.cnblogs.com/ldudxy/p/10841620.html

时间: 2024-10-11 14:28:48

The Largest Generation (25)(BFS)(PAT甲级)的相关文章

1094. The Largest Generation (25) 此题和六度空间一个道理,记录BFS的层次

1094. The Largest Generation (25) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue A family hierarchy is usually presented by a pedigree tree where all the nodes on the same level belong to the same generation. Your task is to find

1094. The Largest Generation (25)【二叉树】——PAT (Advanced Level) Practise

题目信息 1094. The Largest Generation (25) 时间限制200 ms 内存限制65536 kB 代码长度限制16000 B A family hierarchy is usually presented by a pedigree tree where all the nodes on the same level belong to the same generation. Your task is to find the generation with the

pat1094. The Largest Generation (25)

1094. The Largest Generation (25) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue A family hierarchy is usually presented by a pedigree tree where all the nodes on the same level belong to the same generation. Your task is to find

PAT (Advanced Level) 1094. The Largest Generation (25)

简单DFS. #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> #include<map> #include<queue> #include<stack> #include<vector> using namespace std; const int maxn=200; int n,m; vector<int>

1094. The Largest Generation (25)

A family hierarchy is usually presented by a pedigree tree where all the nodes on the same level belong to the same generation. Your task is to find the generation with the largest population. Input Specification: Each input file contains one test ca

1006.Sign in and Sign out(25)—PAT 甲级

At the beginning of every day, the first person who signs in the computer room will unlock the door, and the last one who signs out will lock the door. Given the records of signing in's and out's, you are supposed to find the ones who have unlocked a

PAT甲级题分类汇编——树

AVL树好难!(其实还好啦~) 我本来想着今天应该做不完树了,没想到电脑里有一份讲义,PPT和源代码都有,就一遍复习一遍抄码了一遍,更没想到的是编译一遍通过,再没想到的是运行也正常,最没想到的是一遍AC. 其实很多题都有数,std::set 之类用的是红黑树,据说很复杂,比AVL树还要复杂的那种.但是,用到这些设施的题,都不在这一分类下,这一分类下的题,因为题目要求自己建树,也就不用标准库设施了. 大多数题中,树在内存中都是连续存放的.不是像完全二叉树那样的连续,是物理上连续而逻辑上用数组下表代

PAT 1094 The Largest Generation[bfs][一般]

1094 The Largest Generation(25 分) A family hierarchy is usually presented by a pedigree tree where all the nodes on the same level belong to the same generation. Your task is to find the generation with the largest population. Input Specification: Ea

【PAT甲级】1070 Mooncake (25 分)(贪心水中水)

题意: 输入两个正整数N和M(存疑M是否为整数,N<=1000,M<=500)表示月饼的种数和市场对于月饼的最大需求,接着输入N个正整数表示某种月饼的库存,再输入N个正数表示某种月饼库存全部出手的利润.输出最大利润. trick: 测试点2可能包含M不为整数的数据.(尽管题面说明M是正整数,可是根据从前PAT甲级题目的经验,有可能不是整数.....) 代码: #define HAVE_STRUCT_TIMESPEC#include<bits/stdc++.h>using names