机试刷题:PAT A 1091 Acute Stroke 的BFS和DFS方法解析

PAT A 1091 Acute Stroke 题目

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.

figstroke.jpg

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/1矩阵中,找到那些含有连续*个1的区域。对于每个区域,如果1的个数大于等于给定的T,就将该区域中1的个数累加进最终结果。题目是一个图的遍历问题,跟找联通区域有点像,下面是bfs和dfs两种方法:

bfs

#include<bits/stdc++.h>
#define M 1300
#define N 130
#define L 60
using namespace std;
struct xyz //bfs队列中保存下标,而不是值,用于修改图中的内容来以标识已经访问,从而省去visit数组
{
    int i, j, k;
    xyz(int ti, int tj, int tk)
    {
        i = ti;
        j = tj;
        k = tk;
    }
};
int m, n, l, t;
int g[M][N][L];
int tsum, res;
queue<xyz> q; //声明为全局变量(在main()外部),是在程序堆区,不容易发生内存不足

bool valid(int i, int j, int k)
{
    return (i >=0 && i<m && j>=0 && j<n && k>=0 && k<l);
}

void bfs()
{
    while(!q.empty()){
        xyz x = q.front();
        q.pop();
        if(valid(x.i, x.j, x.k) && g[x.i][x.j][x.k] == 1){
            tsum++;
            g[x.i][x.j][x.k] = 0;
            q.push(xyz(x.i-1, x.j, x.k));
            q.push(xyz(x.i+1, x.j, x.k));
            q.push(xyz(x.i, x.j-1, x.k));
            q.push(xyz(x.i, x.j+1, x.k));
            q.push(xyz(x.i, x.j, x.k-1));
            q.push(xyz(x.i, x.j, x.k+1));
        }
    }
}

int main()
{
    cin>>m>>n>>l>>t;
    for(int k=0; k<l; k++){
        for(int i=0; i<m; i++){
            for(int j=0; j<n; j++){
                cin>>g[i][j][k];
            }
        }
    }
    for(int k=0; k<l; k++){
        for(int i=0; i<m; i++){
            for(int j=0; j<n; j++){
                if(g[i][j][k] == 1){
                    q.push(xyz(i, j, k));
                    tsum = 0;
                    bfs();
                    if(tsum >= t){
                        res += tsum;
                    }
                }
            }
        }
    }

    cout<<res;
    return 0;
}

dfs

首先来看一个除了最后两个测试样例不过的例子

#include<bits/stdc++.h>
#define M 1300
#define N 130
#define L 60
using namespace std;
int m, n, l, t;
int g[M][N][L];
int tsum, res;

bool valid(int i, int j, int k)
{
    return (i >=0 && i<m && j>=0 && j<n && k>=0 && k<l);
}

void dfs(int i, int j, int k)
{
    if(valid(i, j, k) && g[i][j][k] == 1){
        tsum++;
        g[i][j][k] = 0;
        dfs(i-1, j, k);
        dfs(i+1, j, k);
        dfs(i, j-1, k);
        dfs(i, j+1, k);
        dfs(i, j, k-1);
        dfs(i, j, k+1);
    }
}

int main()
{
    cin>>m>>n>>l>>t;
    for(int k=0; k<l; k++){
        for(int i=0; i<m; i++){
            for(int j=0; j<n; j++){
                cin>>g[i][j][k];
            }
        }
    }
    for(int k=0; k<l; k++){
        for(int i=0; i<m; i++){
            for(int j=0; j<n; j++){
                if(g[i][j][k] == 1){
                    tsum = 0;
                    dfs(i, j, k);
                    if(tsum >= t){
                        res += tsum;
                    }
                }
            }
        }
    }

    cout<<res;
    return 0;
}

代码显然比bfs简单,但是当图中有很多的“1”时,递归使得系统栈太深,导致segmentation fault

dfs

系统栈会满主要是递归的时候,系统栈保存了太多函数调用信息,那我们用用户栈代替系统栈,只在栈中保存数据信息就不容易满了,而且用户栈可以申请在程序的堆区。

#include<bits/stdc++.h>
#define M 1300
#define N 130
#define L 60
using namespace std;
struct xyz //bfs队列中保存下标,而不是值,用于修改图中的内容来以标识已经访问,从而省去visit数组
{
    int i, j, k;
    xyz(int ti, int tj, int tk)
    {
        i = ti;
        j = tj;
        k = tk;
    }
};
int m, n, l, t;
int g[M][N][L];
int tsum, res;
stack<xyz> st; //用户栈申请在程序堆区

bool valid(int i, int j, int k)
{
    return (i >=0 && i<m && j>=0 && j<n && k>=0 && k<l);
}

void dfs()
{
    while(!st.empty()){
        xyz x = st.top();
        st.pop();
        if(valid(x.i, x.j, x.k) && g[x.i][x.j][x.k] == 1){
            tsum++;
            g[x.i][x.j][x.k] = 0;
            st.push(xyz(x.i-1, x.j, x.k));
            st.push(xyz(x.i+1, x.j, x.k));
            st.push(xyz(x.i, x.j-1, x.k));
            st.push(xyz(x.i, x.j+1, x.k));
            st.push(xyz(x.i, x.j, x.k-1));
            st.push(xyz(x.i, x.j, x.k+1));
        }
    }
}

int main()
{
    cin>>m>>n>>l>>t;
    for(int k=0; k<l; k++){
        for(int i=0; i<m; i++){
            for(int j=0; j<n; j++){
                cin>>g[i][j][k];
            }
        }
    }
    for(int k=0; k<l; k++){
        for(int i=0; i<m; i++){
            for(int j=0; j<n; j++){
                if(g[i][j][k] == 1){
                    st.push(xyz(i, j, k));
                    tsum = 0;
                    dfs();
                    if(tsum >= t){
                        res += tsum;
                    }
                }
            }
        }
    }

    cout<<res;
    return 0;
}

刚开始写博客,难免会有知识点上的疏漏,希望大家能够谅解!如有错误还望告知 😃

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值