1091. Acute Stroke (30)

题目:

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

注意:
1、题目并不难,求解连通块,只不过从普通的二维变成了三维,注意6个相邻点就好了。
2、由于数据量很大,使用dfs递归是不可取的,过大的递归深度会导致段错误(case 4,5)。
3、还是老老实实用bfs吧,虽然代码会冗长很多。
4、如下的代码中也附上了使用dfs来求解的方案,代码简介很多,但是最后两个case段错误(为了追求简洁也是蛮拼的)。

代码:
//1091  accept
#include<iostream>
#include<vector>
using namespace std;

struct node
{
	int x,y,z;
	node(int a,int b,int c):x(a),y(b),z(c){}
};
int mat[1300][130][60];
int m,n,l,t;

int volume(int x,int y,int z)
{//calculate the volume of the stroke core containing this stroke
	if(mat[x][y][z]!=1)
		return 0;
	mat[x][y][z]=-1;
	int v=1;//volume
	vector<node>stroke;
	node temp(x,y,z);
	stroke.push_back(temp);
	while(!stroke.empty())
	{
		vector<node>next;
		for(int i=0;i<stroke.size();++i)
		{//add the connected stroke to the next loop
			if(stroke[i].x+1<m&&mat[stroke[i].x+1][stroke[i].y][stroke[i].z]==1)
			{//x+1
				mat[stroke[i].x+1][stroke[i].y][stroke[i].z]=-1;//this node has been counted
				node t(stroke[i].x+1,stroke[i].y,stroke[i].z);
				next.push_back(t);
			}
			if(stroke[i].x-1>=0&&mat[stroke[i].x-1][stroke[i].y][stroke[i].z]==1)
			{//x-1
				mat[stroke[i].x-1][stroke[i].y][stroke[i].z]=-1;//this node has been counted
				node t(stroke[i].x-1,stroke[i].y,stroke[i].z);
				next.push_back(t);
			}
			if(stroke[i].y+1<n&&mat[stroke[i].x][stroke[i].y+1][stroke[i].z]==1)
			{//y+1
				mat[stroke[i].x][stroke[i].y+1][stroke[i].z]=-1;//this node has been counted
				node t(stroke[i].x,stroke[i].y+1,stroke[i].z);
				next.push_back(t);
			}
			if(stroke[i].y-1>=0&&mat[stroke[i].x][stroke[i].y-1][stroke[i].z]==1)
			{//y-1
				mat[stroke[i].x][stroke[i].y-1][stroke[i].z]=-1;//this node has been counted
				node t(stroke[i].x,stroke[i].y-1,stroke[i].z);
				next.push_back(t);
			}
			if(stroke[i].z+1<l&&mat[stroke[i].x][stroke[i].y][stroke[i].z+1]==1)
			{//z+1
				mat[stroke[i].x][stroke[i].y][stroke[i].z+1]=-1;//this node has been counted
				node t(stroke[i].x,stroke[i].y,stroke[i].z+1);
				next.push_back(t);
			}
			if(stroke[i].z-1>=0&&mat[stroke[i].x][stroke[i].y][stroke[i].z-1]==1)
			{//z-1
				mat[stroke[i].x][stroke[i].y][stroke[i].z-1]=-1;//this node has been counted
				node t(stroke[i].x,stroke[i].y,stroke[i].z-1);
				next.push_back(t);
			}
		}
		v+=next.size();
		stroke=next;
	}
	return v;
}

int main()
{
	scanf("%d%d%d%d",&m,&n,&l,&t);
	for(int i=0;i<l;++i)
		for(int j=0;j<m;++j)
			for(int k=0;k<n;++k)
				scanf("%d",&mat[j][k][i]);
	int count=0;
	for(int i=0;i<l;++i)
		for(int j=0;j<m;++j)
			for(int k=0;k<n;++k)
			{//calculate the sum of each volume
				int v=volume(j,k,i);
				if(v>=t)
					count+=v;
			}
	printf("%d",count);
	return 0;
}

//1091 case 4,5 is not passed, recursion is too deep
#include<iostream>
using namespace std;

int mat[1300][130][60];
int m,n,l,t;

int volume(int x,int y,int z)
{
	if(x<0||x>=m||y<0||y>=n||z<0||z>=l||mat[x][y][z]!=1)
		return 0;
	mat[x][y][z]=-1;//this node has been counted
	return 1+volume(x+1,y,z)+volume(x-1,y,z)+volume(x,y+1,z)+
		volume(x,y-1,z)+volume(x,y,z+1)+volume(x,y,z-1);
}

int main()
{
	scanf("%d%d%d%d",&m,&n,&l,&t);
	for(int i=0;i<l;++i)
		for(int j=0;j<m;++j)
			for(int k=0;k<n;++k)
				scanf("%d",&mat[j][k][i]);
	int count=0;
	for(int i=0;i<l;++i)
		for(int j=0;j<m;++j)
			for(int k=0;k<n;++k)
			{
				int v=volume(j,k,i);
				if(v>=t)
					count+=v;
			}
	printf("%d",count);
	return 0;
}


你可能感兴趣的:(考试,pat,浙江大学)