ANS

x-hechengye 2022-05-21 16:24:02 14 返回题目

#include <bits/stdc++.h>
using namespace std;
char a[110][110];
int cnt = 0, n, m, dx[8] = { 0, 0, -1, 1, -1, 1, -1, 1 }, dy[8] = { -1, 1, 0, 0, -1, 1, 1, -1 };
struct node {
    int x, y;
};
queue<node> q;
void bfs(int x, int y) {
    q.push(node{ x, y });
    a[x][y] = '1';
    while (!q.empty()) {
        node now = q.front();
        q.pop();
        for (int i = 0; i < 8; i++) {
            int nx = now.x + dx[i];
            int ny = now.y + dy[i];
            if (a[nx][ny] == '.')
                continue;
            q.push(node{ nx, ny });
            a[nx][ny] = '.';
        }
    }
}
int main() {
    cin >> n >> m;
    memset(a, '.', sizeof(a));
    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= m; j++) cin >> a[i][j];
    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= m; j++)
            if (a[i][j] == 'W') {
                cnt++;
                bfs(i, j);
            }
    cout << cnt;
    return 0;
}
{{ vote && vote.total.up }}