算法提高课第二章Flood Fill

news/2024/7/15 19:27:07 标签: 算法, c++, 图论

对于染色问题,可以采用BFS求解

BFS的优点是:每个点只会遍历一次,第一次遇到终点时即可求得最短路

1097. 池塘计数

#include <iostream>
#include <cstring>
#include <queue>
#include <algorithm>
using namespace std;
char a[1010][1010];
bool st[1010][1010];
int bx[8] = {1,-1,0,0,1,1,-1,-1};
int by[8] = {0,0,1,-1,1,-1,1,-1};
int ans = 0;
int n,m;
void bfs(int x, int y)
{
    if(a[x][y] == '.')return;
    queue<pair<int,int>> q;
    q.push({x,y});
    while(!q.empty())
    {
        int nowx = q.front().first;
        int nowy = q.front().second;
        q.pop();
        for(int i = 0; i < 8; i ++ )
        {
            int nx = nowx + bx[i];
            int ny = nowy + by[i];
            if(nx < 1 || ny < 1 || nx > n || ny > m)continue;
            if(a[nx][ny] == '.')continue;
            if(st[nx][ny])continue;
            q.push({nx, ny});
            st[nx][ny] = true;
        }
    }
    ans ++;
}
int main()
{
  
    cin >> n >> m;
    for(int i = 1; i <= n; i ++ )
    {
        for(int  j = 1; j <= m; j ++ )
        {
            cin >> a[i][j];
        }
    }
    for(int i = 1; i <= n; i ++ )
    for (int j = 1; j <= m; j ++ )
    {
        if(!st[i][j])
            bfs(i,j);
    }
    cout << ans;
}

1098. 城堡问题

//     2
//   1   4
//     8
#include <iostream>
#include <cstring>
#include <algorithm>
#include <queue>
#define fi first
#define se second
#define PII pair<int,int>
using namespace std;
int n, m;
int cnt = 0, area = 0;
int a[100][100];
bool st[100][100];
int bx[4] = {0,-1,0,1};
int by[4] = {-1,0,1,0};
void bfs(int x, int y)
{
    int num = 0;
    queue<PII>q;
    q.push({x,y});
    num ++;
    st[x][y] = true;
    while(!q.empty())
    {
        auto t = q.front();
        q.pop();
        int nowx = t.fi, nowy = t.se;
        for(int i = 0; i < 4; i ++ )
        if(!((a[nowx][nowy] >> i)&1 ))
        {
            
            int nx = nowx + bx[i], ny = nowy + by[i];
            if(nx < 1 || ny < 1 || nx > n || ny > m)continue;
            if(st[nx][ny])continue;
            q.push({nx,ny});
            num ++;
            st[nx][ny] = true;
        }
    }
    cnt ++;
    area = max(area, num);
}
int main()
{
    cin >> n >> m;
    for(int i = 1; i <= n; i ++ )
    for(int j = 1; j <= m; j ++ )
        cin >> a[i][j];
    for(int i = 1; i <= n; i ++ )
    for(int j = 1; j <= m; j ++)
    {
        if(!st[i][j])
            bfs(i, j);
    }
    cout << cnt << endl << area;
}

1106. 山峰和山谷

#include <iostream>
#include <cstring>
#include <algorithm>
#include <queue>
#define fi first
#define se second
#define PII pair<int,int>
using namespace std;
int n;
int bx[8] = {1,1,1,-1,-1,-1,0,0};
int by[8] = {0,1,-1,0,1,-1,1,-1};
int a[1010][1010];
bool st[1010][1010];
int cnt1 = 0, cnt2 = 0;
void bfs(int x, int y)
{
    bool flag[2] = {0, 0};
    queue<PII> q;
    q.push({x,y});
    st[x][y] = true;
    while(!q.empty())
    {
        auto t = q.front();
        int nowx = t.fi, nowy = t.se;
        q.pop();
        for(int i = 0; i < 8; i ++ )
        {
            int nx = nowx + bx[i], ny = nowy + by[i];
            if(nx < 1 || ny < 1 || nx > n || ny > n)continue;
            if(a[nowx][nowy] < a[nx][ny])
                flag[0] = true;
            else if(a[nowx][nowy] > a[nx][ny])
                flag[1] = true;
            else if(!st[nx][ny])
            {
                q.push({nx, ny});
                st[nx][ny] = true;
            }
         }
    }
    if(flag[0]&&!flag[1])cnt1++;
    if(!flag[0]&&flag[1])cnt2++;
    if(!flag[0]&&!flag[1]){
        cnt1 = 1;
        cnt2 = 1;
    }
}
int main()
{
    cin >> n;
    for(int i = 1; i <= n; i ++ )
    for(int j = 1; j <= n; j ++ )
    {
        cin >> a[i][j];
    }
    for(int i = 1; i <= n; i ++ )
    for(int j = 1; j <= n; j ++ )
    {
        if(!st[i][j])
            bfs(i,j);
    }
    cout << cnt2 << " " << cnt1;    
}

http://www.niftyadmin.cn/n/864748.html

相关文章

算法提高课第二章最短路模型

BFS第一次扩展到终点时即可求得最短路 AcWing 1076. 迷宫问题 #include <iostream> #include <cstring> #include <algorithm> #include <queue> using namespace std; int bx[4] {0,0,1,-1}; int by[4] {1,-1,0,0}; int n; struct node {int px;in…

系统架构设计笔记(58)—— 嵌入式系统概论

嵌入式系统是一种以应用为中心&#xff0c;以计算机技术为基础&#xff0c;可以适应不同应用对功能 、 可靠性 、 成本 、 体积 、 功耗等方面的要求&#xff0c;集可配置&#xff0c;可裁减的软 、 硬件于一体的专用计算机系统。它具有很强的灵活性&#xff0c;主要由嵌入式硬…

算法提高课第二章多源BFS

有这样一个情景&#xff0c;对于很多源点&#xff0c;求得离他最近的目标的最短路径&#xff0c;称为多源最短路。这时可以创建一个虚拟源点&#xff0c;以边权为0连接所有的源点&#xff0c;这样就把多源最短路转化为了单源最短路 AcWing 173. 矩阵距离 #include <iostrea…

说说 Python 的生成器表达式

列表推导与生成器表达式都可以用于初始化元组、数组或其他类型的序列。但列表推导需要先建立一个完整的列表&#xff0c;然后再把这 个列表传递到某个构造函数。而生成器表达式会逐个产出元素&#xff0c;这样显然能够节省内存。 列表推导写法&#xff1a; codes [ord(symbo…

算法提高课第二章最小步数模型

使用BFS的不一定是在地图上移动&#xff0c;关键点在于状态的扩展 1107. 魔板 #include <iostream> #include <cstring> #include <algorithm> #include <queue> #include <map> #define x first #define y second #define ST pair<string, …

算法提高课第二章双端队列广搜

什么时候会用到双端队列呢&#xff0c;BFS可以看做是一种特殊边权全为1的迪杰斯塔拉算法&#xff0c;BFS的队列满足两端性和单调性&#xff0c;这样就保证了它求解最短路的准确性。当边权为0和1时&#xff0c;可以使用双端队列&#xff0c;0放队头&#xff0c;1放队尾&#xff…

说说 Activiti 中的用户与组的概念

Activiti 中的用户与组用于界定任务的候选者与办理者。组可以理解为角色&#xff0c;属于某个组的用户&#xff0c;就可以作为某个任务的候选者或者办理者。 我们还可以通过 Activiti 的 API 来创建、查询或删除某个用户或者某个组。Activiti 还提供了建立关系的 API 用于绑定…

算法提高第二章课双向广搜

在BFS搜索的过程中&#xff0c;状态的扩展是指数级增长的&#xff0c;为了减少状态数&#xff0c;我们可以从起点和终点双向BFS搜索&#xff0c;且在一个方向的状态数少的情况下多去搜这个方向。在两个方向同时搜到某个状态数结束 注意&#xff0c;不是每个方向一个点一个点的去…