L3-004. 肿瘤诊断_bfs_2018_3_8

5 篇文章 0 订阅

L3-004. 肿瘤诊断

时间限制
400 ms
内存限制
65536 kB
代码长度限制
8000 B
判题程序
Standard
作者
陈越

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

输入格式:

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

提交代码

#include<iostream>
#include<cstring>
#include<queue>
using namespace std;
int mp[1290][130][65];
bool vis[1290][130][65];

int fx[6]={0,0,0,0,1,-1};
int fy[6]={0,0,1,-1,0,0};
int fz[6]={1,-1,0,0,0,0};

int ans;

struct AA{
	int a,b,c;
	AA(int aa,int bb,int cc):a(aa),b(bb),c(cc){}
};

int m,n,l,T;

inline bool check(int x,int xx){
	if(x>=0&&x<xx)return true;
	return false;
}

void bfs(int x,int y,int z){
	vis[x][y][z]=1;
	queue<AA> q;
	q.push(AA(x,y,z));
	int s=0;
	while(!q.empty()){
		AA t=q.front();
		s++;
		q.pop();
		for(int i=0;i<6;i++){
			int xx=t.a+fx[i];
			int yy=t.b+fy[i];
			int zz=t.c+fz[i];
			if(!check(xx,m)||!check(yy,n)||!check(zz,l))
			continue;
			if(!vis[xx][yy][zz]&&mp[xx][yy][zz]){
				q.push(AA(xx,yy,zz));vis[xx][yy][zz]=1;
			}
		}
	}
	if(s>=T)ans+=s;
}

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",&mp[i][j][k]);
	memset(vis,0,sizeof(vis)); 
	ans=0;
	for(int k=0;k<l;k++)
	for(int i=0;i<m;i++)
	for(int j=0;j<n;j++)
	if(!vis[i][j][k]&&mp[i][j][k])
	bfs(i,j,k);
	printf("%d\n",ans);
}

代码解读void bfs() { while (!q.empty()) { Node cur = q.top(); q.pop(); if (cur.box_x == end_x && cur.box_y == end_y) { best = cur.step; flag = true; break; } else for (int i = 0; i < 4; i++) { flag1 = false; memset(visit2, 0, sizeof(visit2)); int x = cur.box_x + dx[i]; int y = cur.box_y + dy[i]; if (x<1 || y<1 || x>n || y>m || board[x][y] == 1) continue; Node next; next.box_x = x; next.box_y = y; next.people_x = cur.box_x; next.people_y = cur.box_y; next.step = cur.step + 1; if (i == 0) if (cur.box_y - 1 > 0) if (board[cur.box_x][cur.box_y - 1] != 'S' && bfs2(cur.box_x, cur.box_y - 1, cur.box_x, cur.box_y, cur.people_x, cur.people_y) && !visit[x][y][cur.box_x][cur.box_y - 1]) { visit[x][y][cur.box_x][cur.box_y - 1] = 1; q.push(next); } if (i == 1) if (cur.box_y + 1 <= m) if (board[cur.box_x][cur.box_y + 1] != 'S' && bfs2(cur.box_x, cur.box_y + 1, cur.box_x, cur.box_y, cur.people_x, cur.people_y) && !visit[x][y][cur.box_x][cur.box_y + 1]) { visit[x][y][cur.box_x][cur.box_y + 1] = 1; q.push(next); } if (i == 2) if (cur.box_x - 1 > 0) if (board[cur.box_x - 1][cur.box_y] != 'S' && bfs2(cur.box_x - 1, cur.box_y, cur.box_x, cur.box_y, cur.people_x, cur.people_y) && !visit[x][y][cur.box_x - 1][cur.box_y]) { visit[x][y][cur.box_x - 1][cur.box_y] = 1; q.push(next); } if (i == 3) if (cur.box_x + 1 <= n) if (board[cur.box_x + 1][cur.box_y] != 'S' && bfs2(cur.box_x + 1, cur.box_y, cur.box_x, cur.box_y, cur.people_x, cur.people_y) && !visit[x][y][cur.box_x + 1][cur.box_y]) { visit[x][y][cur.box_x + 1][cur.box_y] = 1; q.push(next); } } } }
最新发布
07-14
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值