3 solutions

  • 0
    @ 2023-10-13 16:47:39

    和油田一模一样

    using namespace std;
    #define N 500
    char a[N][N];
    int n,m,cnt;
    int dx[4]={0,1,0,-1};
    int dy[4]={1,0,-1,0};
    void dfs(int x,int y ){
    if(x<0||x>=n||y<0||y>=m||a[x][y]=='.')return ;
    a[x][y]='.';//搜过的点 标记为.,或者重新开个vis[N][N]也ok
    //搜索可到达点
    for(int i=0;i<4;i++){
    int tx=x+dx[i];
    int ty=y+dy[i];
    dfs(tx,ty);
    }
    }
    int main(){
    cin>>n>>m;
    for(int i=0;i<n;i++)
    for(int j=0;j<m;j++)
    cin>>a[i][j];
    for(int i=0;i<n;i++)
    {
    for(int j=0;j<m;j++)
    {
    if(a[i][j]=='#'){
    dfs(i,j);
    cnt++;
    }
    }
    }
    cout<<cnt<<endl;
    return 0;
    }
    
    

    Information

    ID
    720
    Time
    1000ms
    Memory
    64MiB
    Difficulty
    1
    Tags
    # Submissions
    62
    Accepted
    41
    Uploaded By