PAT A 1091(甲级)

PAT甲级试题1091要求计算急性脑卒中核心区域的体积。给定MRI切片,通过广度优先搜索(BFS)方法,识别并计算每个连接核心区域的体积,只有当体积大于阈值T时才计入。题目难点在于理解这是一个三维问题,需要进行三维BFS操作。AC代码已给出,欢迎纠错。
摘要由CSDN通过智能技术生成

1091 Acute Stroke(30 分)

作者: CHEN, Yue

单位: 浙江大学

时间限制: 600 ms

内存限制: 64 MB

代码长度限制: 16 KB

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.

【插图】

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

 


 

这道题我在做的时候没有完全读懂题目,导致没有认识到这不是一个二维平面的bfs,而是一个三维的bfs。英语还是硬伤啊

 

回归正题,其实题目读懂了思路不难想出来:就是一个三维的bfs,设置三个数组存储6个方向x, y, z各需要哪些动作,然后记录区域中为1的数目就可以了,最后只有1的数目大于T时才被记入,否则就不计入。

 


 

AC代码:

#include <cstdio>
#include <string.h>
#include <queue>

using namespace std;

int brain[65][1290][133];
int m, n, l, t, total = 0;
int posx[] = {1, 0, -1, 0, 0, 0};
int posy[] = {0, 1, 0, -1, 0, 0};
int posz[] = {0, 0, 0, 0, 1, -1};
bool enqueue[65][1290][133] = {0};

struct Node {
    int x, y, z;
};

void bfs();
bool judge(int, int, int);

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", &brain[i][j][k]);
            }
        }
    }

    bfs();
    printf("%d\n", total);

    return 0;
}

void bfs() {
    for(int i = 0; i < l; ++i) {
        for(int j = 0; j < m; ++j) {
            for(int k = 0; k < n; ++k) {
                if(brain[i][j][k] == 1 && !enqueue[i][j][k]) {
                    queue<Node> q;
                    Node n;
                    n.z = i, n.y = j, n.x = k;
                    q.push(n);
                    enqueue[i][j][k] = true;
                    int rnum = 0;
                    while(q.size() != 0) {
                        for(int ii = 0; ii < 6; ++ii) {
                            int newx = q.front().x + posx[ii];
                            int newy = q.front().y + posy[ii];
                            int newz = q.front().z + posz[ii];
                            if(judge(newx, newy, newz)) {
                                Node jn;
                                jn.x = newx, jn.y = newy, jn.z = newz;
                                enqueue[newz][newy][newx] = true;
                                q.push(jn);
                            }
                        }

                        q.pop();
                        ++rnum;
                    }

                    if(rnum >= t) {
                        total += rnum;
                    }
                }
            }
        }
    }
}

bool judge(int x, int y, int z) {
    if(x < 0 || y < 0 || x >= n || y >= m || z < 0 || z >= l) {
        return false;
    }

    if(brain[z][y][x] == 0 || enqueue[z][y][x]) {
        return false;
    }

    return true;
}

 


 

如有错误,欢迎指摘。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值