PTA_PAT甲级_1091 Acute Stroke (30分)

本文介绍了一种计算急性脑卒中患者MRI图像中卒中核心区域体积的方法,通过三维数组表示图像,利用BFS算法遍历并计算超过阈值的卒中核心区域的像素数量。

摘要生成于 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×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.

figstroke.jpg

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<iostream>
#include<cstdio>
#include<queue>
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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

_bound

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值