PTA甲级 1091 Acute Stroke (30分) -BFS

强烈推荐,刷PTA的朋友都认识一下柳神–PTA解法大佬

本文由参考于柳神博客写成

柳神的CSDN博客,这个可以搜索文章

柳神的个人博客,这个没有广告,但是不能搜索

还有就是非常非常有用的 算法笔记 全名是

算法笔记  上级训练实战指南		//这本都是PTA的题解
算法笔记

PS 今天也要加油鸭

在这里插入图片描述

题目原文

One important factor to identify acute stroke (急性脑卒中) is the volume of the stroke core. Given the results of image analysis in which the core regions are identified in each MRI slice, your job is to calculate the volume of the stroke core.

Input Specification:

Each input file contains one test case. For each case, the first line contains 4 positive integers: M, N, L and T, where M and N are the sizes of each slice (i.e. pixels of a slice are in an M×N matrix, and the maximum resolution is 1286 by 128); L (≤60) is the number of slices of a brain; and T is the integer threshold (i.e. if the volume of a connected core is less than T, then that core must not be counted).

Then L slices are given. Each slice is represented by an M×N matrix of 0’s and 1’s, where 1 represents a pixel of stroke, and 0 means normal. Since the thickness of a slice is a constant, we only have to count the number of 1’s to obtain the volume. However, there might be several separated core regions in a brain, and only those with their volumes no less than T are counted. Two pixels are connected and hence belong to the same region if they share a common side, as shown by Figure 1 where all the 6 red pixels are connected to the blue one.

figstroke.jpg

Figure 1

Output Specification:

For each case, output in a line the total volume of the stroke core.

Sample Input:

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
1 0 0 0

Sample Output:

26

生词如下:

这题的英文是一点都不懂系列,讲的啥都不知道

volume 量,卷,大

matrix 矩阵

slices 切片,图片,片段,割图区域

Then L slices are given. Each slice is represented by an M×N matrix of 0’s and 1’s, where 1 represents a pixel of stroke

这L个切片和m*n连接在一起.切片的个数只能是0 和1

核心句子

Then L slices are given 然后给了L个切片

题目大意:

就是给你一个三维的矩阵,要你求出一个块 .块的定义就是:

如果矩阵中有若干个一是相邻的, (不必两两相邻) 那么称这些1构成了一个 “块” 求给定的矩阵中 “块” 的个数

就是一个点和一个点是相邻的.

然后要你输出符合要求的块数.

思路如下:

套算法笔记的模板

不过这个是三维的了.所以我们的和增量有关的数据都需要改变.

int X[6] = { 0,0,0,0,-1,1 };
int Y[6] = { 0,0,1,-1,0,0 };
int Z[6] = { 1,-1,0,0,0,0 };

其他的都差不多,就是改个增量,然后就是他输入时候的顺序我没有看懂.

我copy的笔记.如果输入的那个看懂了

这题柳神的代码也基本都是模板,就不放上来了.

代码如下:

#include<iostream>
#include<queue>
using namespace std;
struct node {
	int x, y, z;		//位置 x,y,z
	
}Node;
int N,M,L,T;							//矩阵的大小
int matrix[1290][130][61];				//默认的最大矩阵
bool inq[1290][130][61] = { false };
int X[6] = { 0,0,0,0,1,-1 };
int Y[6] = { 0,0,1,-1,0,0 };
int Z[6] = { 1,-1,0,0,0,0 };

bool judge(int x, int y, int z) {		//判断坐标(x,y,z)合理不合理
	//越界返回false
	if (x >= N || x < 0 || y >= M || y < 0||z>=L||z<0)	return false;
	//当前位置为0,或(x,y)已入过对,返回false
	if (matrix[x][y][z] == 0 || inq[x][y][z] == true)	return false;
	//以上条件都不满足,返回true
	return true;
}
int BFS(int x, int y, int z) {
	queue<node> Q;							//定义队列
	Node.x = x, Node.y = y, Node.z = z;	//当前队列的坐标为(x,y,z)
	Q.push(Node);							//将结点Node入列
	inq[x][y][z] = true;					//设置(x,y,z)已入队列
	int tot = 0;
	while (!Q.empty()) {
		node top = Q.front();				//取队首元素
		Q.pop();	
		tot++;
		for (int i = 0; i < 6; ++i) {
			int newX = top.x + X[i];
			int newY = top.y + Y[i];
			int newZ = top.z + Z[i];
			if (judge(newX, newY, newZ)) {
				//设置Node的坐标为(newX,newY,newZ)
				Node.x = newX, Node.y = newY, Node.z = newZ;
				Q.push(Node);
				inq[newX][newY][newZ]=true;
			}
		}
	}
	if (tot >= T)	return tot;
	else return 0;
}
int main(void) {
	scanf("%d%d%d%d", &N, &M, &L, &T);
	for (int z = 0; z < L; ++z) {
		for (int x = 0; x < N; ++x) {
			for (int y = 0; y < M; ++y)
				scanf("%d", &matrix[x][y][z]);
		}
	}
	int ans = 0;
	for (int z = 0; z < L; ++z) {
		for (int x = 0; x < N; ++x) {
			for (int y = 0; y < M; ++y)
				if (matrix[x][y][z] == 1 && inq[x][y][z]==false) {
					ans += BFS(x, y, z);
				}
		}
	}
	printf("%d\n", ans);
	return 0;
}

如果这篇文章对你有张帮助的话,可以用你高贵的小手给我点一个免费的赞吗

相信我,你也能变成光.

在这里插入图片描述

如果你有任何建议,或者是发现了我的错误,欢迎评论留言指出.

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值