PAT A1091

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.

请输入图片描述
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
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
#include "stdio.h"
//#include "math.h"
#include "string.h"
//#include "iostream"
//#include "stdlib.h"
#include "vector"
//#include "set"
//#include "map"
//#include "stack"
#include "queue"
#include "algorithm"
using namespace std;

//typedef long long LL;
struct Node{
int x, y, z;//位置(x, y, z)
} node;
int n, m, slice, T;//矩阵为n*m,共有slice层,T为卒中核心区1的个数的下限
int pixel[1290][130][61];//三维01矩阵
bool inq[1290][130][61] = {false};//记录位置(x, y, z)是否入过队
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};

bool Judge(int x, int y, int z){//判断(x, y, z)是否需要访问
//越界返回false
if (x >= n || x < 0 || y >= m || y < 0 || z >= slice || z < 0) return false;
//若当前位置为0或者已经入过队,则返回false
if (pixel[x][y][z] == 0 ||inq[x][y][z] == true) return false;
//以上都不满足,返回true
return true;
}
//bfs函数访问位置(x, y, z)所在的块,将该块中所有"1"的inq都设置为true
int BFS(int x, int y, int z){
int tot = 0;//当前块中1的个数
queue<Node> Q;//将结点node入队
node.x = x, node.y = y, node.z = z;//结点node的位置为(x, y, z)
Q.push(node);//将结点node入队
inq[x][y][z] = true;//设置位置(x, y, z)已入过队
while (!Q.empty()) {
Node top = Q.front();//取出队首元素
Q.pop();//队首出队
tot++;
for (int i = 0; i < 6; i++) {//循环6次,得到6个方向
int newX = top.x + X[i];
int newY = top.y + Y[i];
int newZ = top.z + Z[i];
if(Judge(newX, newY, newZ)){//新位置(newX, newY, newZ)需要访问
//设置node坐标
node.x = newX, node.y = newY, node.z = newZ;
Q.push(node);//将结点node入队
inq[newX][newY][newZ] = true;//设置(newX, newY, newZ)已入过队

}
}
}
if (tot >= T) {
return tot;
}else{
return 0;
}
}

int main(){
scanf("%d%d%d%d", &n, &m, &slice, &T);
for (int z = 0; z < slice; z++) {
for (int x = 0; x < n; x++) {
for (int y = 0; y < m; y++) {
scanf("%d", &pixel[x][y][z]);
}
}
}
int ans = 0;//记录卒中核心区中1的个数总和
for (int z = 0; z < slice; z++) {
for (int x = 0; x < n; x++) {
for (int y = 0; y < m; y++) {
//如果当前位置为1,且未被访问,则BFS当前块
if (pixel[x][y][z] == 1 && inq[x][y][z] == false) {
ans += BFS(x, y, z);
}
}
}
}
printf("%d\n", ans);
return 0;
}