您的位置:首页 > 其它

PAT - 肿瘤诊断 - 三维简单广搜

2016-05-17 21:07 387 查看
在诊断肿瘤疾病时,计算肿瘤体积是很重要的一环。给定病灶扫描切片中标注出的疑似肿瘤区域,请你计算肿瘤的体积。

输入格式:

输入第一行给出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

根据题目大意可以知道是统计一个L * M * N大小的长方体中所有权值为1的联通块数量不少于t的总数有多少,直接根据题意模拟就可以。

#include <algorithm>
#include <iostream>
#include <numeric>
#include <cstring>
#include <iomanip>
#include <string>
#include <vector>
#include <cstdio>
#include <queue>
#include <stack>
#include <cmath>
#include <map>
#include <set>
#define LL long long
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
const LL M = 1000005;
const double esp = 1e-6;
const double PI = 3.14159265359;
const LL INF = 0x3f3f3f3f;
using namespace std;

bool Map[70][2345][234];
int m,n,l,t;
int dz[]={1,-1,0,0,0,0};
int dx[]={0,0,0,0,1,-1};
int dy[]={0,0,1,-1,0,0};
struct node{
int z,x,y;
}ant;
bool judge(int z,int x,int y){
if(z < 1 || x < 1 || y < 1 || z > l || x > m || y > n || !Map[z][x][y])
return false;
return true;
}
int bfs(int z,int x,int y){
int num = 1;
queue<node>Q;
Q.push({z,x,y});
Map[z][x][y] = false;
while(!Q.empty()){
ant = Q.front();Q.pop();
for(int i=0;i<6;i++){
if(judge(ant.z+dz[i],ant.x+dx[i],ant.y+dy[i])){
Map[ant.z+dz[i]][ant.x+dx[i]][ant.y+dy[i]] = false;
num++;
Q.push({ant.z+dz[i],ant.x+dx[i],ant.y+dy[i]});
}
}
}
return num >= t? num : 0;
}

int main(){
scanf("%d %d %d %d",&m,&n,&l,&t);
for(int i=1;i<=l;i++)
for(int j=1;j<=m;j++)
for(int k=1;k<=n;k++)
scanf("%d",&Map[i][j][k]);
int ans = 0;
for(int i=1;i<=l;i++)
for(int j=1;j<=m;j++)
for(int k=1;k<=n;k++)
if(Map[i][j][k])
ans += bfs(i,j,k);
printf("%d\n",ans);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: