PAT A 1091. Acute Stroke (30)

本文介绍了一种通过MRI图像分析来计算急性脑卒中核心体积的方法。利用深度优先搜索算法统计连通区域内的像素数量,当连通区域大小超过预设阈值时计入总体积。适用于医学图像处理及脑卒中研究。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目

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 by 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 by 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

 

题目的表达真是够坑的……

给出的01数据,每m*n个是一个平面,依次堆叠(z轴方向变化一个单位)。

需要找的是符合要求的1的数量:

即与该1相连的1(包括本身)大于等于T。

相连指上下左右前后。然后,是需要延伸的,即相连某格是1,那么他的相连是1的格也要计算进去……

实际就是算所有总成员数大于T的所有连通域的总成员数。

 

dfs,统计连通域内成员数,大于T时累加即可。

 

代码:

#include <iostream>
#include <vector>
using namespace std;

struct pos	//保存位置
{
	int i,j,k;
	pos(int i1=0,int i2=0,int i3=0):i(i1),j(i2),k(i3){}
};

int main()
{
	int m,n,l,t;
	cin>>m>>n>>l>>t;

	vector<vector<vector<int>>> data(m,vector<vector<int>>(n,vector<int>(l,0)));	//输入的扫描信息
	for(int k=0;k<l;k++)	//输入
		for(int i=0;i<m;i++)
			for(int j=0;j<n;j++)
				scanf("%d",&data[i][j][k]);

	vector<vector<vector<int>>> test(m,vector<vector<int>>(n,vector<int>(l,0)));	//相应单元是否已经探测标记(仅标记值是1的格)
	vector<pos> stack;	//dfs用栈
	pos pos1;	//临时位置
	int count=0,connect;	//总的坏块数,连接在一起的坏单元数
	for(int i=0;i<m;i++)	//扫描
		for(int j=0;j<n;j++)
			for(int k=0;k<l;k++)
			{
				if(data[i][j][k]==1&&test[i][j][k]==0)	//找到新的单元时dfs
				{
					stack.clear();
					stack.push_back(pos(i,j,k));	//压入新的点
					test[i][j][k]=1;
					connect=1;
					while(!stack.empty())	//dfs
					{
						pos1=stack.back();
						stack.pop_back();
						if(pos1.i>0&&data[pos1.i-1][pos1.j][pos1.k]==1&&test[pos1.i-1][pos1.j][pos1.k]==0)
						{
							stack.push_back(pos(pos1.i-1,pos1.j,pos1.k));
							test[pos1.i-1][pos1.j][pos1.k]=1;
							connect++;
						}
						if(pos1.i<m-1&&data[pos1.i+1][pos1.j][pos1.k]==1&&test[pos1.i+1][pos1.j][pos1.k]==0)
						{
							stack.push_back(pos(pos1.i+1,pos1.j,pos1.k));
							test[pos1.i+1][pos1.j][pos1.k]=1;
							connect++;
						}
						if(pos1.j>0&&data[pos1.i][pos1.j-1][pos1.k]==1&&test[pos1.i][pos1.j-1][pos1.k]==0)
						{
							stack.push_back(pos(pos1.i,pos1.j-1,pos1.k));
							test[pos1.i][pos1.j-1][pos1.k]=1;
							connect++;
						}
						if(pos1.j<n-1&&data[pos1.i][pos1.j+1][pos1.k]==1&&test[pos1.i][pos1.j+1][pos1.k]==0)
						{
							stack.push_back(pos(pos1.i,pos1.j+1,pos1.k));
							test[pos1.i][pos1.j+1][pos1.k]=1;
							connect++;
						}
						if(pos1.k>0&&data[pos1.i][pos1.j][pos1.k-1]==1&&test[pos1.i][pos1.j][pos1.k-1]==0)
						{
							stack.push_back(pos(pos1.i,pos1.j,pos1.k-1));
							test[pos1.i][pos1.j][pos1.k-1]=1;
							connect++;
						}
						if(pos1.k<l-1&&data[pos1.i][pos1.j][pos1.k+1]==1&&test[pos1.i][pos1.j][pos1.k+1]==0)
						{
							stack.push_back(pos(pos1.i,pos1.j,pos1.k+1));
							test[pos1.i][pos1.j][pos1.k+1]=1;
							connect++;
						}
					}
					if(connect>=t)
						count+=connect;
				}
			}
	cout<<count;

	return 0;
}


 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值