题解
补充个dfs做法,Acwing过,PAT段错误
C++ 代码
#include <iostream>
#include <cstring>
using namespace std;
const int M = 1300, N = 130, L = 65;
int n, m, l, t;
bool a[L][M][N];
const int d[][3] = {
{1, 0, 0},
{-1, 0, 0},
{0, 1, 0},
{0, -1, 0},
{0, 0, 1},
{0, 0, -1},
};
int dfs(int i, int j, int k)
{
// cout << i << " " << j << " " << k << ":" << endl;
int res = 0;
a[i][j][k] = false;
for (int p = 0; p < 6; p ++ )
{
// cout << d[l][0] << " " << d[l][1] << " " << d[l][2] << endl;
int z = d[p][0] + i;
int y = d[p][1] + j;
int x = d[p][2] + k;
if (x >= 0 && x < n && y >= 0 && y < m && z >= 0 && z < l && a[z][y][x]) res += dfs(z, y, x);
}
return res + 1;
}
int main()
{
cin >> m >> n >> l >> t;
for (int i = 0; i < l; i ++ )
{
for (int j = 0; j < m; j ++ )
{
for (int k = 0; k < n; k ++ )
{
cin >> a[i][j][k];
}
}
}
int res = 0;
for (int i = 0; i < l; i ++ )
{
for (int j = 0; j < m; j ++ )
{
for (int k = 0; k < n; k ++ )
{
if (a[i][j][k])
{
int cnt = dfs(i, j, k);
if (cnt >= t) res += cnt;
// cout << cnt << endl;
}
}
}
}
cout << res << endl;
return 0;
}
PAT段错误的原因在这里~
OK收到