PAT甲级1091 Acute Stroke (30 分)

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: MNL 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

      题目大意:

         给出长度为m, 宽度为n 且片数为l的大脑诊断片,并在同一行中给出阈值t,每一片的诊断片只有不小于t个中风因子才会被纳入统计。1代表则存在中风因子,并且当其前后、左右、上下若均为1则视为一个中风因子区域,计数为1。求总数。

 

 

思路:

         需要能模拟出巡查中分因子的算法,相当于bfs。

         由于是一个三维的概念,用三维数组存储原始数据。有六个方向要进行模拟,向上则为g[x + 0][y + 1][z + 0],依次类推,那么就需要一个增量数组,如(0, 1, 0) 代表向上。由于bfs需要队列,那么队列内应当有一个结构体node 能够存储三维下标。

         那么对于什么时候进行执行遍历是需要判断的,如g[x][y][z] = 0,那么不可执行bfs;如最左边的数向左走了(即数组下标越界的问题), 也是不可以的;如当前虽然为1但是他是属于遍历过的,也不应该进行bfs(仔细想下什么时候会出现从队列中读出时发现是遍历过的?这个是关乎bfs控制的原因,即每一次我们都依次枚举一个节点作为bfs的初始遍历节点,会形成一个最小生成数,如果一整片诊断片都是同一最小生成树,那么此后会有n *m - 1次无效枚举)。

         回想一下叙述中需要什么控制结构以及数据存储结构和相应的算法模板,随后就是如何落实具体代码。

 

参考代码:

 

#include<cstdio>
#include<queue>
using namespace std;
int g[1287][129][61], m, n, l, t,  total;
bool vis[1287][129][61];
int dx[6] = {0, 0, -1, 1, 0, 0};
int dy[6] = {1, -1, 0, 0, 0, 0};
int dz[6] = {0, 0, 0, 0, -1, 1};
struct node{int x, y, z;};
bool judge(int x, int y, int z){
	if(x < 0 || x >= m || y < 0 || y >= n || z < 0 || z >= l)	return false;
	if(!g[x][y][z] || vis[x][y][z])	return false;
	return true; 
}
void bfs(int x, int y, int z){
	queue<node> q;
	node temp;
	temp.x = x, temp.y = y, temp.z = z;
	q.push(temp);
	vis[x][y][z] = true;
	int cnt = 0;
	while(!q.empty()){
		temp = q.front();
		q.pop();
		cnt++;
		for(int i = 0; i < 6 ; ++i){
			node newn;
			newn.x = dx[i] + temp.x, newn.y = dy[i] + temp.y, newn.z = dz[i] + temp.z;
			if(judge(newn.x, newn.y, newn.z))	
				q.push(newn), vis[newn.x][newn.y][newn.z] = true;
		}
	}
	if(cnt >= t)	total += cnt;
}
int main(){
	scanf("%d%d%d%d", &m, &n, &l, &t);
	for(int z = 0; z < l; ++z)
		for(int i = 0; i < m; ++i)
			for(int j = 0; j < n; ++j)
				scanf("%d", &g[i][j][z]);
	for(int z = 0; z < l; ++z)
		for(int i = 0; i < m; ++i)
			for(int j = 0; j < n; ++j)
				if(judge(i, j, z))	bfs(i, j, z);
	printf("%d", total);
	return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值