题目分析:
可能是我的理解能力比较差,在读题的时候一直以为所有的切片是可以排列组合的,并不是按照输入顺序就定死的,那么这题就变得十分的复杂啦~~~~~,查看的题解之后发现所有的切片并没有所谓的自由组合的情况,而是类似与一片一片从下往上叠加在一起,形成一个三维的空间,那我们需要做的就是将所有的像素以三维数组的形式存储,然后bfs所有像素为1的点,只要相连的6个方向中有1则继续bfs,每次搜索过的1像素点都清零,避免重复搜索,在bfs的过程中统计相连的1的个数,每次bfs结束时比较sum和阈值的大小,并加入总体积
代码:
1 #include<iostream> 2 #include<algorithm> 3 #include<stdio.h> 4 #include<cmath> 5 #include<set> 6 #include<queue> 7 #include<vector> 8 using namespace std; 9 10 int dir[6][3] = {{1, 0, 0}, {-1, 0, 0}, {0, 1, 0}, {0, -1, 0}, {0, 0, 1}, {0, 0, -1}}; //x轴 y轴 z轴 11 int a[65][130][1300]; 12 int n, m, l, t; 13 int area = 0; 14 struct Node{ 15 int x, y, z; 16 }; 17 18 bool judge(int x, int y, int z){ 19 if(x < 0 || x > l || y < 0 || y > n || z < 0 || z > m) return false; 20 else return true; 21 } 22 23 void bfs(int x, int y, int z){ 24 queue<Node> q; 25 int sum = 1; 26 Node temp; 27 temp.x = x; temp.y = y; temp.z = z; 28 a[x][y][z] = 0; //搜索过的像素清零 29 q.push(temp); 30 while(!q.empty()){ 31 temp = q.front(); 32 q.pop(); 33 for(int i = 0; i < 6; i++){ 34 Node now; 35 now.x = temp.x + dir[i][0]; 36 now.y = temp.y + dir[i][1]; 37 now.z = temp.z + dir[i][2]; 38 if(judge(now.x, now.y, now.z) && a[now.x][now.y][now.z]){ 39 a[now.x][now.y][now.z] = 0; 40 sum++; 41 q.push(now); 42 } 43 } 44 } 45 if(sum >= t) area += sum; 46 } 47 48 int main(){ 49 scanf("%d%d%d%d", &n, &m, &l, &t); 50 for(int i = 1; i <= l; i++){ 51 for(int j = 1; j <= n; j++){ 52 for(int k = 1; k <= m; k++){ 53 scanf("%d", &a[i][j][k]); 54 } 55 } 56 } 57 for(int i = 1; i <= l; i++){ 58 for(int j = 1; j <= n; j++){ 59 for(int k = 1; k <= m; k++){ 60 if(a[i][j][k]) bfs(i, j, k); 61 } 62 } 63 } 64 printf("%d ", area); 65 return 0; 66 }
回顾:
1 #include<iostream> 2 #include<stdio.h> 3 #include<queue> 4 #include<string.h> 5 #include<algorithm> 6 using namespace std; 7 8 int mat[65][1300][130]; 9 int dir[6][3] = {{1, 0, 0}, {-1, 0, 0}, {0, 0, 1}, {0, 0, -1}, {0, 1, 0}, {0, -1, 0}}; 10 int m, n, l, t; 11 int num; 12 struct Node{ 13 int x, y, z; 14 }; 15 16 void bfs(int x, int y, int z){ 17 queue<Node> q; 18 Node temp; 19 temp.x = x; temp.y = y; temp.z = z; 20 mat[x][y][z] = 0; 21 q.push(temp); 22 int sum = 1; 23 while(!q.empty()){ 24 temp = q.front(); 25 q.pop(); 26 for(int i = 0; i < 6; i++){ 27 int fx = dir[i][0] + temp.x; 28 int fy = dir[i][1] + temp.y; 29 int fz = dir[i][2] + temp.z; 30 if(fx >= 1 && fx <= l && fy >= 1 && fy <= m && fz >= 1 && fz <= n){ 31 if(mat[fx][fy][fz] == 1){ 32 mat[fx][fy][fz] = 0; 33 sum++; 34 Node next; 35 next.x = fx; next.y = fy; next.z = fz; 36 q.push(next); 37 } 38 } 39 } 40 } 41 if(sum >= t) num += sum; 42 } 43 44 int main(){ 45 scanf("%d%d%d%d", &m, &n, &l, &t); 46 for(int i = 1; i <= l; i++){ 47 for(int j = 1; j <= m; j++){ 48 for(int k = 1; k <= n; k++){ 49 scanf("%d", &mat[i][j][k]); 50 } 51 } 52 } 53 for(int i = 1; i <= l; i++){ 54 for(int j = 1; j <= m; j++){ 55 for(int k = 1; k <= n; k++){ 56 if(mat[i][j][k] == 1){ 57 bfs(i, j, k); 58 } 59 } 60 } 61 } 62 printf("%d ", num); 63 return 0; 64 }