https://www.acwing.com/problem/content/1583/
注意:
三维的时候有六个方向。
#include <iostream>
#include <cstring>
#include <queue>
using namespace std;
const int M = 1286, N = 128, L = 60;
int m, n, l, T;
int g[L][M][N];
struct Node
{
int x, y, z;
};
int d[][3] = {
{1, 0, 0},
{-1, 0, 0},
{0, 1, 0},
{0, -1, 0},
{0, 0, 1},
{0, 0, -1},
};
int bfs(int x, int y, int z)
{
queue<Node> q;
q.push({x, y, z});
g[x][y][z] = 0;
int cnt = 1;
while (q.size())
{
auto t = q.front();
q.pop();
for (int i = 0; i < 6; i ++ )
{
int a = t.x + d[i][0], b = t.y + d[i][1], c = t.z + d[i][2];
if (a >= 0 && a < l && b >= 0 && b < m && c >= 0 && c < n && g[a][b][c])
{
g[a][b][c] = 0;
q.push({a, b, c});
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 (g[i][j][k])
{
int t = bfs(i, j, k);
if (t >= T) res += t;
}
printf("%d/n", res);
return 0;
}
原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/281978.html