题意:三维图 找符合条件的1的数目 条件:如果由1组成的联通快的1的数目>=t 那么这个联通块的所有1都符合条件。
看图就知道如果 前后左右上下有相邻的1:这两个1联通
dfs
#include <cstdio>
#include <algorithm>
#include <math.h>
#include <string.h>
#include <vector>
#include <map>
#include <iostream>
#define ll long long
#define INF 0x3f3f3f
using namespace std;
const int N=100000+500;
int s[65][1290][130];
int vis[65][1290][130];
int dx[6]={0,0,1,-1,0,0};
int dy[6]={0,0,0,0,1,-1};
int dh[6]={1,-1,0,0,0,0};
int n,m,l,cnt;
void dfs(int h,int x,int y)
{
int xx,yy,hh,i;
cnt++;
vis[h][x][y]=1;
for(i=0;i<6;i++)
{
xx=dx[i]+x;yy=dy[i]+y;hh=dh[i]+h;
if(hh<0 || hh>=l || xx<0 ||xx>=n ||yy<0 ||yy>=m) continue;
if(vis[hh][xx][yy]==1 || s[hh][xx][yy]==0) continue;
dfs(hh,xx,yy);
}
}
int main()
{
int t,i,j,k,ans;
ans=0;
scanf("%d%d%d%d",&n,&m,&l,&t);
for(i=0;i<l;i++)
for(j=0;j<n;j++)
for(k=0;k<m;k++)
scanf("%d",&s[i][j][k]);
for(i=0;i<l;i++)
{
for(j=0;j<n;j++)
{
for(k=0;k<m;k++)
{
if(!vis[i][j][k] && s[i][j][k]==1)
{
cnt=0;
dfs(i,j,k);
if(cnt>=t) ans+=cnt;
}
}
}
}
printf("%d\n",ans);
}
bfs
#include <cstdio>
#include <algorithm>
#include <math.h>
#include <string.h>
#include <queue>
#include <iostream>
#define ll long long
#define INF 0x3f3f3f
using namespace std;
const int N=100000+500;
int s[65][1290][130];
int vis[65][1290][130];
int dx[6]={0,0,0,0,-1,1};
int dy[6]={0,0,-1,1,0,0};
int dh[6]={1,-1,0,0,0,0};
int n,m,l,cnt;
struct node
{
int h,x,y;
}p,now,nextt;
queue<node> q;
void bfs(int h,int x,int y)
{
int i,hh,xx,yy,nowh,nowx,nowy;
now.x=x;now.y=y;now.h=h;
vis[h][x][y]=1;
q.push(now);
cnt=1;
while(!q.empty())
{
now=q.front();
q.pop();
nowh=now.h,nowx=now.x;nowy=now.y;
for(i=0;i<6;i++)
{
hh=nowh+dh[i]; xx=nowx+dx[i]; yy=nowy+dy[i];
if(hh<0 || hh>=l ||xx<0 ||xx>=n ||yy<0 ||yy>=m) continue;
if(vis[hh][xx][yy] || s[hh][xx][yy]==0) continue;
nextt.h=hh;nextt.x=xx;nextt.y=yy;
q.push(nextt);
vis[hh][xx][yy]=1;
cnt++;
}
}
}
int main()
{
int t,i,j,k,ans;
ans=0;
scanf("%d%d%d%d",&n,&m,&l,&t);
for(i=0;i<l;i++)
for(j=0;j<n;j++)
for(k=0;k<m;k++)
scanf("%d",&s[i][j][k]);
for(i=0;i<l;i++)
{
for(j=0;j<n;j++)
{
for(k=0;k<m;k++)
{
if(!vis[i][j][k] && s[i][j][k]==1)
{
bfs(i,j,k);
if(cnt>=t) ans+=cnt;
}
}
}
}
printf("%d\n",ans);
}