PAT-1091 HAcute Stroke (30)

题目大意:给定L个薄片(slice)依次叠在一起(如下图,7个蓝点相邻),所定义的相连是三维上的相邻,相邻个数不小于T个时,便认为是结果的一部分(the part volume of the stroke core)

题意示意图

该问题是一个三维连通块问题。BFS可以很好地解决该问题,无难点,代码如下。至于为什么不用DFS,本人想的办法要多引入一些标记,遂没写。

题目链接:https://www.patest.cn/contests/pat-a-practise/1091

#include <iostream>
#include <algorithm>
#include <vector>
#include <map>
#include <stack>
#include <queue>
#include <cmath>
using namespace std;
const int XL =  1300;
const int YL = 130;
const int ZL = 61;
int N,M,L,T;
int G[XL][YL][ZL];
 
int dx[6]={1,-1,0,0,0,0};
int dy[6]={0,0,1,-1,0,0};
int dz[6]={0,0,0,0,1,-1};
//和queue配套使用 
class Point{
	public:
		int px,py,pz;
};
//越出边界否 
bool isInside(int x,int y,int z)
{
	if((x>=0 && x<N) && (y>=0&&y<M) && (z>=0&&z<L))
		return true;
	return false;
}

int BFS(int x,int y,int z)
{
	G[x][y][z] = 0;
	int count1 = 1;
	
	queue<Point> que;
	Point point;
	point.px = x;point.py = y;point.pz = z;
	que.push(point);
	
	int x1,y1,z1;
	while(!que.empty())
	{
		//cout<<"*"; 
		x=que.front().px;
		y=que.front().py;
		z=que.front().pz;
		que.pop();
		for(int i=0;i<6;++i)
		{
			x1 = x+dx[i];
			y1 = y+dy[i];
			z1 = z+dz[i];
			if(isInside(x1,y1,z1) && G[x1][y1][z1])
			{
				point.px = x1;point.py = y1;point.pz = z1;
				que.push(point);	
				G[x1][y1][z1] = 0;
				++count1;			
			}
		}
	}
	return count1;
}

int main(int argc, char** argv) {
	int count1,sum=0;
	cin >> N >> M >> L >> T;
	for(int k=0;k<L;++k)
		for(int i=0;i<N;++i)
			for(int j=0;j<M;++j)
				cin >> G[i][j][k];
	
	for(int k=0;k<L;++k)
		for(int i=0;i<N;++i)
			for(int j=0;j<M;++j)
				if(G[i][j][k])
				{
					count1 = BFS(i,j,k);
					if(count1 >= T)
						sum += count1;		
				}
	cout << sum << endl;
	return 0;
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值