ZJU PAT 1091

如果直接使用递归,会因为递归层数过多导致段错误

因此使用queue代替递归

//递归调用层数过多,使用queue代替

#include <iostream>
#include <vector>
#include <queue>

using namespace std;

struct Point {
    int z, x, y;
    Point(int z, int x, int y) : z(z), x(x), y(y) {}
};

int main() {
    int m, n, l, t;
    vector<vector<vector<int>>> matrix;

    scanf("%d %d %d %d", &m, &n, &l, &t);
    for (int k = 0; k != l; ++k) {
        vector<vector<int>> v1;
        for (int i = 0; i != m; ++i) {
            vector<int> v2;
            for (int j = 0; j != n; ++j) {
                int num;
                scanf("%d", &num);
                v2.push_back(num);
            }
            v1.push_back(v2);
        }
        matrix.push_back(v1);
    }

    int totalV = 0;
    int v;
    queue<Point> pointList;

    for (int k = 0; k != l; ++k) {
        for (int i = 0; i != m; ++i) {
            for (int j = 0; j != n; ++j) {
                if (matrix[k][i][j] == 1) {
                    v = 0;
                    pointList.push(Point(k, i, j));

                    while (!pointList.empty()) {
                        Point p = pointList.front();
                        int z = p.z;
                        int x = p.x;
                        int y = p.y;
                        pointList.pop();

                        if (matrix[z][x][y] == 1) {
                            matrix[z][x][y] = 2;
                            ++v;

                            if (z > 0) {
                                pointList.push(Point(z - 1, x, y));
                            }
                            if (z < l - 1) {
                                pointList.push(Point(z + 1, x, y));
                            }
                            if (x > 0) {
                                pointList.push(Point(z, x - 1, y));
                            }
                            if (x < m - 1) {
                                pointList.push(Point(z, x + 1, y));
                            }
                            if (y > 0) {
                                pointList.push(Point(z, x, y - 1));
                            }
                            if (y < n - 1) {
                                pointList.push(Point(z, x, y + 1));
                            }
                        }
                    }

                    totalV += v >= t ? v : 0;
                }
            }
        }
    }

    printf("%d", totalV);

    return 0;
}
时间: 2024-10-29 03:33:39

ZJU PAT 1091的相关文章

PAT 1091. Acute Stroke (30)

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

PAT 1091 Acute Stroke [难][bfs]

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 th

zju pat 1015

#include <iostream> #include <string.h> #include <math.h> using namespace std; bool prime(int num) { int i; if(num ==0 || num ==1) return false; for(i=2;i<=sqrt(double(num));i++) { if(num % i ==0) return false; } return true; } int co

ZJU PAT 1090

依题意构造树并遍历,找出最大深度并统计 #include <iostream> #include <vector> using namespace std; struct Node { vector<int> children; int depth; }; Node nodes[100005]; int maxDepth = 0; int total = 1; void travel(int n, int d) { Node& node = nodes[n];

1091. Acute Stroke (30)【搜索】——PAT (Advanced Level) Practise

题目信息 1091. Acute Stroke (30) 时间限制400 ms 内存限制65536 kB 代码长度限制16000 B 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,

PAT 乙级 1091 N-自守数 (15 分)

1091 N-自守数 (15 分) 如果某个数 K 的平方乘以 N 以后,结果的末尾几位数等于 K,那么就称这个数为"N-自守数".例如 3×92?2??=25392,而 25392 的末尾两位正好是 92,所以 92 是一个 3-自守数. 本题就请你编写程序判断一个给定的数字是否关于某个 N 是 N-自守数. 输入格式: 输入在第一行中给出正整数 M(≤20),随后一行给出 M 个待检测的.不超过 1000 的正整数. 输出格式: 对每个需要检测的数字,如果它是 N-自守数就在一行中

PAT甲级——1091 Acute Stroke (广度优先搜索BFS)

本文同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/94207638 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

【PAT甲级】1091 Acute Stroke (30分)

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

PAT甲题题解-1091. Acute Stroke (30)-BFS

题意:给定三维数组,0表示正常,1表示有肿瘤块,肿瘤块的区域>=t才算是肿瘤,求所有肿瘤块的体积和 这道题一开始就想到了dfs或者bfs,但当时看数据量挺大的,以为会导致栈溢出,所以并没有立刻写,想有没有别的办法.然而结果是,实在想不出别的办法了,所以还是尝试写写dfs.bfs. 一开始先用了dfs,最后两个样例段错误,估计是栈溢出了.之所以dfs栈溢出,因为dfs的时候每个状态都会存储在堆栈里,就好比dfs的第一个状态,一直保存到最后整个dfs结束.而bfs是存储在队列中,每次队列都会有状态取