首页 > 代码库 > ZJU PAT 1091
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;}
ZJU PAT 1091
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。