#pragma warning(disable:4996) #include <stdio.h> #include <stdlib.h> #include <queue> using namespace std; struct node{ int x, y, z; }p; int m, n, l, t, volume = 0; int brain[1300][130][61], bbrain[1300][130][61]; //右,左,上,下,前,后 int X[] = { 0, 0, 0, 0, 1, -1 }; int Y[] = { 1, -1, 0, 0, 0, 0 }; int Z[] = { 0, 0, 1, -1, 0, 0 }; bool judge(int x, int y, int z){ if (x >= m || x < 0 || y >= n || y < 0 || z >= l || z < 0) return false; if (!brain[x][y][z] || bbrain[x][y][z]) return false; return true; } int BFS(int x, int y, int z){ int cnt = 0; queue<node> qn; p.x = x; p.y = y; p.z = z; qn.push(p); bbrain[x][y][z] = 1; while (!qn.empty()){ node top = qn.front(); qn.pop(); cnt++; for (int i = 0; i < 6; i++){ int nx = top.x + X[i], ny = top.y + Y[i], nz = top.z + Z[i]; if (judge(nx, ny, nz)){ p.x = nx; p.y = ny; p.z = nz; qn.push(p); bbrain[nx][ny][nz] = 1; } } } if (cnt >= t) return cnt; else return 0; } 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", &brain[i][j][k]); for (int i = 0; i < m; i++) for (int j = 0; j < n; j++) for (int k = 0; k < l; k++) if (brain[i][j][k] && bbrain[i][j][k] == 0) volume += BFS(i, j, k); printf("%d ", volume); system("pause"); return 0; }
参考:https://blog.csdn.net/ztmajor/article/details/81393476