PAT Advanced1091 Acute Stroke(BFS)

版权声明:个人学习笔记记录 https://blog.csdn.net/Ratina/article/details/86542214

链接:PAT Advanced1091

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.

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。与某一个元素相邻的元素为其上、下、左、右、前、后这6个方向的邻接元素。另外,若干个相邻的“1”称为一个“块”(不必两两相邻,只要与块中某一个“1”相邻,该“1”就在块中)。而如果某个块中的“1”的个数不低于T个,那么称这个块为“卒中核心区”。现在需要求解所有卒中核心区中的1的个数之和。



思路:
构建三维数组: 利用bool类型数组 flag[M][N][L],true/false表示1/0,全部初始化为false,并且读入时下标0均不存储数据,这样就可以避免另外处理越界情况。(就像是在边界围上围墙)

BFS遍历: 对flag为true的结点进行BFS遍历,凡是访问过的结点均置为false,避免重复访问。


以下代码:

#include<cstdio>
#include<queue>
#include<algorithm>
using namespace std;
bool flag[1290][130][70]={false};  //初始化为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};
struct node
{
	int x,y,z;
};
int BFS(int x,int y,int z)
{
	node t1={x,y,z};
	int cnt=0;
	queue<node> q;
	flag[x][y][z]=false;      //别忘了起始结点也要置为false
	q.push(t1);
	while(!q.empty())
	{
		t1=q.front();
		q.pop();
		for(int i=0;i<6;i++)  //遍历周围6个相邻结点
		{
			if(flag[t1.x+X[i]][t1.y+Y[i]][t1.z+Z[i]])
			{
				node t2={t1.x+X[i],t1.y+Y[i],t1.z+Z[i]};
				q.push(t2);
				flag[t1.x+X[i]][t1.y+Y[i]][t1.z+Z[i]]=false;
			}
		}
		cnt++;
	}
	return cnt;
}
int main()
{
	int M,N,L,T;
	int x,y,z,t,ans=0;
	scanf("%d %d %d %d",&M,&N,&L,&T);
	for(z=1;z<=L;z++)           
		for(x=1;x<=M;x++)      //下标均从1开始
			for(y=1;y<=N;y++)
				scanf("%d",&flag[x][y][z]);
	for(z=1;z<=L;z++)
		for(x=1;x<=M;x++)
			for(y=1;y<=N;y++)
			{
				if(flag[x][y][z]&&(t=BFS(x,y,z))>=T)  //要满足≥T
					ans+=t;
			}
	printf("%d",ans);
	return 0;
}

猜你喜欢

转载自blog.csdn.net/Ratina/article/details/86542214