PAT-A | 1091 | Acute Stroke

10 篇文章 0 订阅
2 篇文章 0 订阅

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

the total volume:总量


解题思路:

关键点:图的连通分量。

自己第一遍并没有想到这是一道图的连通分量的问题,谷歌别人的答案才看明白的。我做出来后再回头读了读题,发现这道题说的还是不明白。


这样理解吧:

先输入M,N,L,T,接着输入L个slice(切片),每个切片由MXN矩阵组成,矩阵的元素是0或者1;T是求连通分量时用到的一个阈值(threshold)。

然而拿到L个切片的信息就懵了,根本不知道和图的连通分量有什么关系!

(以下为事后诸葛亮的解答)

1. 你可以把脑子brain想成一个长方体,然后切成了L个切片,于是这道题给了你L个切片的信息。

你需要意识到的是,这L个切片根据输入的顺序应该从下到上或者从上到下垒起来,这样你就把L个切片组成一个长方体了。(我根据输入的顺序把他们从下到上垒起来)

现在你知道有一个长方体,这个长方体有L层,每层由一个MxN矩阵组成,矩阵的每个元素不是1就是0。好了,你可以在脑子里建立这么一个由0,1组成的长方体图像了。

2. 然后,关键来了,这个长方体是个三维空间,在这个三维空间内,一个区域region内的1的个数超过T(前面输入的)才能统计,否则不算数。统计一个区域内1的个数,这就成了统计连通分量里顶点个数的问题了!然后去DFS吧!但是据说用递归版的DFS由于递归层数太多会爆栈,所以用非递归版的DFS。DFS时需要意识到,一个点的邻接点是它的前后左右上下6个点(DFS中neig[6][3]),这就是你看到那个莫名其妙的图片的意思了。


#include <iostream>
#include <stack>

using namespace std;

struct node {
	int x;
	int y;
	int z;
	node(int a, int b, int c) : x(a), y(b), z(c) {}
};

int M, N, L, T;
int ***data;
bool ***visited;

int DFS(int x, int y, int z);

int main()
{
	int sum = 0;

	cin >> M >> N >> L >> T;

	// allocate memory
	// PS M N L
	data = new int**[M];
	visited = new bool**[M];
	for (int i = 0; i != M; ++i) {
		data[i] = new int*[N];
		visited[i] = new bool*[N];
		for (int j = 0; j != N; ++j) {
			data[i][j] = new int[L];
			visited[i][j] = new bool[L];
		}
	}

	// input and initialize
	// PS M N L
	for (int z = 0; z != L; ++z) {
		for (int x = 0; x != M; ++x) {
			for (int y = 0; y != N; ++y) {
				cin >> data[x][y][z];
				visited[x][y][z] = false;
			}
		}
	}

	// DFS
	for (int z = 0; z != L; ++z) {
		for (int x = 0; x != M; ++x) {
			for (int y = 0; y != N; ++y) {
				if (data[x][y][z]) {//key
					int cnt;
					cnt = DFS(x, y, z);
					sum += (cnt >= T ? cnt : 0);
				}
			}
		}
	}

	cout << sum << endl;
}

int DFS(int x, int y, int z)
{
	if (visited[x][y][z]) return 0;
	int neig[6][3];
	int cnt = 0;
	stack<node> s;

	visited[x][y][z] = true;
	s.push(node(x, y, z));
	while (!s.empty()) {
		node top = s.top(); s.pop();
		++cnt;
		neig[0][0] = top.x + 1; neig[0][1] = top.y; neig[0][2] = top.z;
		neig[1][0] = top.x - 1; neig[1][1] = top.y; neig[1][2] = top.z;
		neig[2][0] = top.x; neig[2][1] = top.y + 1; neig[2][2] = top.z;
		neig[3][0] = top.x; neig[3][1] = top.y - 1; neig[3][2] = top.z;
		neig[4][0] = top.x; neig[4][1] = top.y; neig[4][2] = top.z + 1;
		neig[5][0] = top.x; neig[5][1] = top.y; neig[5][2] = top.z - 1;
		for (int i = 0; i < 6; ++i) {
			int a = neig[i][0];
			int b = neig[i][1];
			int c = neig[i][2];
			if (a < 0 || b < 0 || c < 0 || a >= M || b >= N || c >= L) continue;
			if (!visited[a][b][c] && data[a][b][c]) {
				s.push(node(a, b, c));
				visited[a][b][c] = true;
			}
		}

	}

	return cnt;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值