简单BFS,注意数组越界。
#include <bits/stdc++.h>
using namespace std;
int table[65][1290][130], n, x, y;
int minvol = 0;
int ans = 0;
struct point{
int xx;
int yy;
int zz;
point(int a, int b, int c): xx(a), yy(b), zz(c){
}
};
bool isok(int xx, int yy, int zz){
if(xx < 0 || xx >= n || y < 0 || yy >= x || zz < 0 || zz >= y) return false;
return true;
}
int main(){
scanf("%d %d %d %d", &x, &y, &n, &minvol);
for(int i = 0; i < n; ++i)
for(int j = 0; j < x; ++j)
for(int k = 0; k < y; ++k)
scanf("%d", &table[i][j][k]);
queue<point> q;
for(int i = 0; i < n; ++i)
for(int j = 0; j < x; ++j)
for(int k = 0; k < y; ++k){
if(1 == table[i][j][k]){
int tempans = 0;
q.push(point(i, j, k));
while(!q.empty()){
point temppoint = q.front();
q.pop();
if(!isok(temppoint.xx, temppoint.yy, temppoint.zz)) continue;
if(1 == table[temppoint.xx][temppoint.yy][temppoint.zz]){
table[temppoint.xx][temppoint.yy][temppoint.zz] = 0;
tempans++;
q.push(point(temppoint.xx + 1, temppoint.yy, temppoint.zz));
q.push(point(temppoint.xx - 1, temppoint.yy, temppoint.zz));
q.push(point(temppoint.xx, temppoint.yy + 1, temppoint.zz));
q.push(point(temppoint.xx, temppoint.yy - 1, temppoint.zz));
q.push(point(temppoint.xx, temppoint.yy, temppoint.zz + 1));
q.push(point(temppoint.xx, temppoint.yy, temppoint.zz - 1));
}
}
if(tempans >= minvol) ans += tempans;
}
}
printf("%d\n", ans);
}