c++求调,玄关

P8662 [蓝桥杯 2018 省 AB] 全球变暖

nahida_badminton @ 2024-05-21 21:49:36

#include <bits/stdc++.h>
using namespace std;
int n, a1 = 0, a2 = 0, i, j, f1[4] = {-1, 0, 1, 0}, f2[4] = {0, 1, 0, -1};
char c1[1005][1005], c2[1005][1005];

void dfs(int a, int b){
    c1[a][b] = '.';
    for (i = 0; i < 4; i++){
        if (c1[a + f1[i]][b + f2[i]] != '.')
            dfs(a + f1[i], b + f2[i]);
    }
    return;
}

void fsl(int a, int b){
    c2[a][b] = '.';
    for (i = 0; i < 4; i++){
        if (c2[a + f1[i]][b + f2[i]] == '#')
            fsl(a + f1[i], b + f2[i]);
    }
    return;
}

int main(){
    ios::sync_with_stdio(0);
    cin >> n;
    for (i = 1; i <= n; i++){
        for (j = 1; j <= n; j++) cin >> c1[i][j], c2[i][j] = c1[i][j];
    }
    for (i = 1; i <= n; i++)
        for (j = 1; j <= n; j++)
            if (c1[i][j] == '#' && (c1[i - 1][j] == '.' || c1[i + 1][j] == '.' || c1[i][j + 1] == '.' || c1[i][j - 1] == '.')) c1[i][j] = '-';
    for (i = 1; i <= n; i++){
        for (j = 1; j <= n; j++){
            if (c2[i][j] == '#'){
                a1++;
                fsl(i, j);
            }
        }
    }
    for (i = 1; i <= n; i++){
        for (j = 1; j <= n; j++){
            if (c2[i][j] == '#'){
                a2++;
                dfs(i, j);
            }
        }
    }
    cout << a1 - a2 << endl;
    return 0;
}

|