题目链接
分析
三维BFS,table[][][]
数组存储切片,of[][]
数组存储偏移量
对于每一个为 $1$ 的点,BFS 遍历其周围的六个值,每个已经遍历过的值设置为 $0$,cnt
记录本次 BFS 遍历的体积,最后判断是否大于 T 即可
代码实现
#include <iostream>
#include <algorithm>
#include <queue>
using namespace std;
int of[6][3] = {{-1, 0, 0}, {0, 0, -1}, {0, -1, 0}, {0, 0, 1}, {0, 1, 0}, {1, 0, 0}};
bool table[61][1291][131];
int n, m, l, t, ans;
struct node
{
int x, y, z;
node(int a, int b, int c) : x(a), y(b), z(c){};
};
void bfs(int x, int y, int z)
{
queue<node> q;
q.push(node(x, y, z));
int cnt = 1;
table[x][y][z] = 0;
while (!q.empty())
{
node tn = q.front();
q.pop();
int tx = tn.x, ty = tn.y, tz = tn.z;
for (int i = 0; i < 6; ++i)
{
tx = tn.x + of[i][0];
ty = tn.y + of[i][1];
tz = tn.z + of[i][2];
if (!(tx < 0 ty < 0 tz < 0 tx > l ty > n tz > m) && table[tx][ty][tz])
{
cnt++;
table[tx][ty][tz] = 0;
q.push(node(tx, ty, tz));
}
}
}
if (cnt >= t)
ans += cnt;
}
int main()
{
cin >> n >> m >> l >> t;
for (int i = 0; i < l; ++i)
for (int j = 0; j < n; ++j)
for (int k = 0; k < m; ++k)
cin >> table[i][j][k];
for (int i = 0; i < l; ++i)
for (int j = 0; j < n; ++j)
for (int k = 0; k < m; ++k)
if (table[i][j][k])
bfs(i, j, k);
cout << ans << endl;
return 0;
}