If recursion is used directly, the segment error is caused by too many recursive layers
So use queue instead of recursion
//too many recursive call tiers, using queue instead#include<iostream>#include<vector>#include<queue>using namespacestd;structPoint {intz, x, y; Point (intZintXinty): Z (z), x (x), Y (y) {}};intMain () {intm, N, L, T; Vector<vector<vector<int>>>Matrix; scanf ("%d %d%d%d", &m, &n, &l, &t); for(intK =0; K! = l; ++k) {vector<vector<int>>v1; for(inti =0; I! = m; ++i) {vector<int>v2; for(intj =0; J! = N; ++j) {intnum; scanf ("%d", &num); V2.push_back (num); } v1.push_back (v2); } matrix.push_back (v1); } intTotalv =0; intv; Queue<Point>pointlist; for(intK =0; K! = l; ++k) { for(inti =0; I! = m; ++i) { for(intj =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 (); intz =p.z; intx =p.x; inty =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