PTA甲级 1091 Acute Stroke (C++)

本文介绍了一种算法,用于识别MRI扫描中急性脑卒中核心区域的体积,通过分析像素矩阵并应用深度优先搜索来确定小于阈值的连通区域。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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;
}

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

负反馈循环

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值