1091-Acute Stroke (30)

1004

题目

题目描述

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.

输入描述:

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.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).


Figure 1

输出描述:

For each case, output in a line the total volume of the stroke core.

输入例子:

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

输出例子:

26

算法分析

图:BFS、连通块

STL:队列的使用<queue>

三维图问题,求出1个数大于阙值的连通块数目。

连通块问题容易让人想到DFS,但本题需要在6个方向上遍历,递归的层数极多,对堆栈的要求高,容易造成栈溢出。故本题选用BFS进行遍历。

代码

#include <iostream>
#include <queue>
using namespace std;

const int MAXX = 1286;
const int MAXY = 128;
const int MAXZ = 60;
const int di_x[6] = { -1,1,0,0,0,0 };	//沿X轴的移动方向,下同
const int di_y[6] = { 0,0,-1,1,0,0 };
const int di_z[6] = { 0,0,0,0,-1,1 };
int graph[MAXX][MAXY][MAXZ] = { 0 };
int M, N, L, T;		//x,y,z,threshold
int count1 = 0;	//符合要求的像素点1的个数
typedef struct Node {
	Node(int a, int b, int c) : x(a), y(b), z(c) {}
	int x, y, z;
}Node;

void BFS(int x, int y, int z) {
	queue<Node> q;
	q.push(Node(x, y, z));		//进队
	int cur_count = 1;
	graph[x][y][z] = 0;		//将该像素点标记为已遍历

	while (!q.empty()) {
		Node temp = q.front();		//队列头元素
		q.pop();
		graph[temp.x][temp.y][temp.z] = 0;	//标记当前点

		for (int i = 0; i < 6; i++) {	//处理6个方向
			int newx = temp.x + di_x[i];
			int newy = temp.y + di_y[i];
			int newz = temp.z + di_z[i];

			if (newx < 0 || newx >= M || newy < 0 || newy >= N || newz < 0 || newz >= L)
				continue;
			if (0 == graph[newx][newy][newz])
				continue;
			//是连通点
			cur_count++;
			graph[newx][newy][newz] = 0;
			q.push(Node(newx, newy, newz));		//BFS,每访问完一个点(无论是否有效)都把它加入到队列尾部
		}
	}
	if (cur_count >= T)
		count1 += cur_count;

	return;
}

int main(void) {
	cin >> M >> N >> L >> T;
	for (int k = 0; k < L; k++)
		for (int i = 0; i < M; i++)
			for (int j = 0; j < N; j++)		//注意三维空间的遍历顺序!!!
				cin >> graph[i][j][k];

	//不是强连通图,故在DFS/BFS时需要外加循环遍历
	for (int k = 0; k < L; k++)
		for (int i = 0; i < M; i++)
			for (int j = 0; j < N; j++)
				if (1 == graph[i][j][k])
					BFS(i, j, k);

	cout << count1 << endl;
	system("pause");
	return 0;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章