1091 Acute Stroke (PAT甲级)

这道题用dfs做的话,因为递归太多层,堆栈溢出,有两个测试点过不了;所以用bfs。

但令我百思不得其解的是,我没用方向变量x[6], y[6], z[6],直接老老实实算每一个方向的话,最后一个测试点过不了;但理论上来说,实现的本质应该是完全一样的。如有大神看到,望不吝赐教。

无法全部通过的写法;这里的i, j, k即AC代码中的curri, currj, currk:

        if(i + 1 < L && vec[i + 1][j][k] == 1){
            vec[i + 1][j][k] = 0;
            curr.push_back(node(i + 1, j, k));
        }
        if(i - 1 >= 0 && vec[i - 1][j][k] == 1){
            vec[i - 1][j][k] = 0;
            curr.push_back(node(i - 1, j, k));
        }
        if(j + 1 < M && vec[i][j + 1][k] == 1){
            vec[i][j + 1][k] = 0;
            curr.push_back(node(i, j + 1, k));
        }
        if(j - 1 >= 0 && vec[i][j - 1][k] == 1){
            vec[i][j - 1][k] = 0;
            curr.push_back(node(i, j - 1, k));
        }
        if(k + 1 >= 0 && vec[i][j][k + 1] == 1){
            vec[i][j][k + 1] = 0;
            curr.push_back(node(i, j, k + 1));
        }
        if(k - 1 >= 0 && vec[i][j][k - 1] == 1){
            vec[i][j][k - 1] = 0;
            curr.push_back(node(i, j, k - 1));
        }

AC的代码:

#include 
#include 

struct node{
    int height;
    int width;
    int length;
    node(int _height, int _width, int _length): height(_height), width(_width), length(_length){}
};

int M, N, L, T, area, ans;
int x[6] = {1, -1, 0, 0, 0, 0};
int y[6] = {0, 0, 1, -1, 0, 0};
int z[6] = {0, 0, 0, 0, 1, -1};
std::vector>> vec;

void bfs(int a, int b, int c){
    std::vector curr;
    int pivot = 0;
    int curri, currj, currk, tmpi, tmpj, tmpk;
    vec[a][b][c] = 0;
    curr.push_back(node(a, b, c));
    while(pivot != curr.size()){
        curri = curr[pivot].height;
        currj = curr[pivot].width;
        currk = curr[pivot].length;
        for(int i = 0; i < 6; ++i){
            tmpi = curri + x[i];
            tmpj = currj + y[i];
            tmpk = currk + z[i];
            if(tmpi < 0 || tmpi >= L || tmpj < 0 || tmpj >= M || tmpk < 0 || tmpk >= N || vec[tmpi][tmpj][tmpk] == 0){
                continue;
            }
            vec[tmpi][tmpj][tmpk] = 0;
            curr.push_back(node(tmpi, tmpj, tmpk));
        }
        ++pivot;
    }
    area = curr.size();
}

int main(){
    scanf("%d %d %d %d", &M, &N, &L, &T);
    std::vector line(N, 0);
    std::vector> plane(M, line);
    vec.resize(L, plane);
    for(int i = 0; i < L; ++i){
        for(int j = 0; j < M; ++j){
            for(int k = 0; k < N; ++k){
                scanf("%d", &vec[i][j][k]);
            }
        }
    }
    ans = 0;
    for(int i = 0; i < L; ++i){
        for(int j = 0; j < M; ++j){
            for(int k = 0; k < N; ++k){
                if(vec[i][j][k] == 1){
                    bfs(i, j, k);
                    if(area >= T){
                        ans += area;
                    }
                }
            }
        }
    }
    printf("%d", ans);
    return 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, Land 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.

1091 Acute Stroke (PAT甲级)_第1张图片

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

 

你可能感兴趣的:(PAT甲级,算法,pat考试)