PAT 甲级 1091 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.

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.

在这里插入图片描述
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

解题思路:本题是对BFS模板的简单修改,唯一的不同是变成了三维数组,所以存储三维数组的时候需要注意。

代码:

#include<cstdio> 
#include<queue>
using namespace std;
struct node
{
	int x,y,z;
}Node;
int matrix[1300][130][80];   //存初始序列 
int inq[1300][130][80] = {0};  //入队后置为1 
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};
int row,col,slice,T;
bool judge(int x,int y,int z)   //判断是否能访问 
{
	if(x>=row||x < 0||y>=col|| y < 0||z>=slice||z < 0)	
		return false;
	if(matrix[x][y][z] == 0||inq[x][y][z] == 1)
		return false;
	return true;
}
int BFS(int x,int y,int z)
{
	queue<node> q;
	Node.x = x;
	Node.y = y;
	Node.z = z;
	q.push(Node);
	inq[x][y][z] = 1;
	int sum = 0;
	while(!q.empty())
	{
		node top = q.front();
		q.pop();
		sum++;
		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] = 1;
			}
		}
	}
	if(sum>=T)
		return sum;
	else
		return 0;
}
int main(void)
{
	int total = 0;
	scanf("%d %d %d %d",&row,&col,&slice,&T);
	for(int i = 0;i < slice;i++){
		for(int j = 0;j < row;j++){
			for(int m = 0;m < col;m++){
			scanf("%d",&matrix[j][m][i]);
			}
		}	
	}
	for(int i = 0;i < slice;i++){
		for(int j = 0;j < row;j++){
			for(int m = 0;m < col;m++){
				if(matrix[j][m][i] == 1&&inq[j][m][i] == 0) 
					total+=BFS(j,m,i);
				}
			}
		}	
	printf("%d",total);
}
发布了24 篇原创文章 · 获赞 1 · 访问量 500

猜你喜欢

转载自blog.csdn.net/lovingcgq/article/details/104441843