这是一个简单的广度优先搜索题:
#include <iostream>
#include <queue>
using namespace std;
int pixel[1286][128][65];
bool visit[1286][128][65];
int dir[6][3] = {
{0, 0, -1}, {0, 0, 1},
{-1, 0, 0}, {1, 0, 0},
{0, -1, 0}, {0, 1, 0}};
int M, N, L, T;
typedef struct Vec3i{
int x, y, z;
Vec3i(){}
Vec3i(int _x, int _y, int _z): x(_x), y(_y), z(_z){}
}Vec3i;
int ConnectCheck(Vec3i &pos){
queue<Vec3i> Q;
Q.push(pos);
visit[pos.x][pos.y][pos.z] = true;
int cnt = 1;
while(!Q.empty()){
Vec3i cur = Q.front();
Q.pop();
for(int i = 0; i < 6; ++i){
Vec3i next;
next.x = cur.x + dir[i][0];
next.y = cur.y + dir[i][1];
next.z = cur.z + dir[i][2];
if(next.x >= 0 && next.x < N &&
next.y >= 0 && next.y < M &&
next.z >= 0 && next.z < L){
if(pixel[next.x][next.y][next.z] &&
!visit[next.x][next.y][next.z]){
Q.push(next);
visit[next.x][next.y][next.z] = true;
cnt++;
}
}
}
}
return cnt;
}
int main(){
while(cin>>M>>N>>L>>T){
for(int s = 0; s < L; ++s)
for(int h = 0; h < M; ++h)
for(int w = 0; w < N; ++w){
cin>>pixel[w][h][s];
visit[w][h][s] = false;
}
int cnt = 0;
for(int s = 0; s < L; ++s){
for(int h = 0; h < M; ++h){
for(int w = 0; w < N; ++w){
if(pixel[w][h][s] != 0 && !visit[w][h][s]){
Vec3i pos(w, h, s);
int _cnt = ConnectCheck(pos);
if(_cnt >= T){
cnt += _cnt;
}
}
}
}
}
cout<<cnt<<endl;
}
return 0;
}