题目链接如上:
就是求连通块的总和是多少。 如果一个连通块加起来大于等于t 就ans+=连通块的面积。
简单的BFS
#include <bits/stdc++.h>
//#include <ext/pb_ds/tree_policy.hpp>
//#include <ext/pb_ds/assoc_container.hpp>
//using namespace __gnu_pbds;
using namespace std;
#define LL long long
#define INF 1E4 * 1E9
#define pi acos(-1)
#define endl '\n'
#define me(x) memset(x,0,sizeof(x));
#define str(x) strlen(x);
#define fread() freopen("in.in","r",stdin)
#define fwrite() freopen("out.out","w",stdout)
const int maxn=1e6+5;
const int maxx=2e4+5;
const double EPS=1e-2;
//typedef tree<pt,null_type,less< pt >,rb_tree_tag,tree_order_statistics_node_update> rbtree;
int dx[]={1,-1,0,0,0,0};
int dy[]={0,0,1,-1,0,0};
int dz[]={0,0,0,0,1,-1};
int n,m,l,T;
int mapp[65][130][1300];
int ans=0;
struct node
{
int x,y,z;
};
int check(int x,int y,int z)
{
if((x<=0)||(y<=0)||(z<=0))
return 0;
if((x>l)||(y>n)||(z>m))
return 0;
return 1;
}
queue<node > Q;
void bfs(int xx,int yy,int zz)
{
node t,q;
mapp[xx][yy][zz]=0;
t.x=xx;t.y=yy;t.z=zz;
Q.push(t);
int num=1;
while(!Q.empty())
{
q=Q.front();
Q.pop();
for(int i=0;i<6;i++)
{
int tx=q.x+dx[i];
int ty=q.y+dy[i];
int tz=q.z+dz[i];
if(check(tx,ty,tz)&&mapp[tx][ty][tz])
{
num++;
t.x=tx;t.y=ty;t.z=tz;
mapp[tx][ty][tz]=0;
Q.push(t);
}
}
}
if(num>=T)
ans+=num;
}
int main()
{
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>>mapp[i][j][k];
for(int i=1;i<=l;i++)
for(int j=1;j<=n;j++)
for(int k=1;k<=m;k++)
if(mapp[i][j][k]==1) bfs(i,j,k);
cout<<ans<<endl;
}