PAT A1091 Acute Stroke 急性中风【BFS】

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.

 确定急性中风的一个重要因素是中风核心的体积。根据图像分析的结果,在每个MRI切片中识别出核心区域,你的工作就是计算中风核心的体积。

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,o where 1 represents a pixel of strke, 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

每一个测试样例,第一行包含4个正整数:M、N、L和T。M和N是每一个切片的大小(切片像素以m×n矩阵表示,最大分辨率为1286×128。) ;L(≤60)是大脑切片的数量;T是整数阈值(如果连接的核心体积小于T,则这个核心不被计算在内)

之后给出L个切片,每一个切片都是有一个M×N的矩阵来表示,矩阵元素为0或1.1代表中风的1个像素,0表示正常。因为切片的厚度是一个常数,所以我们只需要数1的个数来得到体积。然而,在大脑中有可能会有几个分离的核心区域,只有体积不小于T的才可以算数。两个像素相连,因此如果它们共享同一条边,则它们属于同一个区域。如图1所示,6个红色像素相连变成一个蓝色像素

Output Specification:

For each case, output in a line the total volume of the stroke core.

输出中风核心的体积

算法笔记中总结的题意:给出一个三维数组,数组元素的取值为0或1。与某一个元素相邻的元素为其上下左右前后这6个方向的邻接元素。另外,若干个相邻的"1"称为一个块(不必两两相邻,只要与块中某一个“1”相邻,该1就在块中)。而如果某个块中的"1"的个数不低于T个,那么称这个块为“中风核心区”。现在需要求解所有中风核心区中1的个数之和

#include<cstdio>
#include<queue>
using namespace std;
struct node{
	int x,y,z;
}Node;

int n,m,slice,T;
int pixel[1290][130][61];
bool inq[1290][130][61]={false};
int X[6]={0,0,0,0,1,-1};
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){
	if(x>=n||x<0||y>=m||y<0||z>=slice||z<0) return false;
	if(pixel[x][y][z]==0||inq[x][y][z]==true) return false;
	return 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;
	while(!Q.empty()){
		node top = Q.front();
		Q.pop();
		tot++;
		for(int i=0;i<6;i++){
			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;
				Q.push(Node);
				inq[newX][newY][newZ]=true; 
			} 
		}
	}
	if(tot>=T)  return tot;
	else return 0; 
}

int main(){
	scanf("%d%d%d%d",&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++){
				scanf("%d",&pixel[x][y][z]);
			}
		}
	}
	int ans=0;
	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){
					ans += BFS(x,y,z);
				}
			}
		}
	}
	printf("%d\n",ans);
	return 0;
}

注意点:1.本题使用DFS非常容易在最后两组数据中出现段错误,原因是当三维矩阵中所有元素均为1时,DFS的深度过深,会使系统栈达到上限,从而爆栈

2.输入数据时是按多个二维矩阵的方式读入的,因此3层for循环中第一层遍历矩阵编号,第二三层才是单个矩阵的数据读入

猜你喜欢

转载自blog.csdn.net/qq_38179583/article/details/86708517