三维空间的DFS,挺新颖的一题
AC代码:
#include <vector> #include <cstdio> #include <queue> using namespace std; int x[] = {0,0,1,0,-1,0}; int y[] = {0,1,0,-1,0,0}; int z[] = {-1,0,0,0,0,1}; class node{ public: int x; int y; int z; node(int xx,int yy,int zz):x(xx),y(yy),z(zz){} node(){} }; int main(){ int n,m,l,t; scanf("%d %d %d %d",&n,&m,&l,&t); vector<vector<vector<int>>> cub; vector<vector<vector<bool>>> vs; for(int i = 0;i < l;i++){ vector<vector<int>> p; vector<vector<bool>> pvs; for(int j = 0;j < n;j++){ vector<int> line; vector<bool> vline; for(int k = 0;k < m;k++){ int tmp; scanf("%d",&tmp); line.push_back(tmp); vline.push_back(false); } p.push_back(line); pvs.push_back(vline); } cub.push_back(p); vs.push_back(pvs); } int num(0); for(int i = 0;i < l;i++){ for(int j = 0;j < n;j++){ for(int k = 0;k < m;k++){ if(vs[i][j][k] || cub[i][j][k] == 0) continue; else{ queue<node> q; node tmp(j,k,i); q.push(tmp); vs[i][j][k] = true; int count(1); while(!q.empty()){ for(int s = 0;s < 6;s++){ node son(q.front().x + x[s],q.front().y + y[s],q.front().z + z[s]); if(son.z >= 0 && son.z < l && son.x >= 0 && son.x < n && son.y >= 0 && son.y < m && !vs[son.z][son.x][son.y] && cub[son.z][son.x][son.y] == 1){ vs[son.z][son.x][son.y] = true; q.push(son); count++; } } q.pop(); } if(count >= t) num += count; } } } } printf("%d\n",num); return 0; }
时间: 2024-10-11 13:14:58