PAT-1091 Acute Stroke

1091 Acute Stroke (30)(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

        三维图像连通域问题,其实就是找图的连通分量。开始时直接用并查集超时了,too naive .....尝试过递归的DFS(代码少好写),有两个测试点超时,应该是系统栈爆掉了。故还是用非递归BFS遍历,当然也可以用非递归的DFS,不过感觉麻烦些。

#include<stdio.h>
#include<stdlib.h>
#include<algorithm>
#include<queue>
using namespace std;
struct point{
	int m,n,l;
	point(int _m,int _n,int _l){m=_m;n=_n;l=_l;}
};
bool pic[1286][128][60];
bool visited[1286][128][60]={0};
int t1[6]={1,-1,0,0,0,0};
int t2[6]={0,0,1,-1,0,0};
int t3[6]={0,0,0,0,1,-1};

int M,N,L,T;
int area;
int total=0;
void bfs(point p){
	queue<point> q;
	q.push(p);area++;
	visited[p.m][p.n][p.l]=true;
	while(!q.empty()){
		point t=q.front();
		q.pop();

		for(int i=0;i<6;i++){
			int dx=t.m+t1[i];
			int dy=t.n+t2[i];
			int dz=t.l+t3[i];
			if(dx<M&&dx>=0&&dy<N&&dy>=0&&dz<L&&dz>=0
				&&!visited[dx][dy][dz]&&pic[dx][dy][dz]){
					area++;
					visited[dx][dy][dz]=true;
					q.push(point(dx,dy,dz));	
			}
		}
	}	
}
int main(){

	scanf("%d %d %d %d",&M,&N,&L,&T);

	int tmp,num=0;
	for(int i=0;i<L;i++){
		for(int j=0;j<M;j++){
			for(int k=0;k<N;k++){
				scanf("%d",&tmp);
				if(tmp==1) pic[j][k][i]=1;
				else pic[j][k][i]=0;
			}
		}
	}
	for(int i=0;i<L;i++){
		for(int j=0;j<M;j++){
			for(int k=0;k<N;k++){
				if(!visited[j][k][i]&&pic[j][k][i]){
					area=0;
					bfs(point(j,k,i));
					if(area>=T)
						total+=area;
				}
			}
		}
	}
	printf("%d",total);

	return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值