PAT A1091

方法:BFS。

#include <iostream>
#include <queue>
using namespace std;
int info[62][1290][130]={0};
int row,column,slice,threshold;
struct position{
	int x,y,z;
};
bool judge(int x,int y,int z){
	if(x<0||x>=slice||y<0||y>=row||z<0||z>=column) return false;  //
	if(info[x][y][z]==0) return false;
	info[x][y][z]=0;  //判定正确后,即修改,作用相当于修改vis数组值 
	return true;
}
int BFS(position start){
	queue<position> q;
	q.push(start);
	int a[6]={1,-1,0,0,0,0},b[6]={0,0,1,-1,0,0},c[6]={0,0,0,0,1,-1},count=0;
	while(!q.empty()){
		position t=q.front();
		q.pop();
		count++;
		for(int i=0;i<6;i++){
			if(judge(t.x+a[i],t.y+b[i],t.z+c[i])){
				position temp;
				temp.x=t.x+a[i];
				temp.y=t.y+b[i];
				temp.z=t.z+c[i];
				q.push(temp);
			}
		}
	}
	return count;
}
int BFStrav(){
	int sum=0;
	for(int i=0;i<slice;i++){
		for(int j=0;j<row;j++){
			for(int k=0;k<column;k++){
				if(judge(i,j,k)){
					position start;
					start.x=i;
					start.y=j;
					start.z=k;
					int countnow=BFS(start);
					if(countnow>=threshold) sum+=countnow;
				}
			}
		}
	}
	return sum;
}
int main(){
	scanf("%d%d%d%d",&row,&column,&slice,&threshold);
	for(int i=0;i<slice;i++)
		for(int j=0;j<row;j++)
			for(int k=0;k<column;k++)
				scanf("%d",&info[i][j][k]);
	printf("%d",BFStrav());
	return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值