You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

43 lines
1.3 KiB

2 years ago
#include <bits/stdc++.h>
using namespace std;
const int N = 55;
int g[N][N];
int st[N][N];
int n, m, ans; //注意这里的ans不能做为dfs参数进行传递因为维护的是同一个变量
int dx[] = {0, -1, 0, 1}; //左上右上
int dy[] = {-1, 0, 1, 0}; //西北东南 1 2 4 8 二进制位运算参考1098_0.cpp
void dfs(int sx, int sy) {
st[sx][sy] = true; //标识此位置已访问过
ans++; //到达一个位置,那么面积肯定增大一个
for (int i = 0; i < 4; i++) {
int x = sx + dx[i], y = sy + dy[i];
if (x == 0 || x > n || y == 0 || y > m) continue;
if (st[x][y]) continue;
if (g[sx][sy] >> i & 1) continue; //自带数位压缩表示法~,有墙
dfs(x, y);
}
}
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++; //连通块数量
ans = 0; //清零重新统计
dfs(i, j); //开始Flood Fill
area = max(area, ans); // PK目前的最大面积
}
//输出结果
printf("%d\n%d\n", cnt, area);
return 0;
}