1098. 城堡问题
作者:互联网
方法BFS,flood fill,别被开头一张图给唬住了,对于每一个方格按位判断能不能走四个方向就行了
#include<iostream>
#include<queue>
using namespace std;
#define PII pair<int ,int>
#define x first
#define y second
const int N = 60;
int g[N][N];
int r, c;
int dx[] = {0, -1, 0, 1}, dy[] = {-1, 0, 1, 0}; // 西、北、东、南
int st[N][N];
int bfs(int x, int y){
queue<PII> q;
q.push({x, y});
st[x][y] = 1;
int ans = 1;
while(q.size()){
auto h = q.front();
int x = h.x, y = h.y;
q.pop();
for(int i = 0; i < 4; i ++){
int a = x + dx[i], b = y + dy[i];
if(a < 0 || a >= r || b < 0 || b >= c || st[a][b]) continue;
if(g[x][y] >> i & 1) continue;
st[a][b] = 1;
ans ++;
q.push({a, b});
}
}
return ans;
}
int main(){
cin >> r >> c;
for(int i = 0; i < r; i ++)
for(int j = 0; j < c; j ++)
cin >> g[i][j];
int res = 0, cnt = 0;
for(int i = 0; i < r; i ++)
for(int j = 0; j < c; j ++){
if(st[i][j] == 0){
res = max(res, bfs(i, j));
cnt ++;
}
}
cout << cnt << endl;
cout << res << endl;
return 0;
}
标签:res,int,城堡,1098,st,问题,++,ans,define 来源: https://www.cnblogs.com/tomori/p/13758054.html