1091 Acute Stroke(BFS)

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×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三维矩阵中,一部分是正常区域(0表示),一部分是脑卒中区域(1表示),找出那些连通的脑卒中区域,且只有像素数量大于等于给定阈值T的区域才进行累加计算。

思路

理论上讲,本题可用BFS、DFS两种方法,只需对每个值为1的像素进行访问,访问后将值置为0即可(注意搜索时不要越界)。以下为DFS代码:

#include <cstdio>
#include <iostream>
#include <vector> 
using namespace std; 

int dx[6] = {1, 0, -1, 0, 0, 0}, dy[6] = {0, 1, 0, -1, 0, 0};
int dz[6] = {0, 0, 0, 0, 1, -1};
int a[1286][128][60];
int M, N, L, T, v = 0, vol = 0;

bool judge(int i, int j, int k){ //判断是否越界
	return i >= 0 && i < M && j >= 0 && j < N && k >= 0 && k < L;
}

void DFS(int x, int y, int z){
	if(judge(x, y, z) && a[x][y][z] == 1){
		v++;
		a[x][y][z] = 0;
		for(int i = 0; i < 6; i++)
			DFS(x + dx[i], y + dy[i], z + dz[i]);		
	}		
}

int main(){ 		
	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>>a[i][j][k];
	for(int k = 0; k < L; k++)
		for(int i = 0; i < M; i++)
			for(int j = 0; j < N; j++){
				if(a[i][j][k] == 1){
					v = 0;
					DFS(i, j, k);
					if(v >= T) vol += v;
				}
			}
	cout<<vol;
	return 0;
}

DFS的代码更简洁,所以我的首选是它,然而,由于递归层数较多,有两个测试点段错误。这也是DFS的缺点。因此最终还是要用BFS,思路类似。

AC代码

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

struct node{
	int x, y, z;
	node(int _x, int _y, int _z):x(_x), y(_y), z(_z){}
};
int dx[6] = {1, 0, -1, 0, 0, 0}, dy[6] = {0, 1, 0, -1, 0, 0};
int dz[6] = {0, 0, 0, 0, 1, -1};
int a[1286][128][60];
int M, N, L, T, vol = 0;

bool judge(int i, int j, int k){ //判断是否越界
	return i >= 0 && i < M && j >= 0 && j < N && k >= 0 && k < L;
}

void BFS(int x, int y, int z){
	int v = 1; //该区域体积 
	a[x][y][z] = 0;
	queue<node> Q;
	Q.push(node(x, y, z));
	while(!Q.empty()){
		node top = Q.front();
		Q.pop();
		for(int i = 0; i < 6; i++){
			int newX = top.x + dx[i];
			int newY = top.y + dy[i];
			int newZ = top.z + dz[i];
			if(judge(newX, newY, newZ) && a[newX][newY][newZ] == 1){
				a[newX][newY][newZ] = 0;
				v++;
				Q.push(node(newX, newY, newZ));
			}
		}
	}
	if(v >= T) vol += v;
}

int main(){ 		
	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>>a[i][j][k];
	for(int k = 0; k < L; k++)
		for(int i = 0; i < M; i++)
			for(int j = 0; j < N; j++){
				if(a[i][j][k] == 1)
					BFS(i, j, k);
			}
	cout<<vol;
	return 0;
}
发布了110 篇原创文章 · 获赞 0 · 访问量 1262

猜你喜欢

转载自blog.csdn.net/qq_43072010/article/details/105466920