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

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

这里的每个切片是二维的,然后所有的切片是一层一层叠加起来的,也就是变成了三维的图的遍历问题,我一开始用的深度优先搜索,最后两个测试点出现了段错误,应该是递归的层次太多了,后来想了一下可以直接广度优先搜索,然后就AC了

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#include <cstdio>
#include <queue>

using namespace std;

int acute[60][1286][128], m = 0, n = 0, l = 0;

struct Index {
int i, j, k;

Index(int m_i, int m_j, int m_k) {
i = m_i;
j = m_j;
k = m_k;
}
};

void bfs(int i, int j, int k, int &temp) {
queue<struct Index> q;
q.push(Index(i, j, k));
while (q.size() != 0) {
struct Index t = q.front();
q.pop();
i = t.i;
j = t.j;
k = t.k;
if (k > 0 && acute[i][j][k - 1] == 1) { // left
temp += 1;
acute[i][j][k - 1] = 0;
q.push(Index(i, j, k - 1));
}

if (k < n - 1 && acute[i][j][k + 1] == 1) { // right
temp += 1;
acute[i][j][k + 1] = 0;
q.push(Index(i, j, k + 1));
}

if (i > 0 && acute[i - 1][j][k] == 1) { // up
temp += 1;
acute[i - 1][j][k] = 0;
q.push(Index(i - 1, j, k));
}

if (i < l - 1 && acute[i + 1][j][k] == 1) { // down
temp += 1;
acute[i + 1][j][k] = 0;
q.push(Index(i + 1, j, k));

}

if (j > 0 && acute[i][j - 1][k] == 1) { // front
temp += 1;
acute[i][j - 1][k] = 0;
q.push(Index(i, j - 1, k));
}

if (j < m - 1 && acute[i][j + 1][k] == 1) { // back
temp += 1;
acute[i][j + 1][k] = 0;
q.push(Index(i, j + 1, k));
}
}
}

int main() {
int t = 0;
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", &acute[i][j][k]);
}
}
}

int cnt = 0;
for (int i = 0; i < l; i++) {
for (int j = 0; j < m; j++) {
for (int k = 0; k < n; k++) {
if (acute[i][j][k] == 1) {
int temp = 1;
acute[i][j][k] = 0;
bfs(i, j, k, temp);
if (temp >= t) cnt += temp;
}
}
}
}
printf("%d\n", cnt);
return 0;
}