一、Flood Fill --- 联通块块数问题

活动 - AcWing (1097.池塘计数)
题意:找到以W点为起始的连通块

思路:多方向dfs,更新走过的点,统计联通块的数量

#include<bits/stdc++.h>

using namespace std;

#define ll long long
#define PII pair<int, int>
#define PLL pair<ll, ll>

const int N = 1010;
const int M = 2 * N;
const int INF = 0x3f3f3f3f;
const int mod = 1e9 + 7;
const double PI = acos(-1.0);
const double eps = 1e-8;

int n,m;
int cnt;
char mp[N][N];
//int hr[8][2] = {{-1,0},{-1,1},{0,1},{1,1},{1,0},{1,-1},{0,-1},{-1,-1}};
void dfs(int x,int y)
{
    mp[x][y] = '.';
    for(int i = -1; i <= 1; i ++)
        for(int j = -1; j <= 1; j ++)
        {
            int a = x + i;
            int b = y + j;
            if(a < 0 || a >= n || b < 0 || b >= m) continue;
            if(mp[a][b] == '.') continue;
            
            dfs(a,b);
        }   
    
}
void solve()
{
    scanf("%d%d",&n,&m);
    for(int i = 0; i < n; i ++) scanf("%s",mp[i]);
    for(int i = 0; i < n; i ++)
    {
        for(int j = 0; j < m; j ++)
        {
            if(mp[i][j] == 'W')
            {
                 dfs(i,j);
                cnt ++;
            }
        }
    }
    printf("%d\n",cnt);
}
int main()
{
    solve();



    system("pause");
    return 0;
}

 

活动 - AcWing(1098.城堡问题)

题意:找到连通块最大值和个数

思路:多方向bfs,更新走过的点,统计联通块的数量,并且更新最大值

注意 8421 在二进制更新条件下的方向数组要与对应方向一致的问题

#include<bits/stdc++.h>

using namespace std;

#define ll long long
#define PII pair<int, int>
#define PLL pair<ll, ll>

const int N = 1010;
const int M = 2 * N;
const int INF = 0x3f3f3f3f;
const int mod = 1e9 + 7;
const double PI = acos(-1.0);
const double eps = 1e-8;

int n,m;
int cnt;
char mp[N][N];
//int hr[8][2] = {{-1,0},{-1,1},{0,1},{1,1},{1,0},{1,-1},{0,-1},{-1,-1}};
void dfs(int x,int y)
{
    mp[x][y] = '.';
    for(int i = -1; i <= 1; i ++)
        for(int j = -1; j <= 1; j ++)
        {
            int a = x + i;
            int b = y + j;
            if(a < 0 || a >= n || b < 0 || b >= m) continue;
            if(mp[a][b] == '.') continue;
            
            dfs(a,b);
        }   
    
}
void solve()
{
    scanf("%d%d",&n,&m);
    for(int i = 0; i < n; i ++) scanf("%s",mp[i]);
    for(int i = 0; i < n; i ++)
    {
        for(int j = 0; j < m; j ++)
        {
            if(mp[i][j] == 'W')
            {
                 dfs(i,j);
                cnt ++;
            }
        }
    }
    printf("%d\n",cnt);
}
int main()
{
    solve();



    system("pause");
    return 0;
}

活动 - AcWing(1106. 山峰和山谷)

题意:找到八连通的最高的区块山和最矮的区块谷

思路:bfs爆搜即可

#include<bits/stdc++.h>

using namespace std;

#define ll long long
#define PII pair<int, int>
#define PLL pair<ll, ll>

const int N = 1010;
const int M = 2 * N;
const int INF = 0x3f3f3f3f;
const int mod = 1e9 + 7;
const double PI = acos(-1.0);
const double eps = 1e-8;
int n;
int hr[8][2] = {{-1,-1},{-1,0},{-1,1},{0,1},{1,1},{1,0},{1,-1},{0,-1}};
int g[N][N],st[N][N];
void bfs(int x,int y,bool &high, bool &low)
{
    int stand = g[x][y];
    st[x][y] = 1;
    queue<PII>q;
    q.push({x,y});
    while(q.size())
    {
        auto t = q.front();
        q.pop();
        for(int i = 0; i < 8; i ++)
        {
            int a = t.first + hr[i][0];
            int b = t.second + hr[i][1];
            if(a < 0 || a >= n || b < 0 || b >= n) continue;
            if(g[a][b] > stand) low = 1;
            if(g[a][b] < stand) high = 1;
            if(st[a][b]) continue;
            if(g[a][b] == stand) 
            {
                q.push({a,b});
                st[a][b] = 1;
            }
        }   
    }
}
void solve()
{
    scanf("%d",&n);
    for(int i = 0; i < n; i ++)
        for(int j = 0; j < n; j ++)
            scanf("%d",&g[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 high = 0,low = 0;
                bfs(i,j,high,low);
                //cout << i << " " << j << " " << high << " " << low << endl;
                if(!high) valley ++;
                if(!low) peak ++;
            }
    printf("%d %d\n",peak,valley);


}
int main()
{
    solve();



    system("pause");
    return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值