Pat(A) 1091. Acute Stroke (30)

原题目:

原题链接:https://www.patest.cn/contests/pat-a-practise/1091

1091. Acute Stroke (30)


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.

这里写图片描述 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

题目大意

给定三维数组,求连通节点数目,当一个连通子图的节点数目小于T时,不计入总数。

解题报告

一般就是用BFS去做了,但如果用递归,会报段错误,因为栈太多,所以用队列优化。

代码

#include "iostream"
#include "queue"
using namespace std;

struct node{
    int l;
    int m;
    int n;
};

int G[65][1300][130];
int M,N,L,T;
int ans = 0;
int X[6] = {1, 0, 0, -1, 0, 0};
int Y[6] = {0, 1, 0, 0, -1, 0};
int Z[6] = {0, 0, 1, 0, 0, -1};

bool judge(int x, int y, int z) {
    if(x < 0 || x >= L || y < 0 || y >= M || z < 0 || z >= N) return false;
    if(G[x][y][z] == 0) return false;
    return true;
}

void init(){
    cin>>M>>N>>L>>T;
    int i,j,k;
    for(i = 0; i < L; i++){
        for(j = 0; j < M; j++){
            for(k = 0; k < N; k++)
                scanf("%d",&G[i][j][k]);
        }
    }
}

int bfs(int x, int y, int z){
    queue<struct node> que;
    int cnt = 0;
    struct node temp;
    temp.l = x;
    temp.m = y;
    temp.n = z;
    G[x][y][z] = 0;
    que.push(temp);
    while(!que.empty()){
        struct node top = que.front();
        que.pop();
        cnt ++;
        for(int i = 0; i < 6; i++) {
            int tx = top.l + X[i];
            int ty = top.m + Y[i];
            int tz = top.n + Z[i];
            if(judge(tx, ty, tz)) {
                G[tx][ty][tz] = 0;
                temp.l = tx, temp.m = ty, temp.n = tz;
                que.push(temp);
            }
        }
    }
    return (cnt>=T)?cnt:0;
}

int main(){
    init();
    int i,j,k;
    for(i = 0; i < L; i++){
        for(j = 0; j < M; j++){
            for(k = 0; k < N; k++)
                if(G[i][j][k])
                    ans += bfs(i,j,k);
        }
    }
    cout<<ans<<endl;
    //system("pause");
}

发表评论

    

最新评论

    还没有人评论...

Powered by Django 2. Copyright © 2014.

huchengbei.com. All rights reserved.