1091 Acute Stroke (PAT甲级)

文章提供了一个编程问题的解决方案,该问题涉及计算MRI切片中急性脑卒中的核心体积。由于深度优先搜索(DFS)可能导致堆栈溢出,作者选择了广度优先搜索(BFS)来遍历并计算大于给定阈值的连通组件的体积。两个不同的BFS实现被展示,一个是使用动态内存分配的多维向量,另一个是使用固定大小的二维数组。程序读取输入文件中的矩阵数据,然后遍历每个1像素,对满足条件的连通组件进行计数。
摘要由CSDN通过智能技术生成

这道题用dfs做的话,因为递归太多层,堆栈溢出,有两个测试点过不了;所以用bfs。

根据他人代码修改后的结果:

#include <cstdio>
#include <vector>

struct node{
    int height;
    int width;
    int length;
    node(int _height, int _width, int _length): height(_height), width(_width), length(_length){}
};

int M, N, L, T, area, ans;
int x[6] = {1, -1, 0, 0, 0, 0};
int y[6] = {0, 0, 1, -1, 0, 0};
int z[6] = {0, 0, 0, 0, 1, -1};
std::vector<std::vector<std::vector<int>>> vec;

void bfs(int a, int b, int c){
    std::vector<node> curr;
    int pivot = 0;
    int curri, currj, currk, tmpi, tmpj, tmpk;
    vec[a][b][c] = 0;
    curr.push_back(node(a, b, c));
    while(pivot != curr.size()){
        curri = curr[pivot].height;
        currj = curr[pivot].width;
        currk = curr[pivot].length;
        for(int i = 0; i < 6; ++i){
            tmpi = curri + x[i];
            tmpj = currj + y[i];
            tmpk = currk + z[i];
            if(tmpi < 0 || tmpi >= L || tmpj < 0 || tmpj >= M || tmpk < 0 || tmpk >= N || vec[tmpi][tmpj][tmpk] == 0){
                continue;
            }
            vec[tmpi][tmpj][tmpk] = 0;
            curr.push_back(node(tmpi, tmpj, tmpk));
        }
        ++pivot;
    }
    area = curr.size();
}

int main(){
    scanf("%d %d %d %d", &M, &N, &L, &T);
    std::vector<int> line(N, 0);
    std::vector<std::vector<int>> plane(M, line);
    vec.resize(L, plane);
    for(int i = 0; i < L; ++i){
        for(int j = 0; j < M; ++j){
            for(int k = 0; k < N; ++k){
                scanf("%d", &vec[i][j][k]);
            }
        }
    }
    ans = 0;
    for(int i = 0; i < L; ++i){
        for(int j = 0; j < M; ++j){
            for(int k = 0; k < N; ++k){
                if(vec[i][j][k] == 1){
                    bfs(i, j, k);
                    if(area >= T){
                        ans += area;
                    }
                }
            }
        }
    }
    printf("%d", ans);
    return 0;
}

比较笨拙的写法:

#include <cstdio>
#include <vector>
const int MAXM = 1286;
const int MAXN = 128;
const int MAXL = 60;

struct node{
    int height;
    int width;
    int length;
    node(int _height, int _width, int _length): height(_height), width(_width), length(_length){}
};

int M, N, L, T, area, ans;
int vec[MAXL][MAXM][MAXN];

int bfs(int a, int b, int c){
    std::vector<node> curr;
    int pivot = 0;
    int i, j, k, tmpi, tmpj, tmpk;
    vec[a][b][c] = 0;
    curr.push_back(node(a, b, c));
    while(pivot != curr.size()){
        i = curr[pivot].height;
        j = curr[pivot].width;
        k = curr[pivot].length;
        if(i + 1 < L && vec[i + 1][j][k] == 1){
            vec[i + 1][j][k] = 0;
            curr.push_back(node(i + 1, j, k));
        }
        if(i - 1 >= 0 && vec[i - 1][j][k] == 1){
            vec[i - 1][j][k] = 0;
            curr.push_back(node(i - 1, j, k));
        }
        if(j + 1 < M && vec[i][j + 1][k] == 1){
            vec[i][j + 1][k] = 0;
            curr.push_back(node(i, j + 1, k));
        }
        if(j - 1 >= 0 && vec[i][j - 1][k] == 1){
            vec[i][j - 1][k] = 0;
            curr.push_back(node(i, j - 1, k));
        }
        if(k + 1 < N && vec[i][j][k + 1] == 1){
            vec[i][j][k + 1] = 0;
            curr.push_back(node(i, j, k + 1));
        }
        if(k - 1 >= 0 && vec[i][j][k - 1] == 1){
            vec[i][j][k - 1] = 0;
            curr.push_back(node(i, j, k - 1));
        }
        ++pivot;
    }
    return curr.size();
}

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", &vec[i][j][k]);
            }
        }
    }
    ans = 0;
    for(int i = 0; i < L; ++i){
        for(int j = 0; j < M; ++j){
            for(int k = 0; k < N; ++k){
                if(vec[i][j][k] == 1){
                    area = bfs(i, j, k);
                    if(area >= T){
                        ans += area;
                    }
                }
            }
        }
    }
    printf("%d", ans);
    return 0;
}

题目如下:

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, Land 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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值