1091 Acute Stroke (30point(s)) - C語言 PAT 甲級

1091 Acute Stroke (30point(s))

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

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 腫瘤,連續腫瘤的大小大於等於 t 表示符合條件的腫瘤

輸出所有符合條件的腫瘤的總大小

設計思路:
  • 廣度優先遍歷,只不過擴展到三維,每個點有 6 個方向需要判斷
  • 6 個方向遍歷的時候,要判斷數組越界,是否連通,是否遍歷過
編譯器:C (gcc)
#include <stdio.h>

#define XMAX 1286
#define YMAX 128
#define ZMAX 60
#define DIRMAX 6

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

int xx[DIRMAX] = {1, -1, 0, 0, 0, 0};
int yy[DIRMAX] = {0, 0, 1, -1, 0, 0};
int zz[DIRMAX] = {0, 0, 0, 0, 1, -1};
int v[XMAX][YMAX][ZMAX];
int visit[XMAX][YMAX][ZMAX];
struct node q[XMAX * YMAX * ZMAX];
int front, rear;
int n, m, l, t;

int judge(int x, int y, int z)
{

        if (x < 0 || x >= m)
                return 0;
        if (y < 0 || y >= n)
                return 0;
        if (z < 0 || z >= l)
                return 0;
        if (v[x][y][z] == 0 || visit[x][y][z] == 1)
                return 0;
        return 1;
}

int bfs(int x, int y, int z)
{
        int sum = 0, i;
        struct node a, temp;
        temp.x = x;
        temp.y = y;
        temp.z = z;
        visit[x][y][z] = 1;
        q[0] = temp;
        front = 0;
        rear = 1;
        while (front != rear) {
                a = q[front++];
                sum++;
                for (i = 0; i < DIRMAX; i++) {
                        temp.x = a.x + xx[i];
                        temp.y = a.y + yy[i];
                        temp.z = a.z + zz[i];
                        if (judge(temp.x, temp.y, temp.z)) {
                                visit[temp.x][temp.y][temp.z] = 1;
                                q[rear++] = temp;
                        }
                }
        }
        return sum >= t ? sum : 0;
}

int main(void)
{
        int sum = 0;
        int i, j, k;
        scanf("%d%d%d%d", &m, &n, &l, &t);
        for (i = 0; i < l; i++)
                for (j = 0; j < m; j++)
                        for (k = 0; k < n; k++)
                                scanf("%d", &v[j][k][i]);
        for (i = 0; i < l; i++)
                for (j = 0; j < m; j++)
                        for (k = 0; k < n; k++)
                                if (v[j][k][i] == 1 && visit[j][k][i] == 0)
                                        sum += bfs(j, k, i);
        printf("%d", sum);
        return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章