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
题意: 找中风点的数量。输入M(某片的行数),N(某片的列数),L(总片数),T(中风点的最少像素点)
知识点:三维bfs,求至少数量为T的连通分量的总面积
易错点:
- h,r,c分别代表 L,M,N;
- 所求为总面积,非连通分量个数
#include<iostream>
#include<cstring>
#include<queue>
using namespace std;
int maze[65][1300][130];
int vis[65][1300][130]={0};
int dir[6][3]={{1,0,0},{-1,0,0},{0,1,0},{0,-1,0},{0,0,1},{0,0,-1}};
int total;
int N,M,L,T;
struct node{
int h,r,c;
};
void bfs(int h,int r,int c){
vis[h][r][c]=1;
queue<node> q;
node n={h,r,c};
q.push(n);
int cnt=0;
while(!q.empty()){
node tmp=q.front(),t;
q.pop();
cnt++; //only
int hh,rr,cc;
for(int i=0;i<6;i++){
hh=tmp.h+dir[i][0];
rr=tmp.r+dir[i][1];
cc=tmp.c+dir[i][2];
if(hh>=0&&hh<L&&rr>=0&&rr<M&&cc>=0&&cc<N&&maze[hh][rr][cc]&&!vis[hh][rr][cc]){
vis[hh][rr][cc]=1;
t={hh,rr,cc};
q.push(t);
}
}
}
if(cnt>=T) total+=cnt;
}
int main(){
cin>>M>>N>>L>>T;
for(int h=0;h<L;h++)
for(int r=0;r<M;r++)
for(int c=0;c<N;c++)
scanf("%d",&maze[h][r][c]);
for(int h=0;h<L;h++)
for(int r=0;r<M;r++)
for(int c=0;c<N;c++)
if(maze[h][r][c]&&!vis[h][r][c])
bfs(h,r,c);
cout<<total<<endl;
return 0;
}