50pts求条!!!
查看原帖
50pts求条!!!
1140231
ChangeYuAN楼主2024/8/7 09:42
#include <bits/stdc++.h>
using namespace std;
const int MAXLEN = 105;

int vis[MAXLEN][MAXLEN] = {0}, hl = 0, d[8][2] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}, {1, 1}, {1, -1}, {-1, -1}, {-1, 1}},
n, ans = 0, m;
char mp[MAXLEN][MAXLEN];

void dfs(int x, int y) {
	vis[x][y] = 1;
	for (int i = 0; i <= 7; i++) {
		int nx = x + d[i][0], ny = y + d[i][1];
		if (nx > m || nx < 1 || ny > n || ny < 1)
			continue;
		if (vis[nx][ny] == 0 && mp[nx][ny] == 'W')
			dfs(nx, ny);
	}
}

int main() {
	scanf("%d%d", &n, &m);
	for (int i = 0; i < n; i++)
		scanf("%s", mp[i]);
	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= m; j++) {
			if (mp[i][j] == 'W' && vis[i][j] == 0) {
				dfs(i, j);
				ans++;
			}
		}
	}
	printf("%d", ans);
	return 0;
}
2024/8/7 09:42
加载中...