PAT-A 真题- 1091. Acute Stroke

发布于 / PAT-甲级 / 0 条评论

原题干:

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

生词:

slice : 切片                    pixels : 像素

matrix : 矩阵                 threshold :阈值

represented:代表       thickness:层

constant : 常数,常量   obtain :获得,达到,存在

separated:分开的          regions :区域

volumes : 量                 hence :从此,因此


题目大意:

第一行给出四个正整数:M ,N,L,T

接着给出 L 行M*N的矩阵(可以理解为立体的,每一层是M*N的平面矩阵,高度为L)

以0表示正常,1表示异常

上、下、左、右、前、后代表相邻区域,若干个相邻的 1 称之为一块

如果某个块的 1 的个数不低于 T 个,则称这一块为卒中核心区。求卒中核心区的所有1之和


这道题虽然是三维的,但是可以和之前写过的二维的是一个道理,可以参考这篇文章:C语言-广度优先搜索(BFS)-斐斐のBlog

这里注意不建议使用DFS,因为题目数据量比较大,使用递归很容易把系统栈搞爆栈。

有了那篇文章作为基础,使用BFS算法很好写:

#include <iostream>
#include <queue>
using namespace std;

int x, y, z;
int T;

typedef struct{
    int data;
    bool checked; 
}node;
node map[1290][130][61];

typedef struct{
    int x, y, z;
}point;

int X[] = {0, 0, 0, 0, 1, -1},
    Y[] = {0, 0, 1, -1, 0, 0},
    Z[] = {1, -1, 0, 0, 0, 0};

bool judge(int _x, int _y, int _z){
    if(_x >= x || _x < 0 || _y >= y || _y < 0 || _z >= z || _z < 0)
        return false;
    return !!(map[_x][_y][_z].data) && !(map[_x][_y][_z].checked);
}

int BFS(int _x, int _y, int _z){
    int num_1 = 1;
    queue<point> Q;
    point top, tmp;
    top.x = _x, top.y = _y, top.z = _z;
    Q.push(top);
    map[_x][_y][_z].checked = true;
    while(!Q.empty()){
        top = Q.front();
        Q.pop();
        for(int i = 0; i < 6; i++){
            int newX = top.x + X[i], newY = top.y + Y[i], newZ = top.z + Z[i];
            if(judge(newX, newY, newZ)){
                tmp.x = newX, tmp.y = newY, tmp.z = newZ;
                Q.push(tmp);
                map[newX][newY][newZ].checked = true;
                num_1++;
            }
        }
    }
    if(num_1 >= T) return num_1;
    else return 0;
}

int main(){
    cin >> y >> x >> z >> T;
    for(int zi = 0; zi < z; zi++)
        for(int yi = 0; yi < y; yi++)
            for(int xi = 0; xi < x; xi++){
                cin >> map[xi][yi][zi].data;
                map[xi][yi][zi].checked = false;
            }
    int result = 0;
    for(int zi = 0; zi < z; zi++)
        for(int yi = 0; yi < y; yi++)
            for(int xi = 0; xi < x; xi++){
                if((!map[xi][yi][zi].checked) && map[xi][yi][zi].data)
                result += BFS(xi, yi, zi);
            }
    cout << result << endl;
    return 0;
}

转载原创文章请注明,转载自: 斐斐のBlog » PAT-A 真题- 1091. Acute Stroke
目前还没有评论,快来抢沙发吧~