AcWing 1098. 城堡问题
原题链接
简单
作者:
hai阿卢
,
2021-02-22 19:08:10
,
所有人可见
,
阅读 285
#include<iostream>
#include<queue>
using namespace std;
typedef pair<int, int> PII;
const int N = 55;
int a[N][N], f[N][N], n, m;
bool s[N][N];
queue<PII> q;
int BFS(int i, int j)
{
int dx[4] = {0, -1, 0, 1}, dy[4] = {-1, 0, 1, 0};
q.push({i, j});
s[i][j] = true;
int mx_cnt = 1;
while(!q.empty())
{
int x = q.front().first;
int y = q.front().second;
q.pop();
for(int k = 0; k < 4; k ++)
{
if(((a[x][y] >> k) & 1) == 0 && !s[x + dx[k]][y + dy[k]])
{
q.push({x + dx[k], y + dy[k]});
s[x + dx[k]][y + dy[k]] = true;
mx_cnt++;
}
}
}
return mx_cnt;
}
int main()
{
cin >> n >> m;
for(int i = 1; i <= n; i ++)
{
for(int j = 1; j <= m; j ++)
{
cin >> a[i][j];
}
}
int mx = 0, cnt = 0;
for(int i = 1; i <= n; i ++)
{
for(int j = 1; j <= m; j ++)
{
if(!s[i][j])
{
mx = max(mx, BFS(i, j));
cnt ++;
}
}
}
cout << cnt << endl << mx << endl;
return 0;
}