题目描述
dfs
样例
#include<iostream>
#include<cstring>
#include<vector>
using namespace std;
const int V = 60;
int M, N, L, T;
int derect[6][3] = { {1,0,0},{0,1,0},{0,0,1},{-1,0,0},{0,-1,0},{0,0,-1} };
//bool vis[61][1287][129];
vector<vector<vector<bool>>> vis;
int dfs(vector<vector<vector<int>>>& brain, int x, int y, int z) {
if (x < 0 || x >= M || y < 0 || y >= N || z < 0 || z >= L || brain[z][x][y] == 0 || vis[z][x][y]) {
return 0;
}
vis[z][x][y] = true;
int pk = 1;
for (int i = 0; i < 6; i++) {
int x1 = x + derect[i][0];
int y1 = y + derect[i][1];
int z1 = z + derect[i][2];
pk += dfs(brain, x1, y1, z1);
}
return pk;
}
int main() {
cin >> M >> N >> L >> T;
vis.resize(L, vector<vector<bool>>(M, vector<bool>(N)));
vector<vector<vector<int>>> brain(L, vector<vector<int>>(M, vector<int>(N)));
for (int l = 0; l < L; l++) {
for (int i = 0; i < M; i++) {
for (int j = 0; j < N; j++) {
cin >> brain[l][i][j];
}
}
}
int ans = 0;
for (int l = 0; l < L; l++) {
for (int i = 0; i < M; i++) {
for (int j = 0; j < N; j++) {
if (!vis[l][i][j] && brain[l][i][j] == 1) {
int t = dfs(brain, i, j, l);
if (t >= T) {
ans += t;
}
}
}
}
}
cout << ans;
return 0;
}