【搜索】Flood Fill 和 最短路模型


Flood Fill 

洪水填充(Flood fill)算法:从一个起始节点开始把附近与其连通的节点提取出或填充成不同颜色颜色,直到封闭区域内的所有节点都被处理过为止,是从一个区域中提取若干个连通的点与其他相邻区域区分开(或分别染成不同颜色)的经典算法

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

如果采用DFS,可能会出现“爆栈”的危险,所以我们一般采用BFS来实现这个算法


AcWing 1097. 池塘计数 

输入样例:

10 12
W........WW.
.WWW.....WWW
....WW...WW.
.........WW.
.........W..
..W......W..
.W.W.....WW.
W.W.W.....W.
.W.W......W.
..W.......W.

输出样例:

3

 


 

#include <iostream>
#include <algorithm>
#include <cstring>

using namespace std;

#define x first
#define y second

typedef pair<int, int> PII;

int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};

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

int n, m;
char g[N][N];
PII q[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)
    {
        auto 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(g[i][j] == '.' || st[i][j]) continue;
                
                q[++ tt ] = {i, j};
                st[i][j] = true;
            }
    }
}

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

AcWing 1098. 城堡问题 

 

输入样例:

4 7 
11 6 11 6 3 10 6 
7 9 6 13 5 15 5 
1 10 12 7 13 7 5 
13 11 10 8 10 12 13 

输出样例:

5
9

这题的难点在于初始的建图

需要根据题目所给的条件,按照二进制的格式来进行建图 


 

#include <iostream>
#include <algorithm>
#include <cstring>

using namespace std;

#define x first
#define y second

typedef pair<int, int> PII;

int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};

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

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

int bfs(int sx, int sy)
{
    int dx[] = {0, -1, 0, 1}, dy[] = {-1, 0, 1, 0};
    
    int hh = 0, tt = 0;
    int area = 0;
    
    q[0] = {sx, sy};
    st[sx][sy] = true;
    
    while(hh <= tt)
    {
        auto t = q[hh ++ ];
        
        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(st[a][b]) continue;
            if(g[t.x][t.y] >> i & 1) continue;
            
            q[ ++ tt] = {a, b};
            st[a][b] = true;
        }
    }
}

int main()
{
    cin >> n >> m;
    for(int i = 0; i < n; i ++ )
        for(int j = 0; j < m; j ++ )
            cin >> g[i][j];
            
    int cnt = 0, area = 0;
    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;
}

AcWing 1106. 山峰和山谷  

 

输入样例1:

5
8 8 8 7 7
7 7 8 8 7
7 7 7 7 7
7 8 8 7 8
7 8 8 8 8

输出样例1:

2 1

输入样例2:

5
5 7 8 3 1
5 5 7 6 6
6 6 6 2 8
5 7 2 5 8
7 1 0 1 7

输出样例2:

3 3

 


#include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>

using namespace std;

#define x first
#define y second

typedef pair<int, int> PII;

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

int n;
int h[N][N];
PII q[M];
bool st[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()
{
    cin >> 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;
}

最短路模型 

利用宽搜的最短性,可以用宽度优先搜索,在线性的时间复杂度内,找到最短路


AcWing 1076. 迷宫问题 

输入样例:

5
0 1 0 0 0
0 1 0 1 0
0 0 0 0 0
0 1 1 1 0
0 0 0 1 0

输出样例:

0 0
1 0
2 0
2 1
2 2
2 3
2 4
3 4
4 4

(BFS) 
深度优先搜索超时

(n - 1, n - 1) - > (0, 0) 进行广度优先搜索,用数组存储当前节点是从哪个节点过来的


#include <cstring>
#include <iostream>
#include <algorithm>

#define x first
#define y second

using namespace std;

typedef pair<int, int> PII;

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

int n;
int g[N][N];
PII q[M];
PII pre[N][N]; // 记录路径

int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};

void bfs(int sx, int sy)
{
    int hh = 0, tt = 0;
    q[0] = {sx, sy};
    
    memset(pre, -1, sizeof pre);
    pre[sx][sy] = {0, 0};
    while(hh <= tt)
    {
        PII t = q[hh ++ ];
        
        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 >= n) continue;
            if(g[a][b]) continue;
            if(pre[a][b].x != -1) continue;
            
            q[++ tt] = {a, b};
            pre[a][b] = t;
        }
    }
}

int main()
{
    cin >> n;
    
    for(int i = 0; i < n; i ++ )
        for(int j = 0; j < n; j ++ )
            scanf("%d", &g[i][j]);
            
    bfs(n - 1, n - 1);
    
    PII end(0, 0);
    
    while(1)
    {
        printf("%d %d\n", end.x, end.y);
        if(end.x == n - 1 && end.y == n - 1) break;
        end = pre[end.x][end.y];
    }
    
    return 0;
}


  • 9
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 6
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

玄澈_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值