2 solutions

  • 0
    @ 2022-3-25 19:13:14
    #include <stdio.h>
    #include <math.h>
    #include <algorithm>
    #include <iostream>
    #include <string.h>
    #include <queue>
    #include <stack>
    #include <map>
    #include <set>
    #include <vector>
    using namespace std;
    int n,m,ans;
    int dx[4]= {0,0,1,-1},dy[4]= {1,-1,0,0};
    char a[105][105];
    int vis[105][105];
    void dfs(int xx,int yy) {
        vis[xx][yy]=1;
        for(int i=0; i<4; i++) {
            int x=xx+dx[i],y=yy+dy[i];
            if(x>=0&&y>=0&&x<n&&y<m&&!vis[x][y]&&a[x][y]=='W') {
                dfs(x,y);
            }
        }
    }
    int main() {
        scanf("%d%d",&n,&m);
        for(int i=0; i<n; i++) {
            cin>>a[i];
        }
        for(int i=0; i<n; i++) {
            for(int j=0; j<m; j++) {
                if(!vis[i][j]&&a[i][j]=='W') {
                    ans++;
                    dfs(i,j);
                }
            }
        }
        cout<<ans<<endl;
        return 0;
    }
    

    Information

    ID
    771
    Time
    1000ms
    Memory
    16MiB
    Difficulty
    5
    Tags
    # Submissions
    63
    Accepted
    26
    Uploaded By