'commit'
This commit is contained in:
40
TangDou/AcWing/FloodFill/1098_dfs_2.cpp
Normal file
40
TangDou/AcWing/FloodFill/1098_dfs_2.cpp
Normal file
@@ -0,0 +1,40 @@
|
||||
#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;
|
||||
}
|
||||
Reference in New Issue
Block a user