40pts求助
查看原帖
40pts求助
1140668
___W___Z___L___PVP楼主2024/12/5 19:58
#include <bits/stdc++.h>
using namespace std;
int n, m;
char a[1559][1559];

int dx[8] = {1, 0, -1, 0, 1, 1, -1, -1};

int dy[8] = {0, 1, 0, -1, 1, -1, 1, -1};



int s[1500 * 1500 + 9] = {0}, ss = 0, c[1500 * 1500 + 9] = {0}, cc = 0, d[100009] = {0};

void dfs(int x, int y) {
	for (int i = 0; i < 8; i++) {
		int xx = x + dx[i], yy = y + dy[i];
		if (xx >= n || yy >= m || xx < 0 || yy < 0)
			continue;
		if (a[xx][yy] == '.')
			continue;
		a[xx][yy] = '.';
		s[ss]++;
		dfs(xx, yy);
	}
}

int main() {
	cin >> n >> m;
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < m; j++) {
			cin >> a[i][j];
		}
	}
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < m; j++) {
			if (a[i][j] == '*') {
				a[i][j] = '.';
				s[ss]++;
				dfs(i, j);
				ss++;
			}
		}
	}
	for (int i = 0; i < ss; i++) {
		if (d[s[i]] == 0) {
			c[cc] = s[i];
			cc++;
			d[s[i]] = 1;
		} else {
			for (int j = 0; j < cc; j++) {
				if (c[j] == s[i]) {
					c[j] += s[i];
					break;
				}
			}
		}
	}
	sort(c, c + cc);
	cout << cc << " " << c[cc - 1];

	return 0;
}
2024/12/5 19:58
加载中...