1 条题解

  • 0
    @ 2025-5-10 19:12:57
    #include<bits/stdc++.h>
    using namespace std;
    struct point{// xy坐标
        int x;
        int y;
    };
    int main(){
        int n,m;
        cin>>n>>m;
        char a[n][m];
        for (int i=0;i<n;i++){
            for (int j=0;j<m;j++){
                cin>>a[i][j];
            }
        }//输入
        bool zt[n][m]={};
        int dx[]={0,1,1,1,0,-1,-1,-1};
        int dy[]={-1,-1,0,1,1,1,0,-1};// 方向
        int cnt=0;
        for (int i=0;i<n;i++){
            for (int j=0;j<m;j++){
                if (a[i][j]=='W' && !zt[i][j]){
                    queue<point> o;
                    o.push({i,j});
                    zt[i][j]=true;
                    while (!o.empty()){
                        for (int f=0;f<(int)o.size();f++){
                            point pos=o.front();
                            for (int g=0;g<8;g++){
                                int nx=dx[g]+pos.x;
                                int ny=dy[g]+pos.y;
                                if (nx>=0 && nx<n && ny>=0 && ny<m){
                                    if (!zt[nx][ny]){
                                        if (a[nx][ny]=='W'){
                                            //cout<<nx<<" "<<ny<<endl;
                                            o.push({nx,ny});
                                            zt[nx][ny]=true;
                                        }
                                    }
                                }
                            }
                            o.pop();
                        }
                    }
                    cnt++;
                }
            }
        }//广搜
        cout<<cnt;//输出
        return 0;//完美收官
    }//AC CODE
    
    • 1

    信息

    ID
    45
    时间
    1000ms
    内存
    256MiB
    难度
    4
    标签
    (无)
    递交数
    168
    已通过
    81
    上传者