肿瘤诊断(三维BFS)

 题目地址:https://www.patest.cn/contests/gplt/L3-004

肿瘤诊断 

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

输入格式:

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

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

输出格式:

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

输入样例:

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

不知道这题用DFS为什么会有两个案例段错误,难道是爆了系统栈?知道的大神能不能告诉一下,谢了。。。
DFS代码:
 
#include <iostream> #include <cstdio> #include <cmath> #include <queue> #include <stack> #include <map> #include <algorithm> #include <vector> #include <string> #include <cstring> #include <sstream> using namespace std; int pic[80][1300][150]; bool vis[80][1300][150]; int m,n,h,T; int cnt; int di[10]={-1,1,0,0,0,0}; int dj[10]={0,0,-1,1,0,0}; int dk[10]={0,0,0,0,1,-1}; void dfs(int i,int j,int k) { if(i<0||i>=h||j<0||j>=m||k<0||k>=n) return; if(vis[i][j][k]==true) return; if(pic[i][j][k]==0) return; vis[i][j][k]=true; cnt++; for(int a=0; a<6; a++) { int x=i+di[a]; int y=j+dj[a]; int z=k+dk[a]; dfs(x,y,z); } } int main() { memset(vis,false,sizeof(vis)); scanf("%d%d%d%d",&m,&n,&h,&T); for(int i=0;i<h;i++) { for(int j=0;j<m;j++) { for(int k=0;k<n;k++) { scanf("%d",&pic[i][j][k]); } } } int ans=0; for(int i=0;i<h;i++) { for(int j=0;j<m;j++) { for(int k=0;k<n;k++) { if(vis[i][j][k]==false&&pic[i][j][k]==1) { cnt=0; dfs(i,j,k); if(cnt>=T) { ans+=cnt; } } } } } printf("%d\n",ans); return 0; } 


BFS代码:

 
#include <iostream> #include <cstdio> #include <cmath> #include <queue> #include <stack> #include <map> #include <algorithm> #include <vector> #include <string> #include <cstring> #include <sstream> using namespace std; int pic[80][1300][150]; bool vis[80][1300][150]; int m,n,h,T; int cnt; int di[10]={-1,1,0,0,0,0}; int dj[10]={0,0,-1,1,0,0}; int dk[10]={0,0,0,0,1,-1}; int ans; struct note { int i,j,k; note(int x,int y,int z) { i=x; j=y; k=z; } }; typedef struct note note; queue<note> Q; void bfs(int i,int j,int k) { note tmp(i,j,k); Q.push(tmp); while(!Q.empty()) { note a=Q.front(); Q.pop(); if(a.i>=0&&a.i<h&&a.j>=0&&a.j<m&&a.k>=0&&a.k<n) { if(vis[a.i][a.j][a.k]==false && pic[a.i][a.j][a.k]==1) { vis[a.i][a.j][a.k]=true; cnt++; for(int c=0; c<6; c++) { int x=a.i+di[c]; int y=a.j+dj[c]; int z=a.k+dk[c]; if(x>=0&&y>=0&&z>=0&&x<h&&y<m&&z<n&&vis[x][y][z]==false&&pic[x][y][z]==1) { note temp(x,y,z); Q.push(temp); } } } } } } int main() { memset(vis,false,sizeof(vis)); scanf("%d%d%d%d",&m,&n,&h,&T); for(int i=0;i<h;i++) { for(int j=0;j<m;j++) { for(int k=0;k<n;k++) { scanf("%d",&pic[i][j][k]); } } } for(int i=0;i<h;i++) { for(int j=0;j<m;j++) { for(int k=0;k<n;k++) { if(vis[i][j][k]==false&&pic[i][j][k]==1) { cnt=0; bfs(i,j,k); if(cnt>=T) { ans+=cnt; } } } } } printf("%d\n",ans); return 0; } 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值