L3-004. 肿瘤诊断 - bfs - 天梯赛练习

L3-004. 肿瘤诊断

在诊断肿瘤疾病时,计算肿瘤体积是很重要的一环。给定病灶扫描切片中标注出的疑似肿瘤区域,请你计算肿瘤的体积。

输入格式:

输入第一行给出4个正整数:M、N、L、T,其中M和N是每张切片的尺寸(即每张切片是一个M×N的像素矩阵。最大分辨率是1286×128);L(<=60)是切片的张数;T是一个整数阈值(若疑似肿瘤的连通体体积小于T,则该小块忽略不计)。

最后给出L张切片。每张用一个由0和1组成的M×N的矩阵表示,其中1表示疑似肿瘤的像素,0表示正常像素。由于切片厚度可以认为是一个常数,于是我们只要数连通体中1的个数就可以得到体积了。麻烦的是,可能存在多个肿瘤,这时我们只统计那些体积不小于T的。两个像素被认为是“连通的”,如果它们有一个共同的切面,如下图所示,所有6个红色的像素都与蓝色的像素连通。


Figure 1

输出格式:

在一行中输出肿瘤的总体积。

输入样例:

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


解题思路

其实就是求连通块的个数,只不过二维变成三维,一样的

讲道理bfs和dfs都能做,但是我写的dfs代码过不了最后两个样例, 原因是递归层数太多,dfs代码不能AC仅供参考



AC代码

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <string>
#include <cstring>
#include <set>
#include <queue>
#include <stack>
#include <stdlib.h>
#include <map>

using namespace std;

const int maxn = 10000;
const int INF = 0x3f3f3f3f;

int g[1300][130][65];
bool visit[1300][130][65];
int t;///阈值
int n,m,l;
int dx[6] = {-1,1,0,0,0,0};
int dy[6] = {0,0,1,-1,0,0};
int dz[6] = {0,0,0,0,-1,1};

struct node{
    int x,y,z;
};


bool ok(int x, int y, int z){
    if(x>=m || x<0 || y<0 || y>=n || z>=l || z<0 || visit[x][y][z] || g[x][y][z] == 0) return false;
    return true;
}


int bfs(int i,int j,int k){///行、列、层
    node s;
    int v = 0;
    s.x = i; s.y = j; s.z = k;
    queue<node> q;
    q.push(s);
    visit[i][j][k] = true;
    while(!q.empty()){
        node now  = q.front();
        v++;
        q.pop();
        for(int i = 0; i<6; ++i){
            node next;
            next.x = now.x+dx[i];
            next.y = now.y+dy[i];
            next.z = now.z+dz[i];

            if(ok(next.x, next.y, next.z)){
                q.push(next);
                visit[next.x][next.y][next.z] = true;
            }
        }
    }
    if(v >= t) return v;
    else return 0;

}



int main()
{
    scanf("%d%d%d%d",&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)///列
                scanf("%d",&g[j][k][i]);
        }
    }

    int  ans = 0;

    for(int i = 0; i<l; ++i)///层
        for(int j = 0; j < m; ++j)///行
            for(int k = 0; k < n; ++k){///列
                if(g[j][k][i]==1 && visit[j][k][i] == false)
                    ans += bfs(j,k,i);
            }
    printf("%d",ans);

    return 0;

}

dfs代码-4 5 样例不过

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <string>
#include <cstring>
#include <set>
#include <queue>
#include <stack>
#include <stdlib.h>
#include <map>

using namespace std;

const int maxn = 10000;
const int INF = 0x3f3f3f3f;

int g[1300][130][65];
bool visit[1300][130][65];
int t;///阈值
int n,m,l;
int dx[6] = {-1,1,0,0,0,0};
int dy[6] = {0,0,1,-1,0,0};
int dz[6] = {0,0,0,0,-1,1};

struct node{
    int x,y,z;
};


bool ok(int x, int y, int z){
    if(x>=m || x<0 || y<0 || y>=n || z>=l || z<0 || visit[x][y][z] || g[x][y][z] == 0) return false;
    return true;
}


int dfs(int x,int y,int z){///行、列、层
    int v = 1;
    visit[x][y][z] = true;
    for(int i = 0; i<6; ++i){
        node next;
        next.x = x+dx[i];
        next.y = y+dy[i];
        next.z = z+dz[i];

        if(ok(next.x, next.y, next.z)){
            v += dfs(next.x, next.y, next.z);
        }
    }
    return v;
}



int main()
{
    scanf("%d%d%d%d",&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)///列
                scanf("%d",&g[j][k][i]);
        }
    }

    int  ans = 0;

    for(int i = 0; i<l; ++i)///层
        for(int j = 0; j < m; ++j)///行
            for(int k = 0; k < n; ++k)///列
                if(g[j][k][i]==1 && visit[j][k][i] == false){
                    int x = dfs(j,k,i);
                    if(x >= t)
                        ans += x;
                }

    printf("%d",ans);

    return 0;

}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值