1091. Acute Stroke (30)

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


题目分析

三维空间求连通分量大小,如果达到阈值就加入总体积,dfs和bfs都可以采用,注意访问前后标识符的变化;
本文采用了两种方法分别实现了代码;


代码实现

dfs深搜解法
#include <stdio.h>
int val[65][1300][130];
bool info[65][1300][130];
int M, N, L, T;
int dfs(int i, int j, int k, int m) {
    if (i < 0 || i >= L || j < 0 || j >= M || k < 0 || k >= N || info[i][j][k]) return 0;
    info[i][j][k] = true;
    if (val[i][j][k] == 0) return 0;
    m ++;
    m += dfs(i-1, j, k, 0) + dfs(i+1, j, k, 0) + dfs(i, j-1, k, 0);
    m += dfs(i, j+1, k, 0) + dfs(i, j, k-1, 0) + dfs(i, j, k+1, 0);
    return m;
}
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", &val[i][j][k]);
            }
        }
    }
    int sum = 0;
    for (int i=0; i<L; i++) {
        for (int j=0; j<M; j++) {
            for (int k=0; k<N; k++) {
                if (!info[i][j][k] && val[i][j][k] == 1) {
                    int temp = dfs(i, j, k, 0);
                    if (temp >= T) sum += temp;
                }
            }
        }
    }
    printf("%d", sum);
    return 0;
}
bfs广搜解法
#include <stdio.h>
#include <queue>
using namespace std;
int val[65][1300][130];
bool info[65][1300][130];
int M, N, L, T;
int step[6][3] = {{-1, 0, 0}, {0, -1, 0}, {0, 0, -1}, {1, 0, 0}, {0, 1, 0}, {0, 0, 1}};
struct node {
    int x, y, z;
};
queue<node> q;
bool check(int i, int j, int k) {
    if (i < 0 || i >= L || j < 0 || j >= M || k < 0 || k >= N || info[i][j][k]) return false;
    info[i][j][k] = true;
    return val[i][j][k] == 1 ? true : false;
}
int bfs(int i, int j, int k, int m) {
    info[i][j][k] = true;
    m ++;
    node head = {i, j, k};
    q.push(head);
    while (!q.empty()) {
        node temp = q.front();
        q.pop();
        for (int t=0; t<6; t++) {
            head.x = temp.x + step[t][0];
            head.y = temp.y + step[t][1];
            head.z = temp.z + step[t][2];
            if (check(head.x, head.y, head.z)) {
                m ++;
                q.push(head);
            }
        }
    }
    return m >= T ? m : 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", &val[i][j][k]);
            }
        }
    }
    int sum = 0;
    for (int i=0; i<L; i++) {
        for (int j=0; j<M; j++) {
            for (int k=0; k<N; k++) {
                if (!info[i][j][k] && val[i][j][k] == 1) {
                    sum += bfs(i, j, k, 0);
                }
            }
        }
    }
    printf("%d", sum);
    return 0;
}

发现问题

  • 第一种方法里的深搜算法实现的代码里,在PAT官网提交的过程中,在最后两个大数据测试点出现了段错误;
    这里写图片描述

  • 以及在牛客网上提交的代码
    这里写图片描述

  • 第二种广搜算法,也就是大多数人采用的方法(百度上一搜,大家解题思路基本一摸一样),照着思路自己实现了一下,也的确是全部通过,但为什么深搜算法却出现段错误呢?不得而知。。。

终于找到原因

  • 经过和某位大神的讨论,发掘问题应该是出在递归上,在最后两个大数据点,递归调用很吃栈,所以出现所谓的“段错误”,要想不出现那种错误,直接广度优先遍历是最好的方法;

总结

  • 感谢 hdsjbzy 前辈不遗余力的指教,而且手把手教写代码,从时间和空间都做到极力的优化,主要是非常耐心的,真的非常感激!贴上前辈的二维 py 代码;

这里写图片描述
这里写图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值