题目

题意:给定一个三维数组,0表示正常1表示有肿瘤,肿瘤块的大小大于等于t才算作是肿瘤,让计算所有满足肿瘤块的大小

tip:BFS

#include<iostream>
#include<queue>
using namespace std;
struct ss {
	int x,y,z;
};
int image[1300][130][60];
bool checked[1300][130][60];
int X[6] = {1, 0, 0, -1, 0, 0};
int Y[6] = {0, 1, 0, 0, -1, 0};
int Z[6] = {0, 0, 1, 0, 0, -1};
int m,n,l,t;
bool judge(int x,int y,int z) {
	if(x<0||x>=m||y<0||y>=n||z<0||z>=l)//越界 
		return false;
	if(!image[x][y][z]||checked[x][y][z])//遍历过或没毛病 
		return false;
	return true;
}
int bfs(int x,int y,int z) {
	int count=0;
	queue<struct ss> q;
	q.push({x,y,z});
	checked[x][y][z]=true;
	while(!q.empty()) {
		struct ss t=q.front();
		q.pop();
		count++;
		for(int i=0; i<6; ++i) {
			int a=t.x+X[i];
			int b=t.y+Y[i];
			int c=t.z+Z[i];
			if(judge(a,b,c)) {
				checked[a][b][c]=true;
				q.push({a,b,c});
			}
		}
	}
	if(count>=t)
		return count;
	return 0;
}
int main() {
	cin>>m>>n>>l>>t;
	for(int i=0; i<l; ++i)
		for(int j=0; j<m; ++j)
			for(int k=0; k<n; ++k)
				cin>>image[j][k][i];
	int ans=0;
	for(int i=0; i<l; ++i)
		for(int j=0; j<m; ++j)
			for(int k=0; k<n; ++k)
				if(image[j][k][i]&&!checked[j][k][i])
					ans+=bfs(j,k,i);
	cout<<ans<<endl;
	return 0;
}