浙大Pat | 浙大pat 牛客网甲级 1004 Acute Stroke (30) BFS

题目描述

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



输入描述:

Each input file contains one test case.  For each case, the first line contains 4positive 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 by N matrix, and the maximum resolution is1286 by 128); L (<=60) is the number of slices of a brain; and T is theinteger threshold (i.e. if the volume of a connected core is less than T, thenthat core must not be counted).

Then L slices are given.  Each slice isrepresented by an M by N matrix of 0's and 1's, where 1 represents a pixel ofstroke, and 0 means normal.  Since thethickness of a slice is a constant, we only have to count the number of 1's toobtain the volume.  However, there mightbe several separated core regions in a brain, and only those with their volumesno less than T are counted. Two pixels are "connected" and hencebelong to the same region if they share a common side, as shown by Figure 1where all the 6 red pixels are connected to the blue one.

 


Figure 1




输出描述:

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



输入例子:

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,设计到三维数组的使用技巧,首先使用一个三维数组存储所有的值,然后遍历一遍这个三维数组,对于每个为1的值,以这个值为基准进行BFS搜索,通过搜索得到的体积,来得到最终的体积

这一题有一个隐藏条件,就是对于边缘的1,也需要算到最终的体积之和里面去,如果不看测试用例我还发现不了这个隐藏条件

使用BFS的时候需要注意,有三个限制条件

1)       不能超过立方体的边缘

2)       一定要是1

3)       Vst一定要是false

以后在使用BFS的时候一定要记得不要把条件弄掉了

#include <iostream>
#include <queue>
using namespace std;
int M, N, L, T;
bool theCore[62][1288][131];
bool vst[62][1288][131] = { false };
int theDongDongCount = 0;
int theX[6] = { 1,-1,0,0,0,0 };
int theY[6] = { 0,0,1,-1,0,0 };
int theZ[6] = { 0,0,0,0,1,-1 };
struct node
{
	int z, x, y;
	node(int a=0,int b=0,int c=0) : z(a),x(b),y(c) {}
};
void BFS(int z, int x, int y)
{
	int theDongDongtmp = 1;
	node tmp,p;
	vst[z][x][y] = true;
	queue<node> theLine;
	theLine.push(node(z, x, y));
	while (!theLine.empty())
	{
		tmp = theLine.front();
		theLine.pop();
		for (int i = 0; i < 6; i++)
		{
			p.x = tmp.x + theX[i];
			p.y = tmp.y + theY[i];
			p.z = tmp.z + theZ[i];
			if (p.x < 0 || p.x >= M || p.y < 0 || p.y >= N || p.z < 0 || p.z >= L) continue;
			if (theCore[p.z][p.x][p.y]==0||vst[p.z][p.x][p.y]) continue;
			vst[p.z][p.x][p.y] = true;
			theLine.push(p);
			theDongDongtmp++;
		}
	}
	if (theDongDongtmp < T) return;
	else theDongDongCount += theDongDongtmp;
}

int main()
{
	int tmp;
	cin >> M >> N >> L >> T;
	for(int i=0;i<L;i++)
		for(int j=0;j<M;j++)
			for (int k = 0; k <N; k++)
			{
				cin >> tmp;
				theCore[i][j][k] = tmp;
			}
	for (int i = 0; i<L; i++)
		for (int j = 0; j<M; j++)
			for (int k = 0; k < N; k++)
			{
				if (theCore[i][j][k] == 1 && !vst[i][j][k]) 
					BFS(i, j, k);
			}
	cout << theDongDongCount;
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值