One important factor to identify acute stroke (急性脑卒中) is the volume of the stroke core. Given the results of image analysis in which the core regions are identified in each MRI slice, your job is to calculate the volume of the stroke core.
Input Specification:
Each input file contains one test case. For each case, the first line contains 4 positive integers: M, N, L and T, where M and N are the sizes of each slice (i.e. pixels of a slice are in an M×N matrix, and the maximum resolution is 1286 by 128); L (≤60) is the number of slices of a brain; and T is the integer threshold (i.e. if the volume of a connected core is less than T, then that core must not be counted).
Then L slices are given. Each slice is represented by an M×N matrix of 0's and 1's, where 1 represents a pixel of stroke, and 0 means normal. Since the thickness of a slice is a constant, we only have to count the number of 1's to obtain the volume. However, there might be several separated core regions in a brain, and only those with their volumes no less than T are counted. Two pixels are connected and hence belong to the same region if they share a common side, as shown by Figure 1 where all the 6 red pixels are connected to the blue one.
【插图】
Figure 1
Output Specification:
For each case, output in a line the total volume of the stroke core.
Sample Input:
3 4 5 2
1 1 1 1
1 1 1 1
1 1 1 1
0 0 1 1
0 0 1 1
0 0 1 1
1 0 1 1
0 1 0 0
0 0 0 0
1 0 1 1
0 0 0 0
0 0 0 0
0 0 0 1
0 0 0 1
1 0 0 0
Sample Output:
26
解题思路:相当于二维图中考察节点数超过k的所有连通分量的总结点数,如果是在二维图中,可以用BFS和DFS做,本题是三维图,也同样是BFS和DFS的两种思路,不过本题用DFS的话会超内存
另外vector定义三维数组
vector<vector<vector<int>>>v(m, vector<vector<int>>(n, vector<int>(k, x)));
//定义m行n列k高的三维数组,并赋值为x
BFS
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
typedef struct{
int x, y, z;
}coord;
int m, n, L, t, ans = 0;
int main(){
scanf("%d %d %d %d", &m, &n, &L, &t);
vector<vector<vector<int>>>matrix(m, vector<vector<int>>(n, vector<int>(L)));
vector<vector<vector<bool>>>visit(m, vector<vector<bool>>(n, vector<bool>(L, false)));
for(int k = 0; k < L; ++ k)
for(int i = 0; i < m; ++ i)
for(int j = 0; j < n; ++ j)
scanf("%d", &matrix[i][j][k]);
for(int k = 0; k < L; ++ k)
for(int i = 0; i < m; ++ i)
for(int j = 0; j < n; ++ j){
if(matrix[i][j][k] == 1 && visit[i][j][k] == false){
queue<coord>q;
q.push({i, j, k});
visit[i][j][k] = true;
int tempans = 0;
while(! q.empty()){
coord temp = q.front();
++ tempans;
q.pop();
if(temp.x > 0 && matrix[temp.x-1][temp.y][temp.z] == 1 &&visit[temp.x-1][temp.y][temp.z] == false){
q.push({temp.x-1,temp.y,temp.z});
visit[temp.x-1][temp.y][temp.z] = true;
}
if(temp.x < m-1 && matrix[temp.x+1][temp.y][temp.z] == 1 &&visit[temp.x+1][temp.y][temp.z] == false){
q.push({temp.x+1,temp.y,temp.z});
visit[temp.x+1][temp.y][temp.z] = true;
}
if(temp.y > 0 && matrix[temp.x][temp.y-1][temp.z] == 1 &&visit[temp.x][temp.y-1][temp.z] == false){
q.push({temp.x,temp.y-1,temp.z});
visit[temp.x][temp.y-1][temp.z] = true;
}
if(temp.y < n-1 && matrix[temp.x][temp.y+1][temp.z] == 1 &&visit[temp.x][temp.y+1][temp.z] == false){
q.push({temp.x,temp.y+1,temp.z});
visit[temp.x][temp.y+1][temp.z] = true;
}
if(temp.z > 0 && matrix[temp.x][temp.y][temp.z-1] == 1 && visit[temp.x][temp.y][temp.z-1] == false){
q.push({temp.x,temp.y,temp.z-1});
visit[temp.x][temp.y][temp.z-1] = true;
}
if(temp.z < L-1 && matrix[temp.x][temp.y][temp.z+1] == 1 && visit[temp.x][temp.y][temp.z+1] == false){
q.push({temp.x,temp.y,temp.z+1});
visit[temp.x][temp.y][temp.z+1] = true;
}
}
ans += (tempans >= t ? tempans : 0);
}
}
printf("%d", ans);
}