C++算法(Acwing)学习(持续更新中)---Flood Fill

Flood Fill 洪水覆盖问题

BFS中的一个问题

深搜(dfs)可能会爆栈

Flood Fill 算法可以在线性时间复杂度内,找到某个点所在的连通块。

八连通是公共点,四连通是公共边.

小知识----数组模拟队列

Acwing829.模拟队列

//数组模拟队列
//在队尾插入元素,在队头弹出元素
#include<iostream>
#include<cstring>
using namespace std;

const int N=100010;
int q[N],hh,tt=-1;//hh表示队头,tt表示队尾,队尾可初始化为-1;

int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        string s;
        cin>>s;
        if(s=="push")//向队尾插入一个数 x;
        {
            int x;
            cin>>x;
            q[++tt]=x;
        }
        if(s=="empty")//判断队列是否为空;
        {
            if(hh<=tt) puts("NO");
            else puts("YES");
        }
        if(s=="pop") hh++;//从队头弹出一个数;
        if(s=="query") printf("%d\n",q[hh]);//查询队头元素。
    }
    return 0;
}

Acwing1097. 池塘计数-----统计连通块的数量

//此题是八连通
#include<iostream>
#include<cstring>
#include<algorithm>

#define x first
#define y second
//可以用x,y来分别表示pair的第一个和第二个元素

using namespace std;

const int N = 1010,M=N*N;
char str[N][N];

typedef pair<int, int> PII;//二维数组的下标可以用pair来存
PII q[M];//定义一个队列
int n,m;
bool st[N][N];//判重

void bfs(int sx,int sy)
{
    int hh=0,tt=0;
    q[0]={sx,sy};
    st[sx][sy]=true;
    
    while(hh<=tt)
    {
        PII t=q[hh++];//取出队头
        
        for(int i=t.x-1;i<=t.x+1;i++)//八连通类型
        {
            for(int j=t.y-1;j<=t.y+1;j++)
            {
                if(i==t.x&&j==t.y) continue;//不等于八连通中间的那个块
                if(i<0||i>=n||j<0||j>=m) continue;//不越界
                if(str[i][j]=='.'||st[i][j]) continue;
                
                q[++tt]={i,j};
                st[i][j]=true;
            }
        }
    }
}


int main()
{
    scanf("%d%d", &n, &m);
    
    for(int i=0;i<n;i++)
    {
        scanf("%s",str[i]);
    }
    
    int cnt=0;
    
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<m;j++)
        {
            if(str[i][j]=='W'&&!st[i][j])
            {
                bfs(i,j);
                cnt++;
            }
        }
    }
    cout<<cnt;
    return 0;
}

Acwing1098. 城堡问题----统计每个连通块里的个数

#include<iostream>
#include<cstring>

#define x first
#define y second

//难点是怎样来存图
//可考虑2进制来求解,0~15;
//可考虑二进制进行移位操作
//x>>k&1 判断x右移k位是不是1;
//Flood Fill是线形的

using namespace std;

typedef pair<int,int>PII;
const int N=55,M=N*N;

bool st[N][N];
int g[N][N];
PII q[M];
int n,m;

int bfs(int sx,int sy)
{
    int dx[4]={0,-1,0,1},dy[4]={-1,0,1,0};//我们用1表示西墙,2表示北墙,4表示东墙,8表示南墙
    int hh=0,tt=0;
    int area=0;
    q[0]={sx,sy};
    st[sx][sy]=true;
    
    while(hh<=tt)
    {
        PII t=q[hh++];
        area++;
        
        for(int i=0;i<4;i++)
        {
            int a=t.x+dx[i],b=t.y+dy[i];
            if(a<0||a>=n||b<0||b>=m) continue;
            if(g[t.x][t.y]>>i&1) continue;
            if(st[a][b]) continue;
            q[++tt]={a,b};
            st[a][b]=true;
        }
    }
    return area;
}

int main()
{
    cin>>n>>m;
    int cnt=0;
    int area=0;
    
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<m;j++)
        {
            scanf("%d",&g[i][j]);
        }
    }
    
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<m;j++)
        {
            if(!st[i][j])
            {
                area=max(area,bfs(i,j));
                cnt++;
            }
        }
    }
    cout<<cnt<<endl;
    cout<<area<<endl;
    return 0;
}

Acwing1106. 山峰和山谷----每个连通块的类型

//又是八连通类型
//1:03
#include<iostream>
#include<algorithm>
#include<cstring>

#define x first
#define y second

using namespace std;

typedef pair<int,int>PII;

const int N=1010,M=N*N;

int n;
PII q[M];
bool st[N][N];
int h[N][N];

void bfs(int sx,int sy,bool&has_higher,bool&has_lower)
{
    int hh=0,tt=0;
    q[0]={sx,sy};
    st[sx][sy]=true;
    
    while(hh<=tt)
    {
        PII t=q[hh++];
        for(int i=t.x-1;i<=t.x+1;i++)
        {
            for(int j=t.y-1;j<=t.y+1;j++)
            {
                if(i<0||i>=n||j<0||j>=n) continue;
                if(h[i][j]!=h[t.x][t.y]) //山脉的边界
                {
                    if(h[i][j]>h[t.x][t.y]) has_higher=true;
                    else has_lower=true;
                }
                else if(!st[i][j])
                {
                    q[++tt]={i,j};
                    st[i][j]=true;
                }
            }
        }
    }
}

int main()
{
    scanf("%d",&n);
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<n;j++)
        {
            scanf("%d",&h[i][j]);
        }
    }
    int peak=0,valley=0;
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<n;j++)
        {
            if(!st[i][j])
            {
                bool has_higher=false,has_lower=false;
                bfs(i,j,has_higher,has_lower);
                if(!has_higher) peak++;
                if(!has_lower) valley++;
            }
        }
    }
    printf("%d %d\n",peak,valley);
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值