重返天梯-L3-004 肿瘤诊断 (30 分)(三维bfs)

题目描述

重返天梯-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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

CodeSlogan

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值