2.4-染色、洪水填充

· · 个人记录

模板

模板。。

染色怎么写

洪水填充怎么写

怎么记录连通块的格子数量

T1

题意

二维矩阵上,W 和周围八格相连的,看作一个连通块,问有多少个连通块。

思路

八个方向找连通块。

代码

#include<bits/stdc++.h>
using namespace std;

/*
染色&洪水填充

连通性的问题 


主函数里的逻辑:

枚举遍历二维数组,遇到连通块上的点 1~9,计数 

洪水填充:

1. 0行0列~n+1行m+1列看作是矩阵 
2. 对0,0跑染色 

易错点:
1. 矩阵扩张到了 0行0列~n+1行m+1列,边界问题 
*/

int n, m, cnt, sum;
int vis[105][105];
char mp[105][105];
int d[8][2] = {
    {1, 0}, {-1, 0}, {0, -1}, {0, 1}, {-1, -1}, {1, -1}, {-1, 1}, {1, 1} 
}; 

void dfs(int x, int y) {
    vis[x][y] = 1;  // 当前格子标记成1
    for (int i=0; i<8; i++) {
        int nx = x + d[i][0];
        int ny = y + d[i][1];

        if (nx < 1 or nx > n or ny < 1 or ny > m) continue;
        if (mp[nx][ny] == '.' or vis[nx][ny] == 1) continue;

        dfs(nx, ny);
    } 
}

signed main(){
    ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);


    cin >> n >> m;
    for (int i=1; i<=n; i++) {
        for (int j=1; j<=m; j++) {
            cin >> mp[i][j];
        }
    }

    for (int i=1; i<=n; i++) {
        for (int j=1; j<=m; j++) {
            if (mp[i][j] == 'W' and vis[i][j] == 0) {
                cnt++;
                dfs(i, j);
            }
        }
    }

    cout << cnt;
    return 0;
}

错因分析

把 W 和 . 的含义搞反了,盐津虾

T2

题意

被 * 围起来的区域,洪水是进不去的,统计最后有多少个区域没有被淹。

思路

洪水填充的思路:

矩阵外扩一圈,看作是 0~n+1 0~m+1 范围的矩阵,从 0,0 开始跑洪水填充。

代码

#include<bits/stdc++.h>
using namespace std;

/*
染色&洪水填充

连通性的问题 


主函数里的逻辑:

枚举遍历二维数组,遇到连通块上的点 1~9,计数 

洪水填充:

1. 0行0列~n+1行m+1列看作是矩阵 
2. 对0,0跑染色 

易错点:
1. 矩阵扩张到了 0行0列~n+1行m+1列,边界问题 
*/

int n, m;
int vis[105][105];
char mp[105][105];
int d[4][2] = {
    {1, 0}, {-1, 0}, {0, -1}, {0, 1}
}; 

void dfs(int x, int y) {
    vis[x][y] = 1;
    for (int i=0; i<4; i++) {
        int nx = x + d[i][0];
        int ny = y + d[i][1];

        if (nx < 0 or nx > n+1 or ny < 0 or n y > m+1) continue;
        if (mp[nx][ny] == '*' or vis[nx][ny] == 1) continue;
        dfs(nx, ny);
    }
}

signed main(){
    ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);


    cin >> n >> m;
    for (int i=1; i<=n; i++) {
        for (int j=1; j<=m; j++) {
            cin >> mp[i][j];
        }
    }

    dfs(0, 0); 

    // 找mp[i][j] == '0' 并且 vis[i][j] == 0 
    return 0;
}

T3

统计连通块的格子数:

  1. 全局变量定义 sum
  2. 每次 dfs 进一次,sum++
  3. 主函数每次搜索开始前重置 sum 的值

注意,这不是T3的正确代码.

#include<bits/stdc++.h>
using namespace std;

int n, m, cnt, sum;
int vis[105][105];
char mp[105][105];
int d[8][2] = {
    {1, 0}, {-1, 0}, {0, -1}, {0, 1}, {-1, -1}, {1, -1}, {-1, 1}, {1, 1} 
}; 

void dfs(int x, int y) {
    vis[x][y] = 1;  // 当前格子标记成1
    sum++;
    for (int i=0; i<8; i++) {
        int nx = x + d[i][0];
        int ny = y + d[i][1];

        if (nx < 1 or nx > n or ny < 1 or ny > m) continue;
        if (mp[nx][ny] == '.' or vis[nx][ny] == 1) continue;

        dfs(nx, ny);
    } 
}

signed main(){
    ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);

    for (int i=1; i<=n; i++) {
        for (int j=1; j<=m; j++) {
            if (mp[i][j] == 'W' and vis[i][j] == 0) {
                cnt++;
                sum = 0;
                dfs(i, j);

                // 每次的sum,就是这次的连通块的格子数量 
            }
        }
    }

    cout << cnt;
    return 0;
}

T4

/*
枚举以 i,j 为起点,搜索这次能找到的最大宝藏数。

int max1, max2;

dfs() {
    本次搜索的最大值 
}

宝藏 > maxn {
    maxn = 宝藏;
    记录这次的i, j 
} 
*/

代码:

#include<bits/stdc++.h>
#define int long long
using namespace std;
int n, m;
const int N = 3e3 + 5;
int a[N][N];
int v[N][N];
int d[4][2] = {{1, 0}, {-1, 0}, {0, -1}, {0, 1}};
int xx, yy, maxn;
int res;
void dfs(int x, int y, int sum){
    if(maxn < sum){
        maxn = sum;
    }

    for(int i = 0;i < 4;i++){
        int nx = x + d[i][0];
        int ny = y + d[i][1];
        if(nx > n || nx < 1 || ny > m || ny < 1){
            continue;
        }
        if(a[nx][ny] >= a[x][y]){
            continue;
        }
        dfs(nx, ny, sum + 1);
    }
}

signed main(){
    ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
    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++){
            maxn = 0;
            dfs(i, j, 1);
            if (maxn > res) {
                res = maxn;
                xx = i;
                yy = j;
            }
        }
    }
    cout << xx<< ' '<< yy<< '\n'<< res;
    return 0;
} 

T5

/*
题意:
1是雷,0不是雷。

数字:一个不是雷的格子,周围有几个雷,上面的数字就是几
空格:数字是0的,是空格 
空:空格的八方向连通块。 

空的数量 + 周围没有空格的数字数量 

1. 根据输入的矩阵,建立好棋盘 
*/

#include <bit/stdc++.h>
using namespace std;

const int N = 1e3+5;

int n, m;
int a[N][N], mp[N][N];
int d[8][2] = {
    {1, 0}, {-1, 0}, {0, -1}, {0, 1}, {-1, -1}, {1, -1}, {-1, 1}, {1, 1}
};

void change(int x, int y) {
    // 让x, y的周围八格,不是雷的,值++
    for (int i=0; i<8; i++) {
        int nx = x + d[i][0];
        int ny = y + d[i][1];

        if (nx < 1 or nx > n or ny < 1 or ny > m) continue;
        if (a[nx][ny] == 1) continue;
        mp[nx][ny]++; 
    } 
}

int main() {
    cin >> n >> m;
    for (int i=1; i<=n; i++) {
        for (int j=1; j<=m; j++) {
            cin >> a[i][j];
        }
    }

    // 如果 a[i][j] == 1,周围的八个位置,不是雷的格子,值+1
    for (int i=1; i<=n; i++) {
        for (int j=1; j<=m; j++) {
            if (a[i][j] == 1) {  // 自己是雷 
                mp[i][j] = -1;  // 标记自己
                // 让周围八个格子不是雷的,数字+1 
            }
        }
    } 

    /*
        1. 统计mp数组上周围没有0的数字数量 
        2. 空格连通块的数量。。。 mp数组上统计0的八连通块的数量。 
    */ 
    return 0;
}