PAT甲级真题 1091 Acute Stroke (30分) C++实现(bfs累计节点数,dfs会栈溢出)

题目

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.
在这里插入图片描述
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

思路

题目没有明确描述的关键信息是:矩阵是按照输入次序决定上下次序的。

问题转化为三维空间上图的遍历问题,需要求出每个连通分支的总节点个数,如果超过了阈值t,则计入总数。

首先尝试了dfs,代码比较简洁,结果测试点4、5段错误,因为递归调用层数太多:

void dfs(vector<vector<vector<int> > > &e, int i, int j, int k, int *num){
    if (i<0 || j<0 || k<0 || i>=l || j>=m || k>=n || e[i][j][k]!=1){
        return;
    }
    (*num)++;
    e[i][j][k] = 0;  //表示已访问过
    dfs(e, i-1, j, k, num);
    dfs(e, i+1, j, k, num);
    dfs(e, i, j-1, k, num);
    dfs(e, i, j+1, k, num);
    dfs(e, i, j, k-1, num);
    dfs(e, i, j, k+1, num);
}

在这里插入图片描述
改用bfs,顺利AC。
在这里插入图片描述

具体做法是:

  1. 将访问过的节点值置为0(省去了visited数组),对所有未访问过的节点进行bfs;
  2. 对于每个节点的bfs,考虑其六个方向的相邻位置,如果值为1则加入队列并累计个数,直到队列空为止;
  3. 主函数中统计符合要求的bfs返回值即可。

代码

改为bfs:

#include <iostream> 
#include <vector> 
#include <queue> 
using namespace std; 

int m, n, l, t;
int total = 0 ;

struct Pos{
    int i, j, k;
};

int bfs(vector<vector<vector<int> > > &e, int i, int j, int k){
    int num = 1;
    queue<Pos> que;
    que.push({i, j, k});
    e[i][j][k] = 0;  //表示已访问过
    while(!que.empty()){
        Pos p = que.front();
        que.pop();
        int i = p.i;
        int j = p.j;
        int k = p.k;
        if (i>0 && e[i-1][j][k]==1){
            que.push({i-1, j, k});
            e[i-1][j][k] = 0;
            num++;
        }
        if (i<l-1 && e[i+1][j][k]==1){
            que.push({i+1, j, k});
            e[i+1][j][k] = 0;
            num++;
        }
        if (j>0 && e[i][j-1][k]==1){
            que.push({i, j-1, k});
            e[i][j-1][k] = 0;
            num++;
        }
        if (j<m-1 && e[i][j+1][k]==1){
            que.push({i, j+1, k});
            e[i][j+1][k] = 0;
            num++;
        }
        if (k>0 && e[i][j][k-1]==1){
            que.push({i, j, k-1});
            e[i][j][k-1] = 0;
            num++;
        }
        if (k<n-1 && e[i][j][k+1]==1){
            que.push({i, j, k+1});
            e[i][j][k+1] = 0;
            num++;
        }
    }
    return num;
}

int main() { 
    scanf("%d %d %d %d", &m, &n, &l, &t);
    vector<vector<vector<int> > > e(l, vector<vector<int> >(m, vector<int>(n)));
    for (int i=0; i<l; i++){
        for (int j=0; j<m; j++){
            for (int k=0; k<n; k++){
                scanf("%d", &e[i][j][k]);
            }
        }
    }
    
    for (int i=0; i<l; i++){
        for (int j=0; j<m; j++){
            for (int k=0; k<n; k++){
                if (e[i][j][k]==1){
                    int num = bfs(e, i, j, k);
                    if (num >= t){
                        total += num;
                    }
                }
            }
        }
    }

    printf("%d\n", total);
    return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值