L3-004. 肿瘤诊断(三维数组的BFS)

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
// 这题就是一个三维数组的BFS,其实也可以用DFS,但是数据很大,容易爆栈,所以还是乖乖用BFS吧,还有就是cnt用long存储,免得溢出了
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
struct point {
	int x;
	int y;
	int z;
	point(int x, int y, int z) {
		this->x = x;
		this->y = y;
		this->z = z;
	}
};

int M, N, L, T;

long bfs(point po, vector< vector< vector<int> > >& map, vector< vector< vector<int> > >& visit) {
	queue<point> q;
	q.push(po);
	visit[po.x][po.y][po.z] = 1;
	long cnt = 0;
	while (!q.empty()) {
		point p = q.front();
		q.pop();
		cnt++;
		if (p.z + 1 < L  && map[p.x][p.y][p.z + 1] == 1 && visit[p.x][p.y][p.z + 1] == 0) {
			q.push(point(p.x, p.y, p.z + 1));
			visit[p.x][p.y][p.z + 1] = 1;
		}
		if (p.z - 1 >= 0 && map[p.x][p.y][p.z - 1] == 1 && visit[p.x][p.y][p.z - 1] == 0) {
			q.push(point(p.x, p.y, p.z - 1));
			visit[p.x][p.y][p.z - 1] = 1;
		}
		if (p.x + 1 < M && map[p.x + 1][p.y][p.z] == 1 && visit[p.x + 1][p.y][p.z] == 0) {
			q.push(point(p.x + 1, p.y, p.z));
			visit[p.x + 1][p.y][p.z] = 1;
		}
		if (p.x - 1 >= 0 && map[p.x - 1][p.y][p.z] == 1 && visit[p.x - 1][p.y][p.z] == 0) {
			q.push(point(p.x - 1, p.y, p.z));
			visit[p.x - 1][p.y][p.z] = 1;
		}
		if (p.y + 1 < N && map[p.x][p.y + 1][p.z] == 1 && visit[p.x][p.y + 1][p.z] == 0) {
			q.push(point(p.x, p.y + 1, p.z));
			visit[p.x][p.y + 1][p.z] = 1;
		}
		if (p.y - 1 >= 0 && map[p.x][p.y - 1][p.z] == 1 && visit[p.x][p.y - 1][p.z] == 0) {
			q.push(point(p.x, p.y - 1, p.z));
			visit[p.x][p.y - 1][p.z] = 1;
		}
	}
	return cnt >= T ? cnt : 0;
}


int main() {
	//freopen("1.txt", "r", stdin);
	cin >> M >> N >> L >> T;
	vector< vector< vector<int> > > map(M, vector< vector<int> >(N, vector<int>(L, 0)));
	vector< vector< vector<int> > > visit(M, vector< vector<int> >(N, vector<int>(L, 0)));
	for (int i = 0; i < L; i++) {
		for (int j = 0; j < M; j++) {
			for (int k = 0; k < N; k++) {
				cin >> map[j][k][i];
			}
		}
	}

	long cnt = 0;
	for (int z = 0; z < L; z++) {
		for (int x = 0; x < M; x++) {
			for (int y = 0; y < N; y++) {
				if (map[x][y][z] == 1 && visit[x][y][z] == 0) {
					cnt += bfs(point(x, y, z), map, visit);
				}
			}
		}
	}

	cout << cnt;
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以使用广度优先搜索(BFS)算法来解决二维数组的寻路问题。下面是一个简单的示例代码,用于找到从起点到终点的最短路径: ```python from collections import deque def bfs(matrix, start, end): # 获取二维数组的行数和列数 rows = len(matrix) cols = len(matrix[0]) # 定义四个方向的偏移量,上、下、左、右 directions = [(-1, 0), (1, 0), (0, -1), (0, 1)] # 使用队列来保存待访问的节点 queue = deque() queue.append(start) # 使用visited集合来记录已经访问过的节点 visited = set() visited.add(start) # 使用distances字典来记录每个节点到起点的距离 distances = {} distances[start] = 0 while queue: node = queue.popleft() if node == end: # 找到终点,返回最短路径长度 return distances[node] x, y = node for dx, dy in directions: new_x, new_y = x + dx, y + dy # 检查新的节点是否越界或已经访问过 if 0 <= new_x < rows and 0 <= new_y < cols and (new_x, new_y) not in visited and matrix[new_x][new_y] != 0: # 更新新节点的距离,并添加到队列和visited集合中 new_node = (new_x, new_y) queue.append(new_node) visited.add(new_node) distances[new_node] = distances[node] + 1 # 没有找到终点,返回-1表示无法到达 return -1 ``` 你可以根据实际情况调用这个函数,并传入相应的参数,例如起点坐标和终点坐标。注意,这个示例代码假设二维数组中的非零元素表示可以通过的路径,零表示障碍物。你可以根据实际需求进行修改。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值