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: MMM, NNN, LLL and TTT, where MMM and NNN are the sizes of each slice (i.e. pixels of a slice are in an M×NM×NM×N matrix, and the maximum resolution is 1286 by 128); L(≤60)L (≤60)L(≤60) is the number of slices of a brain; and TTT is the integer threshold (i.e. if the volume of a connected core is less than TTT, then that core must not be counted).
Then LLL slices are given. Each slice is represented by an M×NM×NM×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 TTT 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
Solution:
// Talk is cheap, show me the code
// Created by Misdirection 2021-09-04 13:25:31
// All rights reserved.
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
struct Node{
int x, y, z;
Node(int a, int b, int c){
x = a;
y = b;
z = c;
}
~Node(){}
};
int m, n, l, t;
int graph[62][1290][130];
bool vis[62][1290][130];
int bfs(int x, int y, int z){
int cnt = 0;
queue<Node> q;
q.emplace(x, y, z);
vis[x][y][z] = true;
while(!q.empty()){
cnt++;
int tmpX = q.front().x;
int tmpY = q.front().y;
int tmpZ = q.front().z;
q.pop();
if(tmpX - 1 >= 0 && graph[tmpX - 1][tmpY][tmpZ] == 1 && !vis[tmpX - 1][tmpY][tmpZ]){
q.emplace(tmpX - 1, tmpY, tmpZ);
vis[tmpX - 1][tmpY][tmpZ] = true;
}
if(tmpX + 1 < l && graph[tmpX + 1][tmpY][tmpZ] == 1 && !vis[tmpX + 1][tmpY][tmpZ]){
q.emplace(tmpX + 1, tmpY, tmpZ);
vis[tmpX + 1][tmpY][tmpZ] = true;
}
if(tmpY - 1 >= 0 && graph[tmpX][tmpY - 1][tmpZ] == 1 && !vis[tmpX][tmpY - 1][tmpZ]){
q.emplace(tmpX, tmpY - 1, tmpZ);
vis[tmpX][tmpY - 1][tmpZ] = true;
}
if(tmpY + 1 < m && graph[tmpX][tmpY + 1][tmpZ] == 1 && !vis[tmpX][tmpY + 1][tmpZ]){
q.emplace(tmpX, tmpY + 1, tmpZ);
vis[tmpX][tmpY + 1][tmpZ] = true;
}
if(tmpZ - 1 >= 0 && graph[tmpX][tmpY][tmpZ - 1] == 1 && !vis[tmpX][tmpY][tmpZ - 1]){
q.emplace(tmpX, tmpY, tmpZ - 1);
vis[tmpX][tmpY][tmpZ - 1] = true;
}
if(tmpZ + 1 < n && graph[tmpX][tmpY][tmpZ + 1] == 1 && !vis[tmpX][tmpY][tmpZ + 1]){
q.emplace(tmpX, tmpY, tmpZ + 1);
vis[tmpX][tmpY][tmpZ + 1] = true;
}
}
if(cnt >= t) return cnt;
else return 0;
}
int main(){
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", &graph[i][j][k]);
}
}
int ans = 0;
for(int i = 0; i < l; ++i){
for(int j = 0; j < m; ++j){
for(int k = 0; k < n; ++k){
if(graph[i][j][k] == 1 && !vis[i][j][k]) ans += bfs(i, j, k);
}
}
}
printf("%d\n", ans);
return 0;
}