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.

【插图】

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

解题思路:

三维的广度优先遍历,还是第一次遇到,不过思路和二维的是一样的

题意:给出一个三维矩阵,有好几个1组成的块,如果每个块中的1的数目大于T则计入总数,否则不计入总数。统计总数

还是一样的解法,定义好走的方向:

int X[6] = { 0,0,0,0,1,-1 };
int Y[6] = { 0,0,1,-1,0,0 };
int Z[6] = { 1,-1,0,0,0,0 };

把坐标定义成结构体存到队列中进行广度优先遍历

struct Pos {
	int x, y, z;
	Pos(int _x, int _y, int _z) {
		x = _x;
		y = _y;
		z = _z;
	} 
};

AC代码如下:

#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
using namespace std;
struct Pos {
	int x, y, z;
	Pos(int _x, int _y, int _z) {
		x = _x;
		y = _y;
		z = _z;
	} 
};

queue<Pos> BFS_Queue;
int X[6] = { 0,0,0,0,1,-1 };
int Y[6] = { 0,0,1,-1,0,0 };
int Z[6] = { 1,-1,0,0,0,0 };
int BrianSlide[1290][135][65];  //闹切片
bool visited[1290][135][65];    //访问数组
int finalAns = 0;
int main() {
	int M, N, L, T;
	scanf("%d %d %d %d", &M, &N, &L, &T);
	for (int i = 0; i < L; ++i) {
		for (int j = 0; j < M; ++j) {
			for (int k = 0; k < N; ++k) {
				scanf("%d", &BrianSlide[j][k][i]);
				visited[j][k][i] = false;
			}
		}
	}
	int currentX, currentY, currentZ;
	for (int i = 0; i < L; ++i) {
		for (int j = 0; j < M; ++j) {
			for (int k = 0; k < N; ++k) {
				int tempAns = 0;
				if (!visited[j][k][i]) {
					currentX = j, currentY = k, currentZ = i;
					visited[currentX][currentY][currentZ] = true;
					if (BrianSlide[currentX][currentY][currentZ] == 1) tempAns++;
					Pos tpos = { currentX,currentY,currentZ };
					BFS_Queue.push(tpos);
					while (!BFS_Queue.empty()) {
						Pos curPos = BFS_Queue.front();
						BFS_Queue.pop();
						for (int l = 0; l < 6; ++l) {
							int newX = curPos.x + X[l];
							int newY = curPos.y + Y[l];
							int newZ = curPos.z + Z[l];
							if (newX >= 0 && newX < M && newY >= 0 && newY < N && newZ >= 0 && newZ < L && !visited[newX][newY][newZ]) {
								visited[newX][newY][newZ] = true;
								if (BrianSlide[newX][newY][newZ] == 1) {
									tempAns++;
									Pos ttpos = { newX,newY,newZ };
									BFS_Queue.push(ttpos);
								}
							}
						}
					}
				}
				if (tempAns >= T) {
					finalAns += tempAns;
				}
				tempAns = 0;
			}
		}
	}
	printf("%d", finalAns);
	system("PAUSE");
	return 0;
}