1091Acute 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,LandT, whereMandNare the sizes of each slice (i.e. pixels of a slice are in anM×Nmatrix, and the maximum resolution is 1286 by 128);L(≤60) is the number of slices of a brain; andTis the integer threshold (i.e. if the volume of a connected core is less thanT, then that core must not be counted).
ThenLslices are given. Each slice is represented by anM×Nmatrix 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 thanTare counted. Two pixels areconnectedand 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
notice
- 三维数组读入时的for循环中,首先循环z,在循环x, y 。
- 只有符合条件的才会入队,因此可以在出队时将进行num ++,对1计数。
code
#include
#include
using namespace std;
int m, n, l, t;
struct Node
{
int x, y, z;
}node;
int pixel[1300][130][70];
bool inq[1300][130][70];
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};
queue q;
bool judge(int x, int y, int z)
{
if (x >= m || x < 0 || y >= n || y < 0 || z >= l || z < 0)
return false;
if (pixel[x][y][z] == 0 || inq[x][y][z] == true) return false;
return true;
}
int bfs(int x, int y, int z)
{
int num = 0;
node.x = x, node.y = y, node.z = z;
q.push(node);
inq[x][y][z] = true;
while (!q.empty())
{
Node top = q.front();
q.pop();
num ++;
for (int i = 0; i < 6; i ++)
{
int newX = top.x + X[i];
int newY = top.y + Y[i];
int newZ = top.z + Z[i];
if (judge(newX, newY, newZ))
{
node.x = newX, node.y = newY, node.z = newZ;
q.push(node);
inq[newX][newY][newZ] = true;
}
}
}
if (num >= t)
return num;
else
return 0;
}
int main()
{
//freopen("test.txt", "r", stdin);
scanf("%d %d %d %d", &m, &n, &l, &t);
for (int z = 0; z < l; z ++)
for (int x = 0; x < m; x ++)
for (int y = 0; y < n; y ++)
scanf("%d", &pixel[x][y][z]);
int ans = 0;
for (int z = 0; z < l; z ++)
for (int x = 0; x < m; x ++)
for (int y = 0; y < n;y ++)
if (inq[x][y][z] == false && pixel[x][y][z] == 1)
ans += bfs(x, y, z);
printf("%d\n", ans);
return 0;
}