PAT甲级1091 Acute Stroke【三维bfs】

时间:2022-12-10 11:11:55

题目https://pintia.cn/problem-sets/994805342720868352/problems/994805375457411072

题意:

求三维的连通块

思路:

简单bfs

 #include<cstdio>
#include<cstdlib>
#include<map>
#include<set>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<vector>
#include<cmath>
#include<stack>
#include<queue> #define inf 0x7fffffff
using namespace std;
typedef long long LL;
typedef pair<string, string> pr; int m, n, l, t;
struct node{
int x, y, z;
node(){
}
node(int _x, int _y, int _z){
x = _x;
y = _y;
z = _z;
}
}; int dx[] = {, , , , , -};
int dy[] = {, -, , , , };
int dz[] = {, , , -, , };
bool space[][][];
bool vis[][][];
int tot = ; bool check(int i, int j, int k)
{
if(i < || i >= m || j < || j >= n || k < || k >= l)return false;
else return true;
} void bfs(int x, int y, int z)
{
queue<node>que;
que.push(node(x, y, z));
vis[x][y][z] = true;
int cnt = ;
while(!que.empty()){
node now = que.front();que.pop();
for(int i = ; i < ; i++){
int a = now.x + dx[i], b = now.y + dy[i], c = now.z + dz[i];
if(check(a, b, c) && !vis[a][b][c] && space[a][b][c]){
que.push(node(a, b, c));
vis[a][b][c] = true;
cnt++;
}
}
}
if(cnt >= t){
tot += cnt;
}
} int main()
{
scanf("%d%d%d%d", &m, &n, &l, &t);
for(int k = ; k < l; k++){
for(int i = ; i < m; i++){
for(int j = ; j < n; j++){
scanf("%d", &space[i][j][k]);
}
}
} for(int k = ; k < l; k++){
for(int i = ; i < m; i++){
for(int j = ; j < n; j++){
if(!vis[i][j][k] && space[i][j][k])
bfs(i, j, k);
}
}
}
printf("%d\n", tot);
return ;
}