1091 Acute Stroke

题目来源:PAT (Advanced Level) Practice

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

words:

acute 严重的        volume 体积        regions 地区,地域        slice 片,部分        

threshold 门槛,入口

题意:

统计连通分支面积(即1的个数)大于等于t的部分的1的总个数;

思路:

1. 用一个三维数组来存储题目给定的数据,读入数据是按多个二维矩阵的方式读入的;

2. 枚举三维数组中的每一个位置,如果为0,则跳过,如果为1,则继续使用BFS查询与该位置相邻的6个位置,判断他们是否为1(如果为1则继续同样的步骤)。为了防止重复访问,可以设置一个bool 型的vis[][][]来标记该位置是否已经入过队;每一次的枚举返回连续的1的个数(前提是1的个数大于等于t,否则返回0);

3. 输出所有枚举的返回值的和,即符合要求的1的个数;

//PAT ad 1091 Acute Stroke
#include <iostream>
using namespace std;
#include <vector>
#include <queue>
#define M 1290
#define N 130
#define L 65

int a[M][N][L];		//三维数组 
bool vis[M][N][L]={false};

struct xyz 
{
	int x,y,z;
}p;

int m,n,l,t;

int dx[]={0,0,0,0,1,-1};
int dy[]={0,0,1,-1,0,0};
int dz[]={1,-1,0,0,0,0};


int BFS(int i,int j,int k)
{
	queue<xyz> qe;
	qe.push({i,j,k});	//初始结点
	vis[i][j][k]=true;	//标记已入队  
	int c=0;			//记录1的个数	
	while(!qe.empty()) 
	{
		p=qe.front();qe.pop();

		for(i=0;i<6;i++)
		{
			int newX=p.x+dx[i];
			int newY=p.y+dy[i];
			int newZ=p.z+dz[i];
			if(newX>=0&&newX<m&&newY>=0&&newY<n&&newZ>=0&&newZ<l&&a[newX][newY][newZ]!=0&&!vis[newX][newY][newZ])
			{
				qe.push({newX,newY,newZ});	//是1结点入队 
				vis[newX][newY][newZ]=true;	//标记已入队  
			}	
			
		}
		c++; 	//统计1的个数		
	}

	if(c>=t)	return c;
	else	return 0; 	
}
int main()
{
	int i,j,k;
	cin>>m>>n>>l>>t;
	
	
	for(k=0;k<l;k++)		//输入 
	for(i=0;i<m;i++)
	for(j=0;j<n;j++)
		cin>>a[i][j][k];
	
	int c=0;	//统计符合题目要求的1的个数 
	for(k=0;k<l;k++)		//遍历 
	for(i=0;i<m;i++)
	for(j=0;j<n;j++)
		if(!vis[i][j][k]&&a[i][j][k]==1) 	//未访问且为1 
			c+=BFS(i,j,k);
	
	cout<<c<<endl; 
	
	
	return 0;
} 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值