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×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
分析:遍历所有像素,若若干病变像素(超过T个)构成一个连通区域,则被计入总和。最后输出区域的病变像素块和。将输入的数组转化为三维矩阵(高度作为最后一维,即长宽为M和N,高度为L), 然后遍历数组计数即可。用DFS会有段错误,因此使用BFS。
代码:
#include<iostream>
#include<algorithm>
#include<queue>
#define MAXN 200
#define MAXL 61
using namespace std;
struct Pixel {
int x;
int y;
int z;
Pixel(int _x, int _y, int _z) {
x = _x;
y = _y;
z = _z;
}
};
int M, N, L, T;
int brain[MAXN][MAXN][MAXN] = { 0 };
int visit[MAXN][MAXN][MAXN] = { 0 };
int dire[6][3] = { {-1, 0, 0}, {1, 0, 0}, {0, -1, 0}, {0, 1, 0}, {0, 0, -1}, {0, 0, 1} };
int ans = 0;
void bfs(int x, int y, int z, int &tempSum) {
tempSum++;
visit[x][y][z] = 1;
queue<Pixel> q;
q.push(Pixel(x, y, z));
while (q.empty() == 0) {
Pixel top = q.front();
q.pop();
for (int i = 0; i < 6; i++) {
int nx = top.x + dire[i][0];
int ny = top.y + dire[i][1];
int nz = top.z + dire[i][2];
if (nx >= 0 && ny >= 0 && nz >= 0 && nx < MAXN && ny < MAXN && nz < MAXL && visit[nx][ny][nz] == 0 && brain[nx][ny][nz] == 1) {
q.push(Pixel(nx, ny, nz));
visit[nx][ny][nz] = 1;
tempSum++;
}
}
}
}
void dfs() {
for (int i = 0; i < L; i++) {
for (int j = 0; j < M; j++) {
for (int k = 0; k < N; k++) {
if (visit[j][k][i] == 0 && brain[j][k][i] == 1) {
int tempSum = 0;
bfs(j, k, i, tempSum);
ans += tempSum >= T ? tempSum : 0;
}
}
}
}
}
int main() {
cin >> 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++) {
cin >> brain[j][k][i];
}
}
}
dfs();
cout << ans << endl;
return 0;
}