题目描述
重返天梯-L3-004 肿瘤诊断 (30 分)
需要理解下,搜索每个像素点的上下左右里外,6个方位,连通超过t的,则符合题意,加入到总体积中
拿样例来说
3 4 5 2
1 1 1 1
1 1 1 1
1 1 1 1
0 0 1 1
0 0 1 1
0 0 1 1
1 0 1 1
0 **1** 0 0
0 0 0 0
1 0 1 1
0 0 0 0
0 0 0 0
0 0 0 1
0 0 0 1
**1** 0 0 0
打*的这两个点,在三维空间中看,都只有它们单独一点,因为不算入总体积中。答案为26
C++ (三维bfs)
#include <iostream>
#include <cstring>
#include <queue>
using namespace std;
struct Node {
int x, y, z;
};
int m, n, l, t;
int arr[1300][140][70];
bool st[1300][140][70];
int dx[6] = {-1, 0, 1, 0, 0, 0}, dy[6] = {0, 1, 0, -1, 0, 0}, dz[6] = {0, 0, 0, 0, 1, -1};
bool check_valid(int x, int y, int z) {
if (x < 0 || x >= m || y < 0 || y >= n || z < 0 || z >= l) return false;
if (!arr[x][y][z] || st[x][y][z]) return false;
return true;
}
int bfs(int x, int y, int z) {
queue<Node> q;
Node t1 = {x, y, z};
q.push(t1);
int cnt = 0;
while(q.size()) {
auto t = q.front();
q.pop();
int xx = t.x, yy = t.y, zz = t.z;
if (st[xx][yy][zz]) continue;
st[xx][yy][zz] = true;
cnt++;
for (int i = 0; i < 6; i++) {
int a = xx + dx[i], b = yy + dy[i], c = zz + dz[i];
if (check_valid(a, b, c)) {
Node temp = {a, b, c};
q.push(temp);
}
}
}
if (cnt >= t) return cnt;
else return 0;
}
int main() {
scanf("%d%d%d%d", &m, &n, &l, &t);
for (int k = 0; k < l; k++)
for (int i = 0; i < m; i++)
for (int j = 0; j < n; j++)
scanf("%d", &arr[i][j][k]);
int ans = 0;
for (int k = 0; k < l; k++)
for (int i = 0; i < m; i++)
for (int j = 0; j < n; j++)
if (arr[i][j][k] && !st[i][j][k])
ans += bfs(i, j, k);
cout << ans << endl;
return 0;
}
|