2026.1.28_401_总结

· · 个人记录

总分

\color{green}100pts$+$\color{green}100pts$+$\color{green}100pts$+$\color{green}100pts$+$\color{green}100pts$=$\LARGE \color{red} AK!!!

P1596 [USACO10OCT] Lake Counting S

\color{green}100pts

思路

dfs连通块模板

代码

#include<bits/stdc++.h>
#define Guo using
#define Weiqian namespace
#define Code std
#define ll long long
#define N (int)(1e2 + 10)
#define FIO(IN, OUT); freopen(IN, "r", stdin);freopen(OUT, "w", stdout);
Guo Weiqian Code;
char c[N][N];
bool vis[N][N];
int n, m, cnt;
int dx[] = {0, -1, 0, 1, -1, -1, 1, 1};
int dy[] = {-1, 0, 1, 0, 1, -1, 1, -1};

void dfs(int x, int y){
    for(int i = 0; i < 8; i++){
        int tx = x + dx[i];
        int ty = y + dy[i];
        if(tx > 0 && ty > 0 && tx <= n && ty <= m && !vis[tx][ty] && c[tx][ty] != '.'){
            vis[tx][ty] = 1;
            dfs(tx, ty);
        }
    }
}

int 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 >> c[i][j];
        }
    }
    for(int i = 1; i <= n; i++){
        for(int j = 1; j <= m; j++){
            if(!vis[i][j] && c[i][j] == 'W'){
                vis[i][j] = 1;
                cnt++;
                dfs(i, j);
            }
        }
    }
    cout << cnt;
    return 0;
}

P1451 求细胞数量

\color{green}100pts

思路

dfs连通块模板

代码

#include<bits/stdc++.h>
#define Guo using
#define Weiqian namespace
#define Code std
#define ll long long
#define N (int)(1e2 + 10)
#define FIO(IN, OUT); freopen(IN, "r", stdin);freopen(OUT, "w", stdout);
Guo Weiqian Code;
char c[N][N];
bool vis[N][N];
int n, m, cnt;
int dx[] = {0, -1, 0, 1};
int dy[] = {-1, 0, 1, 0};

void dfs(int x, int y){
    for(int i = 0; i < 4; i++){
        int tx = x + dx[i];
        int ty = y + dy[i];
        if(tx > 0 && ty > 0 && tx <= n && ty <= m && !vis[tx][ty] && c[tx][ty] != '0'){
            vis[tx][ty] = 1;
            dfs(tx, ty);
        }
    }
}

int 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 >> c[i][j];
        }
    }
    for(int i = 1; i <= n; i++){
        for(int j = 1; j <= m; j++){
            if(!vis[i][j] && c[i][j] != '0'){
                vis[i][j] = 1;
                cnt++;
                dfs(i, j);
            }
        }
    }
    cout << cnt;
    return 0;
}

P1331 海战

\color{green}100pts

思路

dfs连通块,要判断不是方形的形状,判断法:

void dfs(int x, int y, int sx, int sy){ if(f){ return; } if(x != sx){ if(c[x - 1][y] != '#'){ f = 1; return; } if(c[x - 1][y - 1] == '#' && c[x][y - 1] != '#'){ f = 1; return; } if(c[x - 1][y + 1] == '#' && c[x][y + 1] != '#'){ f = 1; return; } } if(y != sy){ if(c[x][y - 1] != '#'){ f = 1; return; } if(c[x - 1][y - 1] == '#' && c[x - 1][y] != '#'){ f = 1; return; } if(c[x + 1][y - 1] == '#' && c[x + 1][y] != '#'){ f = 1; return; } } for(int i = 0; i < 4; i++){ int tx = x + dx[i]; int ty = y + dy[i]; if(tx > 0 && ty > 0 && tx <= n && ty <= m && !vis[tx][ty] && c[tx][ty] != '.'){ vis[tx][ty] = 1; dfs(tx, ty, sx, sy); } } }

int 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 >> c[i][j]; } } for(int i = 1; i <= n; i++){ for(int j = 1; j <= m; j++){ if(!vis[i][j] && c[i][j] == '#'){ vis[i][j] = 1; cnt++; dfs(i, j, i, j); } } } if(f){ cout << "Bad placement."; return 0; } cout << "There are " << cnt << " ships."; return 0; }

# P4961 小埋与扫雷
$\color{green}100pts$
## 思路
按照题意模拟,找出所有空格、周围八格没有空格的数字,再dfs算出空格连通块数量,最后计算答案
## 代码
```cpp
#include<bits/stdc++.h>
#define Guo using
#define Weiqian namespace
#define Code std
#define ll long long
#define N (int)(1e3 + 10)
#define FIO(IN, OUT); freopen(IN, "r", stdin);freopen(OUT, "w", stdout);
Guo Weiqian Code;
bool vis[N][N], c[N][N];
int n, m, cnt, a[N][N];
int dx[] = {0, -1, 0, 1, -1, -1, 1, 1};
int dy[] = {-1, 0, 1, 0, 1, -1, 1, -1};

void dfs(int x, int y){
    for(int i = 0; i < 8; i++){
        int tx = x + dx[i];
        int ty = y + dy[i];
        if(a[tx][ty] == 1){
            a[tx][ty] = 3;
        }
        if(tx > 0 && ty > 0 && tx <= n && ty <= m && !vis[tx][ty] && !a[tx][ty]){
            vis[tx][ty] = 1;
            dfs(tx, ty);
        }
    }
}

int 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 >> c[i][j];
        }
    }
    for(int i = 1; i <= n; i++){
        for(int j = 1; j <= m; j++){
            if(c[i][j]){
                a[i][j] = 2;
                for(int k = 0; k < 8; k++){
                    int tx = i + dx[k];
                    int ty = j + dy[k];
                    if(!c[tx][ty]){
                        a[tx][ty] = 1;
                    } 
                }
            }
        }
    }
    for(int i = 1; i <= n; i++){
        for(int j = 1; j <= m; j++){
            if(!vis[i][j] && !a[i][j]){
                vis[i][j] = 1;
                cnt++;
                dfs(i, j);
            }
        }
    }
    for(int i = 1; i <= n; i++){
        for(int j = 1; j <= m; j++){
            if(a[i][j] == 1){
                cnt++;
            }
        }
    }
    cout << cnt;
    return 0;
}

P9243 [蓝桥杯 2023 省 B] 岛屿个数

\color{green}100pts

思路

从最外面灌入海水(一定要八联通,因为斜着的岛屿也能漏进去,如下)

.####
#...#
#####
(左上角会漏)

然后没有被灌到的区域要么是单个岛屿+淡水,要么是岛屿+岛中岛+淡水,符合题目要求,按照dfs连通块做就行

多测记得换行、清空

代码

#include<bits/stdc++.h>
#define Guo using
#define Weiqian namespace
#define Code std
#define ll long long
#define N (int)(1e2 + 10)
#define FIO(IN, OUT); freopen(IN, "r", stdin);freopen(OUT, "w", stdout);
Guo Weiqian Code;
char c[N][N];
bool vis[N][N];
int n, m, cnt;
int dx[] = {0, -1, 0, 1, -1, -1, 1, 1};
int dy[] = {-1, 0, 1, 0, 1, -1, 1, -1};
int dx1[] = {0, -1, 0, 1};
int dy1[] = {-1, 0, 1, 0};

void dfs(int x, int y){
    for(int i = 0; i < 8; i++){
        int tx = x + dx[i];
        int ty = y + dy[i];
        if(tx >= 0 && ty >= 0 && tx <= n + 1 && ty <= m + 1 && !vis[tx][ty] && c[tx][ty] != '1'){
            vis[tx][ty] = 1;
            dfs(tx, ty);
        }
    }
}

void dfs1(int x, int y){
    for(int i = 0; i < 4; i++){
        int tx = x + dx1[i];
        int ty = y + dy1[i];
        if(tx >= 1 && ty >= 1 && tx <= n && ty <= m && !vis[tx][ty]){
            vis[tx][ty] = 1;
            dfs1(tx, ty);
        }
    }
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    int t;
    cin >> t;
    while(t--){
        cin >> n >> m;
        for(int i = 1; i <= n; i++){
            for(int j = 1; j <= m; j++){
                cin >> c[i][j];
            }
        }
        vis[0][0] = 1;
        dfs(0, 0);
        for(int i = 1; i <= n; i++){
            for(int j = 1; j <= m; j++){
                if(!vis[i][j]){
                    vis[i][j] = 1;
                    cnt++;
                    dfs1(i, j);
                }
            }
        }
        cout << cnt << "\n";
        for(int i = 0; i <= n + 1; i++){
            for(int j = 0; j <= m + 1; j++){
                vis[i][j] = 0;
            }
        }
        cnt = 0;
    } 
    return 0;
}