C++ 代码
#include <iostream>
#include <queue>
using namespace std;
const int MAX_M = 1300;
const int MAX_N = 130;
const int MAX_L = 100;
int G[MAX_L][MAX_M][MAX_N];
bool visited[MAX_L][MAX_M][MAX_N];
int m, n, l, t;
struct Node {
int x, y, z;
};
int BFS(Node v) {
int dx[] = { 1, -1, 0, 0, 0, 0 };
int dy[] = { 0, 0, 1, -1, 0, 0 };
int dz[] = { 0, 0, 0, 0, 1, -1 };
queue<Node> q;
q.push(v);
visited[v.x][v.y][v.z] = true;
int cnt = 1;
while (!q.empty()) {
auto w = q.front(); q.pop();
for (int i = 0; i < 6; i++) {
int x = w.x + dx[i];
int y = w.y + dy[i];
int z = w.z + dz[i];
if (x >= 0 && x < l && y >= 0 && y < m && z >= 0 && z < n) {
if (!visited[x][y][z]&& G[x][y][z]) {
visited[x][y][z] = true;
q.push({ x, y, z });
cnt++;
}
}
}
}
return cnt;
}
int main() {
scanf("%d%d%d%d", &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++) {
scanf("%d", &G[i][j][k]);
}
}
}
int res = 0;
for (int i = 0; i < l; i++) {
for (int j = 0; j < m; j++) {
for (int k = 0; k < n; k++) {
if (!visited[i][j][k] && G[i][j][k]) {
int volume = BFS({ i, j , k});
if (volume >= t) res += volume;
}
}
}
}
printf("%d\n", res);
return 0;
}