1091 Acute Stroke (30分)

1 问题

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×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×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

2 解析

  • 题意:求厚度为L的M行*N列的矩阵中的块的1相邻的个数超过T的块的个数
  • 思路:
    • 用BFS遍历矩阵
      • 1 定义队列
      • 2 写一个while循环,并判断队列非空
      • 3 取出并访问队首元素,队首元素出队
      • 4 将top下一层未入队的结点全部入队,并设置为已入队
      • 5 如果块中1的个数大于阈值,则计入统计

3 参考代码

#include <cstdio>
#include <queue>

using std::queue;


int pixels[1300][130][100];//三位0、1矩阵
bool inq[1300][130][100] = {false};
int N, M, L, T;//N行*M列的矩阵,厚度为L
int X[] = {1, -1, 0, 0, 0, 0};//增量矩阵
int Y[] = {0, 0, 1, -1, 0, 0};
int Z[] = {0, 0, 0, 0, 1, -1};

struct node
{
    int x, y, z;
}Node;

bool Judge(int x, int y, int z){//判断是否需要访问
    //越界
    if(x < 0 || x >= N || y < 0 || y >= M || z < 0 || z >= L) return false;
    //当前为0或已入过队
    if(inq[x][y][z] == true || pixels[x][y][z] == 0) return false;
    return true;
}

int BFS(int x,int y,int z){
    queue<node> Q;//1 定义队列
    Node.x = x;
    Node.y = y;
    Node.z = z;
    Q.push(Node);
    int total = 0;
    inq[x][y][z] = true;//设置为已入队
    while(!Q.empty()){//2 写一个while循环,判断队列非空
        node top = Q.front();
        Q.pop();//3 取出并访问队首元素,队首元素出队,
        total++;
        for (int i = 0; i < 6; ++i)//4 将top下一层结点未入队的结点全部入队,并设置为已入队
        {
            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(total < T){//如果没有超过阈值,则没有不计算在内
        return 0;
    }else{
        return total;
    }

}

int main(int argc, char const *argv[])
{
    scanf("%d%d%d%d", &N, &M, &L, &T);

    for (int z = 0; z < L; ++z)//L个矩阵
    {
        for (int x = 0; x < N; ++x)//N行
        {
            for (int y = 0; y < M; ++y)//M列
            {
                scanf("%d", &pixels[x][y][z]);
            }
        }
    }

    int ans = 0;//记录卒中核中1的个数
    for (int z = 0; z < L; ++z)
    {
        for (int x = 0; x < N; ++x)
        {
            for (int y = 0; y < M; ++y)
            {
                if(inq[x][y][z] == false && pixels[x][y][z] == 1){
                    ans += BFS(x, y, z);
                }
            }
        }
    }


    printf("%d\n", ans);

    return 0;
}
已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 岁月 设计师:pinMode 返回首页