PAT甲级:A1091 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.
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的点为中风块的可疑点,0则是正常的。现在给定阈值,如果某一区域的中风块数量大于给定阈值则视这块为病变区,现在要求计算总的病变区体积。
- 分析:这里显然是考察的BFS广度优先搜索,题目给定的是三维图,实际上二维的图没什么区别,二维中邻接点为相连的点(无向图中),这里的三维则是二维上的上下左右和第三维(即垂直方向)的上和下,一共是6个方向来表示邻接点。因此,首先用三维数组存储图,之后用X、Y、Z数组依次存放坐标改变的增量,这样用一个for循环遍历坐标的增量数组很轻松就能找到邻接点,之后和BFS同样的做法,用队列完成BFS。要注意的是,这里给定的三维的坐标,对于边界上的点,计算邻接点时会出现越界的情况的,那么相较于传统判断某点是否可访问的条件,这里还需要判断是否越界,越界了也是不能访问的。之后因为图可能是不连通的,所以需要将各个连通子图所得出的大于阈值的体积相加。
#include <bits/stdc++.h>
using namespace std;
struct node {
int x, y, z;
};
int n, m, l, t, e[65][1300][130];
int X[] = {0, 0, 1, -1, 0, 0}, Y[] = {0, 0, 0, 0, 1, -1},
Z[] = {1, -1, 0, 0, 0, 0};
bool inq[65][1300][130] = {false};
bool isVisitable(int x, int y, int z) {
return !(x < 0 || x >= m || y < 0 || y >= n || z < 0 || z >= l) &&
e[z][x][y] != 0 && !inq[z][x][y];
}
int bfs(node a) {
queue<node> q;
q.push(a);
inq[a.z][a.x][a.y] = true;
int sum = 0;
while (!q.empty()) {
node now = q.front();
q.pop();
int x = now.x, y = now.y, z = now.z;
sum += e[z][x][y];
for (int i = 0; i < 6; i++) {
x = now.x + X[i];
y = now.y + Y[i];
z = now.z + Z[i];
if (isVisitable(x, y, z)) {
inq[z][x][y] = true;
q.push({x, y, z});
}
}
}
return sum >= t ? sum : 0;
}
int main() {
scanf("%d%d%d%d", &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++) scanf("%d", &e[k][i][j]);
int ans = 0;
for (int k = 0; k < l; k++)
for (int i = 0; i < m; i++)
for (int j = 0; j < n; j++)
if (isVisitable(i, j, k)) ans += bfs({i, j, k});
printf("%d", ans);
return 0;
}