One spaceman places several food cubes in space such that there may be holes between cubes. Others, given the coordinates of the food cubes, should find the number of holes. A hole is a continuous empty space surrounded by food cubes in all six directions. You are to write a program to read the coordinates of each food cube and compute the number of holes.
Input
The first line of the input contains a single integer t (1 <= t <= 20) which is the number of test cases in the input. Each test case starts with an integer M, the number of food cubes. Each line i (1 <= i <= M) of the M following lines contains integers xi, yi and zi, all between 1 and 100 inclusive, indicating the three coordinates of a food cube in the 3D space.
Output
For each test case, there is one line containing the number of holes.
#include<iostream>
#include<string.h>
using namespace std;
bool coord[102][102][102];
int max_x, min_x, max_y, min_y, max_z, min_z;
bool inline isSide(int x, int y, int z)
{
return (x==max_x || x==min_x
||y==max_y || y==min_y
||z==max_z || z==min_z);
}
bool BFS(int x, int y, int z) // 广度优先搜索 当一个点周围的六个点都为1时这个点时洞
{
if(coord[x][y][z]==1){
return true;
}
if(isSide(x,y,z)){
return false;
} // 遇到边界时返回false 已经无法满足六面为1的条件
bool re = 1;
coord[x][y][z]=1; // 将访问过的节点标记为1 否则无限递归 并让相邻顶点有满足条件的可能
re &= BFS(x-1, y, z);
re &= BFS(x+1, y, z);
re &= BFS(x, y-1, z);
re &= BFS(x, y+1, z);
re &= BFS(x, y, z-1);
re &= BFS(x, y, z+1);
return re;
}
int main()
{
int t;
cin >> t;
while(t--){
int n, x , y , z;
memset(coord,0,sizeof(coord));
max_x = max_y = max_z = 0;
min_x = min_y = min_z = 110;
cin >> n;
for(int i=0; i<n; i++){
cin >> x >> y >> z;
coord[x][y][z] = 1;
max_x = max(max_x, x);min_x = min(min_x, x);
max_y = max(max_y, y);min_y = min(min_y, y);
max_z = max(max_z, z);min_z = min(min_z, z);
}
int holes = 0;
for(int i=min_x; i<=max_x; i++)
for(int j=min_y; j<=max_y; j++)
for(int k=min_z; k<=max_z; k++){
if(coord[i][j][k]==0 && BFS(i,j,k))
holes++;
}
cout << holes << endl;
}
return 0;
}