40 分求助 bfs
查看原帖
40 分求助 bfs
830485
Verdammt_scheisse楼主2023/11/19 13:03
#include <bits/stdc++.h>
using namespace std;
int n, m, ans;

int dx[8] = {-1, 0, 1, 0, -1, -1, 1, 1};
int dy[8] = {0, -1, 0, 1, 1, -1, -1, 1};
char a[1000][1000];
int v[1000][1000];
queue<int> q, p;
void bfs(int i, int j) {
	if (!v[i][j])
		ans++;
	q.push(i);
	p.push(j);
	v[i][j] = 1;
	while (!p.empty()) {
		int c = q.front(), c1 = p.front();
		q.pop();
		p.pop();
		for (int i = 0; i < 8; i++) {
			int nx = c + dx[i], ny = c1 + dy[i];
			if (nx < 0 || v[nx][ny] || ny < 0 || nx >= n || ny >= m || a[nx][ny] == '.')
				continue;
			v[nx][ny] = 1;
		}
	}
}
int main() {
	cin >> 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 (a[i][j] == 'W') {
				bfs(i, j);
			}
		}
	}
	cout << ans;
	return 0;
}

2023/11/19 13:03
加载中...