题目描述
https://www.acwing.com/problem/content/1100/
样例
输入样例:
4 7
11 6 11 6 3 10 6
7 9 6 13 5 15 5
1 10 12 7 13 7 5
13 11 10 8 10 12 13
输出样例:
5
9
#include <iostream>
#include <cstring>
#include <queue>
using namespace std;
const int N = 60;
int n, m;
int g[N][N];
int dist[N][N];//判重数组,距离数组
int dx[] = {0, -1, 0, 1}, dy[] = {-1, 0, 1, 0};
int bfs(int x, int y)
{
queue<pair<int, int>> q;
int area = 1;
dist[x][y] = 0;
q.push({x, y});
while (q.size())
{
auto t = q.front();
q.pop();
for (int i = 0; i < 4; i ++ )
{
int a = t.first + dx[i], b = t.second + dy[i];
if (a < 0 || a >= n || b < 0 || b >= m) continue;
if (dist[a][b] != -1) continue;
if (g[t.first][t.second] >> i & 1) continue;//判断是否有墙
area ++ ;
dist[a][b] = dist[t.first][t.second] + 1;
q.push({a, b});
}
}
return area;
}
int main()
{
cin >> n >> m;
for (int i = 0; i < n; i ++ )
for (int j = 0; j < m; j ++ )
cin >> g[i][j];
memset(dist, -1, sizeof(dist));
int area = 0, res = 0;
for (int i = 0; i < n; i ++ )
for (int j = 0; j < m; j ++ )
if (dist[i][j] == -1)
{
area = max(area, bfs(i, j));
res ++ ;
}
cout << res << endl;
cout << area << endl;
return 0;
}