patA1091 bfs

3 篇文章 0 订阅
#include <iostream>
#include <stdio.h>
#include <queue>
using namespace std;
struct node{
int x,y,z;
};
int m,n,l,T;
int map[1300][130][61];
bool book[1300][130][61]={false};
int next1[6][3]={
0,0,-1,
0,0,1,
0,-1,0,
0,1,0,
-1,0,0,
1,0,0
};
int ans=0;
void bfs(int x1,int y1,int z1){
queue<node> Q;
while(!Q.empty())  Q.pop();
node now;
now.x=x1;  now.y=y1;  now.z=z1;
Q.push(now);
book[x1][y1][z1]=true;
int count=0;
while(!Q.empty()){
node nn=Q.front();
Q.pop();
count++;
for(int i=0;i<6;i++){
int nx=nn.x+next1[i][0]; 
int ny=nn.y+next1[i][1]; 
int nz=nn.z+next1[i][2];
if(nx<0 || nx>m-1 || ny<0 || ny>n-1 || nz<0 || nz>l-1)  continue;
if(map[nx][ny][nz]==0)   continue;
if(book[nx][ny][nz]==true)  continue;
book[nx][ny][nz]=true;
node rec;
rec.x=nx;  rec.y=ny;   rec.z=nz;
Q.push(rec); 
}
}
if(count>=T) ans+=count;
}
int main(int argc, char** argv) {
scanf("%d%d%d%d",&m,&n,&l,&T);
for(int i=0;i<l;i++){
for(int j=0;j<m;j++){
for(int z=0;z<n;z++){
scanf("%d",&map[j][z][i]);
}
}
}
for(int i=0;i<l;i++){
for(int j=0;j<m;j++){
for(int z=0;z<n;z++){
if(map[j][z][i]==1 && book[j][z][i]==false){
bfs(j,z,i);
}

}
}
printf("%d\n",ans);
return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值