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");
}
發佈了58 篇原創文章 · 獲贊 114 · 訪問量 158萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章