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
题意:
三维数组每个点取值为0或1,给出一个阈值,一个块中取值为1的个数超过阈值则为一个核心区,求所有核心区中1的个数之和
分析:
读入时先循环层数,如样例为5层,每层为3*4,然后对每个点依次BFS
代码:
#include
#include
#include
using namespace std;
struct node{
int x, y, z;//位置(x,y,z)
}Node;
int N, M, slice, T;//矩阵N*M,共有slice层,T为卒中核心区中1的个数的下限
int pixel[1290][130][61];//三维01矩阵
bool inq[1290][130][61] = {false};//记录位置{x,y,z}是否已入过队
int X[6] = {0, 0, 0, 0, 1, -1};//增量矩阵//6个方向分别为6列坐标
int Y[6] = {0, 0, 1, -1, 0, 0};
int Z[6] = {1, -1, 0, 0, 0, 0};
bool judge(int x, int y, int z){//判断坐标(x,y,z)是否需要访问
if(x>=N||x<0||y>=M||y<0||z>=slice||z<0) return false;//越界返回false
if(pixel[x][y][z]==0||inq[x][y][z]==true) return false;//当前位置为0或已入过队
return true;
}
//BFS函数访问位置(x,y,z)所在的块,将该块中所有"1"的inq都设置为true
int BFS(int x, int y, int z){
int tot = 0;//计数当前块中1的个数
queue<node> Q;
Node.x = x, Node.y = y, Node.z = z;
Q.push(Node);
inq[x][y][z] = true;//设置(x,y,z)已入过队
while(!Q.empty()){
node top = Q.front();//取出队首元素
Q.pop();//队首元素出队
tot++;//当前块中1的个数加1
for(int i=0;i<6;i++){//循环6次,得到6个增量方向
int newX = top.x + X[i];
int newY = top.y + Y[i];
int newZ = top.z + Z[i];
if(judge(newX, newY, newZ)){//新位置需要访问
Node.x = newX, Node.y = newY, Node.z = newZ;//设置Node坐标
Q.push(Node);//将节点Node入队
inq[newX][newY][newZ] = true;//设置(newX,newY,newZ)已入过队
}
}
}
if(tot>=T) return tot;//如果超过阈值则返回
else return 0;
}
int main()
{
cin>>N>>M>>slice>>T;
for(int z=0;z<slice;z++)//注意先枚举切片层号
for(int x=0;x<N;x++)
for(int y=0;y<M;y++)
cin>>pixel[x][y][z];
int ans = 0;//记录卒中核心区中1的个数总和
for(int z=0;z<slice;z++)
for(int x=0;x<N;x++)
for(int y=0;y<M;y++)
if(pixel[x][y][z]==1&&inq[x][y][z]==false)//当前位置为1且未被访问则BFS当前块
ans += BFS(x,y,z);
cout<<ans<<endl;
return 0;
}