总显示Waiting折么办~求救
查看原帖
总显示Waiting折么办~求救
654004
Zhouyuhan_1125楼主2024/3/24 22:20
#include <bits/stdc++.h>
using namespace std;
int n,m;
char a[105][105];
bool vis[105][105];
int dirx[8] = {1,-1,0,0,1,-1,1,-1};
int diry[8] = {0,0,-1,1,1,-1,-1,1};
struct node{
    int x,y;
};
void bfs(int x1,int y1){
    vis[x1][y1] = true;
    queue<node> q;
    q.push({x1,y1});
    while(!q.empty()){
        node tm = q.front();
        q.pop();
        for(int i = 0; i < 8; i++){
            int dx = tm.x + dirx[i];
            int dy = tm.y + diry[i];
            if(dx < 1 || dx > n || dy < 1 || dy > m){
                continue;
            }
            if(a[dx][dy] == '.' || vis[dx][dy] == true){
                continue;
            }
            q.push({dx,dy});
            vis[dx][dy] = true;
        }
    }
}
int main(){
    cin >> n >> m;
    for(int i = 1; i <= n; i++){
        for(int j = 1; j <= m; j++){
            cin >> a[i][j];
        }
    }
    int ans = 0;
    for(int i = 1; i <= n; i++){
        for(int j = 1; j <= m; j++){
            if(a[i][j] == 'W' && vis[i][j] == 0){
                bfs(i,j);
                ans += 1;
            }
        }
    }
    cout << ans;
    return 0;
}
2024/3/24 22:20
加载中...