# PAT-A 真题- 1091. Acute Stroke

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`

slice ： 切片                    pixels ： 像素

matrix ： 矩阵                 threshold ：阈值

represented：代表      　thickness：层

constant ： 常数，常量   obtain ：获得，达到，存在

separated：分开的          regions ：区域

volumes ： 量                 hence ：从此，因此

``````#include <iostream>
#include <queue>
using namespace std;

int x, y, z;
int T;

typedef struct{
int data;
bool checked;
}node;
node map;

typedef struct{
int x, y, z;
}point;

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

bool judge(int _x, int _y, int _z){
if(_x >= x || _x < 0 || _y >= y || _y < 0 || _z >= z || _z < 0)
return false;
return !!(map[_x][_y][_z].data) && !(map[_x][_y][_z].checked);
}

int BFS(int _x, int _y, int _z){
int num_1 = 1;
queue<point> Q;
point top, tmp;
top.x = _x, top.y = _y, top.z = _z;
Q.push(top);
map[_x][_y][_z].checked = true;
while(!Q.empty()){
top = Q.front();
Q.pop();
for(int i = 0; i < 6; i++){
int newX = top.x + X[i], newY = top.y + Y[i], newZ = top.z + Z[i];
if(judge(newX, newY, newZ)){
tmp.x = newX, tmp.y = newY, tmp.z = newZ;
Q.push(tmp);
map[newX][newY][newZ].checked = true;
num_1++;
}
}
}
if(num_1 >= T) return num_1;
else return 0;
}

int main(){
cin >> y >> x >> z >> T;
for(int zi = 0; zi < z; zi++)
for(int yi = 0; yi < y; yi++)
for(int xi = 0; xi < x; xi++){
cin >> map[xi][yi][zi].data;
map[xi][yi][zi].checked = false;
}
int result = 0;
for(int zi = 0; zi < z; zi++)
for(int yi = 0; yi < y; yi++)
for(int xi = 0; xi < x; xi++){
if((!map[xi][yi][zi].checked) && map[xi][yi][zi].data)
result += BFS(xi, yi, zi);
}
cout << result << endl;
return 0;
}``````