https://pintia.cn/problem-sets/1218774283169423360/problems/1218774532776648716
#include<iostream> #include<string> #include <cstdlib> #include <algorithm> #include<cmath> #include<cstring> #include<cstdio> #include<vector> #include<queue> #include<map> #include<set> #include<bitset> #include <iomanip> #include <unordered_map> // #pragma comment(linker, "/STACK:1024000000,1024000000") // #define pi acos(-1) // #include<bits/stdc++.h> using namespace std; typedef long long ll; #define INF 0x7f7f7f7f //2139062143 #define INF1 0x3f3f3f3f //1061109567 #define INF2 2147483647 #define llINF 9223372036854775807 #define pi 3.141592653589793//23846264338327950254 #define pb push_back #define ll long long #define debug cout << "debug "; #define STDIN freopen("in.txt","r",stdin); freopen("out.txt","w",stdout); #define Quick ios::sync_with_stdio(false);cin.tie(NULL); #define scai(x) scanf("%d", &x) #define sca2i(x, y) scanf("%d %d", &x, &y) #define scaf(x) scanf("%lf", &x) #define sca2f(x, y) scanf("%lf %lf", &x, &y) #define For(m,n) for (int i = m; i < n; i++) #define PII pair<int,int> #define PLL pair<long, long> inline int read(){ int s=0,w=1; char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-')w=-1;ch=getchar();} while(ch>='0'&&ch<='9') s=s*10+ch-'0',ch=getchar(); return s*w; } #define local #ifdef local #endif #define MAX 10233 #define LCH(i) ((i) << 1) #define RCH(i) ((i) << 1 | 1) int a[2300][230][165]; int m, n, l ,t; int d[6][3] = { {1,0,0},{0,1,0},{0,0,1},{-1,0,0},{0,-1,0},{0,0,-1} }; int dfs(int x, int y, int z) { a[x][y][z] = 0; int sum = 1; for (int ii = 0; ii < 6; ii++) { int xx = x + d[ii][0]; int yy = y+ d[ii][1]; int zz = z + d[ii][2]; if (xx >= 0 && xx < m && yy >=0 && yy < n && zz >= 0 && zz <l && a[xx][yy][zz] == 1) { sum += dfs(xx, yy, zz); } } return sum; } struct node { int x, y, z; }; int bfs(int x, int y, int z) { queue<node> q; q.push({x, y, z}); int sum = 1; a[x][y][z] = 0; while (q.size()) { //debug node t = q.front();q.pop(); int x = t.x; int y = t.y; int z = t.z; for (int ii = 0; ii < 6; ii++) { int xx = x + d[ii][0]; int yy = y+ d[ii][1]; int zz = z + d[ii][2]; if (xx >= 0 && xx < m && yy >=0 && yy < n && zz >= 0 && zz <l && a[xx][yy][zz] == 1) { q.push({xx, yy, zz}); a[xx][yy][zz] = 0; sum += 1; } // printf("x = %d y = %d z = %d ", d[ii][0], d[ii][1], d[ii][2]); } } return sum; } int main() { scanf("%d %d %d %d", &m, &n, &l, &t); for (int k = 0; k < l; k++) { for (int i = 0; i < m; i++) for (int j = 0; j < n; j++) { scanf("%d", &a[i][j][k]); } } int sum = 0; for (int k = 0; k < l; k++) { for (int i = 0; i < m; i++) for (int j = 0; j < n; j++) { if(a[i][j][k] == 1) { int temp = bfs(i, j, k); if(temp >= t) { sum += temp; } } } } //cout << sum << endl; printf("%d ", sum); }