求调(玄关)
查看原帖
求调(玄关)
1046508
Sparse_Table楼主2024/9/29 21:10

rt.

#include <bits/stdc++.h>
using namespace std;

int n, m, ans = 1, cnt = 0;
const int N = 1005;
bool mp[N][N], vis[N][N];

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

void dfs(int x, int y) {
    if (!(x >= 1 && x <= n && y >= 1 && y <= m && !vis[x][y])) return; 
	vis[x][y] = 1, cnt++;
	if (mp[x][y]) return;
	for (int i = 0; i < 4; i++) {
		int nx = x + dx[i], ny = y + dy[i];
		if (!mp[x][y]) dfs(nx, ny);
	}
}

int main() {
	cin >> n >> m;
	for (int i = 1; i <= n; i++)
		for (int j = 1; j <= m; j++) {
			char c;
			cin >> c;
			if (c == '#') {
				mp[i][j] = 1;
				mp[i - 1][j] = 1;
				mp[i + 1][j] = 1;
				mp[i][j + 1] = 1;
				mp[i][j - 1] = 1; 
				vis[i][j] = 1;
			}
		}
	for (int i = 1; i <= n; i++)
	    for (int j = 1; j <= m; j++)
	        if (!mp[i][j]) {
	            cnt = 0;
	            dfs(i, j);
	            ans = max(ans, cnt);
	        }
	cout << ans;
	return 0;
}
2024/9/29 21:10
加载中...