求助,只过了一个点

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

whitefish @ 2024-04-02 19:21:42

讨论区很多佬的样例都试过了,但还是不对

#include<iostream>
#include<bits/stdc++.h>
using namespace std;
int n,ret[1010][1010],cnt=2;
bool vis[1010];
int dx[5] = {0, 0, 1, -1};
int dy[5] = {1, -1, 0, 0};
bool check(int x,int y){
    if(x>=1&&x<=n&&y>=1&&y<=n)
        return 1;
    return 0;
}
void dfs(int x,int y,int color){
    ret[x][y] = color;
    for (int k = 0; k < 4;k++){
        if(check(x+dx[k],y+dy[k])&&ret[x+dx[k]][y+dy[k]]==1)
            dfs(x+dx[k],y+dy[k],color);
    }  
}
int main(){
    cin >> n;
    memset(vis, 0, sizeof(vis));
    for (int i = 1; i <= n;i++){
        for (int j = 1; j <= n;j++){
            char temp;
            cin >> temp;
            if(temp=='#'){
                ret[i][j] = 1;
            }
            else{
                ret[i][j] = 0;
            }
        }
    }
    int ans = 0;
    for (int i = 1; i <= n;i++){
        for (int j = 1; j <= n;j++){
            if(ret[i][j]==1){
            dfs(i, j,cnt);
            cnt++;
            }
        }
    }
   for (int i = 1; i <= n;i++){
        for (int j = 1; j <= n;j++){
            if(ret[i][j]==0||vis[ret[i][j]])continue;
            bool flag = 0;
            for (int k = 0; k < 4;k++){
               if(check(i+dx[k],j+dy[k])&&ret[i+dx[k]][j+dy[k]]==0){
                   flag = 1;
                  break;
                 }
            }
            if(flag==1)continue;
            ans++;
            vis[ret[i][j]] = 1;
        }
    }
    cout << ans;
}

by Party_dragon @ 2024-05-08 23:46:56

看看是不是输出了没淹没的岛屿,我刚开始输错了结果,也只过了一个


by shayebuhxiaomengxin @ 2024-07-05 22:00:45

@Party_dragon 感谢大佬


by jmy2012 @ 2024-07-20 17:25:01


|