#include<cstdio>
#include<stdlib.h>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<set>
#include<vector>
#include<map>
#include<queue>
#include<stack>
#include<string>
using namespace std;
int m,n,slice,Tsize; //每个切片大小是m*n
const int maxn=2000;
//需要使用三维数组
int pixel[1290][130][61];
//根据题目要求是1286by 128 slice<=69(切片个数:也就是z)
bool inqu[1290][130][61]={false};//记录是否访问过
//六个方向的遍历
int X[6]={0,0,0,0,1,-1};
int Y[6]={0,0,1,-1,0,0};
int Z[6]={1,-1,0,0,0,0};
struct node{
int x,y,z;
}temp;
//pop的时候进行操作是访问的时候,push的时候进行操作是入队列的时候
//判断是否需要访问
bool test(int x,int y,int z){
//越界
if(x>=m||x<0||y>=n||y<0||z>=slice||z<0)return false;
if(pixel[x][y][z]==0||inqu[x][y][z]==true) return false;
return true;
}
int DFS(int x,int y,int z){
int volume=0;
queue<node> q;
temp.x=x,temp.y=y,temp.z=z;
q.push(temp);
//push时进行inqu的赋值
inqu[x][y][z]=true;
volume++;
while(!q.empty()){
node top=q.front();//新设一个node的变量
q.pop();//只有pop出去之后再改变temp的值不会影响
for(int i=0;i<6;i++){
int newX=top.x+X[i];
int newY=top.y+Y[i];
int newZ=top.z+Z[i];
//更新的temp的节点
if(test(newX,newY,newZ)){
temp.x=newX;
temp.y=newY;
temp.z=newZ;
q.push(temp);
volume++;
inqu[newX][newY][newZ]=true;
}
}
}
return volume;
}
int main()
{
scanf("%d%d%d%d",&m,&n,&slice,&Tsize);
for(int z=0;z<slice;z++){
for(int x=0;x<m;x++){
for(int y=0;y<n;y++){
scanf("%d",&pixel[x][y][z]);
}
}
}
int totalSum=0;//计算最后的数量
for(int z=0;z<slice;z++){
for(int x=0;x<m;x++){
for(int y=0;y<n;y++){
if(test(x,y,z)){
int volume=DFS(x,y,z);
if(volume>=Tsize)totalSum+=volume;
}
}
}
}
cout<<totalSum<<endl;
return 0;
}
此题把算法笔记例题看懂加上看懂题目就能写出来了,不难