【正经题解】最大黑区域
2024-02-21 17:48:52
发布于:浙江
13阅读
0回复
0点赞
#include <bits/stdc++.h>
using namespace std;
const int N = 109;
int d[N][N];
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};
int n, m;
int dfs(int x, int y) {
int cnt = 1; // 当前黑区域的像素数
d[x][y] = 0; // 标记为已访问
for (int k = 0; k < 4; k++) {
int nx = x + dx[k], ny = y + dy[k];
if (nx >= 1 && nx <= n && ny >= 1 && ny <= m && d[nx][ny] == 1) {
cnt += dfs(nx, ny);
}
}
return cnt;
}
int main() {
cin >> n >> m;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
cin >> d[i][j];
}
}
int ans = 0;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
if (d[i][j] == 1) {
ans = max(ans, dfs(i, j));
}
}
}
cout << ans << endl;
return 0;
}
这里空空如也
有帮助,赞一个