1091. Acute Stroke (30)--BFS数多少块儿

#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <cmath>

#include <string>
#include <cstring>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <stack>

using namespace std;

struct node
{
    int x,y,z;
};
int N,M,L,T;
int g[2000][200][100]={0};
int book[2000][200][100]={0};
int X[6]={1,-1,0,0,0,0};
int Y[6]={0,0,1,-1,0,0};
int Z[6]={0,0,0,0,1,-1};

bool judge(node a)
{
    int x=a.x;
    int y=a.y;
    int z=a.z;

    if( (x<0||y<0||z<0)||(x>=M||y>=N||z>=L) )//出界
        return 0;
    if(g[x][y][z]==0)//点是0
        return 0;
    if(book[x][y][z]==1)//已被访问过
        return 0;

    return 1;
}

int bfs(int x,int y,int z)
{
    int cou=0;

    book[x][y][z]=1;//原点先标记
    cou++;

    node tnode;//搜索的元素
    tnode.x=x;  tnode.y=y;  tnode.z=z;
    queue<node> q;
    q.push(tnode);
    while(!q.empty())
    {
        for(int i=0;i<6;i++)
        {
            int nx=q.front().x+X[i];
            int ny=q.front().y+Y[i];
            int nz=q.front().z+Z[i];
            node tnode2;//周围的node
            tnode2.x=nx;  tnode2.y=ny;  tnode2.z=nz;

            if(judge(tnode2)==1)
            {
                q.push(tnode2);
                book[nx][ny][nz]=1;
                cou++;
            }
        }

        q.pop();
    }

    return cou;
}
int main()
{
   // freopen("in.txt","r",stdin);

    scanf("%d %d %d %d",&M,&N,&L,&T);
    for(int k=0;k<L;k++)
    {
        for(int i=0;i<M;i++)
        {
            for(int j=0;j<N;j++)
            {
                scanf("%d",&g[i][j][k]);
            }
        }
    }

    int ans=0;
    for(int k=0;k<L;k++)
    {
        for(int i=0;i<M;i++)
        {
            for(int j=0;j<N;j++)
            {
                if(g[i][j][k]==1&&book[i][j][k]==0)
                {
                    int cou=bfs(i,j,k);//对每个点进行搜索
                    if(cou>=T)
                    {
                        ans+=cou;
                    }

                }
            }
        }
    }

    printf("%d",ans);



    return 0;
}





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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值