Files
python/TangDou/AcWing/FloodFill/1098_dfs_2.cpp
HuangHai 1f397eca87 'commit'
2025-08-30 18:35:01 +08:00

41 lines
1.2 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include <bits/stdc++.h>
using namespace std;
const int N = 55;
int g[N][N];
int st[N][N];
int n, m;
int dx[] = {0, -1, 0, 1}; // 左上右上
int dy[] = {-1, 0, 1, 0}; // 西北东南 1 2 4 8 二进制位运算,参考1098_0.cpp
int dfs(int sx, int sy) {
st[sx][sy] = true; // 标识此位置已访问过
int ans = 1; // 自己贡献一个面积
for (int i = 0; i < 4; i++) {
int tx = sx + dx[i], ty = sy + dy[i];
if (tx == 0 || tx > n || ty == 0 || ty > m) continue;
if (st[tx][ty]) continue;
if (g[sx][sy] >> i & 1) continue; // 自带数位压缩表示法~,有墙
ans += dfs(tx, ty); // 孩子们继续贡献面积
}
return ans; // 我们的总面积
}
int cnt, area;
int main() {
cin >> n >> m;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
cin >> g[i][j];
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
if (!st[i][j]) {
cnt++; // 连通块数量
area = max(area, dfs(i, j)); // PK目前的最大面积
}
// 输出结果
printf("%d\n%d\n", cnt, area);
return 0;
}