PAT- 1901 Acute Stroke (30) dfs or bfs

题意:三维图 找符合条件的1的数目 条件:如果由1组成的联通快的1的数目>=t 那么这个联通块的所有1都符合条件。

看图就知道如果 前后左右上下有相邻的1:这两个1联通

dfs

#include <cstdio>
#include <algorithm>
#include <math.h>
#include <string.h>
#include <vector>
#include <map>
#include <iostream>
#define ll long long
#define INF 0x3f3f3f
using namespace std;
const int N=100000+500;
int s[65][1290][130];
int vis[65][1290][130];
int dx[6]={0,0,1,-1,0,0};
int dy[6]={0,0,0,0,1,-1};
int dh[6]={1,-1,0,0,0,0};
int n,m,l,cnt;
void dfs(int h,int x,int y)
{
    int xx,yy,hh,i;
    cnt++;
    vis[h][x][y]=1;
    for(i=0;i<6;i++)
    {
        xx=dx[i]+x;yy=dy[i]+y;hh=dh[i]+h;
        if(hh<0 || hh>=l || xx<0 ||xx>=n ||yy<0 ||yy>=m) continue;
        if(vis[hh][xx][yy]==1 || s[hh][xx][yy]==0) continue;
        dfs(hh,xx,yy);
    }
}
int main()
{
    int t,i,j,k,ans;
    ans=0;
    scanf("%d%d%d%d",&n,&m,&l,&t);
    for(i=0;i<l;i++)
        for(j=0;j<n;j++)
            for(k=0;k<m;k++)
                scanf("%d",&s[i][j][k]);

    for(i=0;i<l;i++)
    {
        for(j=0;j<n;j++)
        {
            for(k=0;k<m;k++)
            {
                if(!vis[i][j][k] && s[i][j][k]==1)
                {
                    cnt=0;
                    dfs(i,j,k);
                    if(cnt>=t) ans+=cnt;
                }
            }
        }
    }
    printf("%d\n",ans);
}

bfs

#include <cstdio>
#include <algorithm>
#include <math.h>
#include <string.h>
#include <queue>
#include <iostream>
#define ll long long
#define INF 0x3f3f3f
using namespace std;
const int N=100000+500;
int s[65][1290][130];
int vis[65][1290][130];
int dx[6]={0,0,0,0,-1,1};
int dy[6]={0,0,-1,1,0,0};
int dh[6]={1,-1,0,0,0,0};
int n,m,l,cnt;
struct node
{
    int h,x,y;
}p,now,nextt;
queue<node> q;
void bfs(int h,int x,int y)
{
    int i,hh,xx,yy,nowh,nowx,nowy;
    now.x=x;now.y=y;now.h=h;
    vis[h][x][y]=1;
    q.push(now);
    cnt=1;
    while(!q.empty())
    {
        now=q.front();
        q.pop();
        nowh=now.h,nowx=now.x;nowy=now.y;
        for(i=0;i<6;i++)
        {
            hh=nowh+dh[i]; xx=nowx+dx[i]; yy=nowy+dy[i];
            if(hh<0 || hh>=l ||xx<0 ||xx>=n ||yy<0 ||yy>=m) continue;
            if(vis[hh][xx][yy] || s[hh][xx][yy]==0) continue;
            nextt.h=hh;nextt.x=xx;nextt.y=yy;
            q.push(nextt);
            vis[hh][xx][yy]=1;
            cnt++;
        }
    }
}
int main()
{
    int t,i,j,k,ans;
    ans=0;
    scanf("%d%d%d%d",&n,&m,&l,&t);
    for(i=0;i<l;i++)
        for(j=0;j<n;j++)
            for(k=0;k<m;k++)
                scanf("%d",&s[i][j][k]);
 
    for(i=0;i<l;i++)
    {
        for(j=0;j<n;j++)
        {
            for(k=0;k<m;k++)
            {
                if(!vis[i][j][k] && s[i][j][k]==1)
                {
                    bfs(i,j,k);
                    if(cnt>=t) ans+=cnt;
                }
            }
        }
    }
    printf("%d\n",ans);
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值