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;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章