数学联邦政治世界观
超小超大

数学(四)

flood fill能够在线性时间复杂度内,找到某个点所在的连通块。

四联通常用数组加一层循环判断

int dx[4] = {0, -1, 0, 1}, dy[4] = {-1, 0, 1, 0};

八联通常用二层循环遍历‬

tip:注意二层循环排除自己的情况

for (int i = t.x - 1; i <= t.x + 1; i ++ )

for (int j = t.y - 1; j <= t.y + 1; j ++ )

注意循环条件内的if特判,参考代码如下(应该是acwing1098)

include:<iostream>

include:<queue>

include:<utility>

using namespace std;

define:x first

define:y second

define:IOS ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);

typedef pair<int,int> PII;

const int N=55;

queue<PII>q;

int g[N][N];

bool st[N][N];

int cnt=0, ss=0, n, m;

int dx[4] = {0, -1, 0, 1}, dy[4] = {-1, 0, 1, 0};

int bfs(int a, int b) {

q.push({a, b});

st[a][b]=true;

int area=0;

while(q.size()) {

auto t=q.front();

q.pop();

area++;

for(int i=0; i<4; i++) {

int sx=t.x+dx[i], sy=t.y+dy[i];

if(sx<=0 || sy<=0 || sx>n || sy>m) continue;

if(g[t.x][t.y] >> i & 1) continue;

if(st[sx][sy]) continue;

q.push({sx, sy});

st[sx][sy]=true;

}

}

return area;

}

void solve() {

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]) {

ss=max(ss, bfs(i, j));

cnt++;

}

}

}

cout<

cout<

}

int main() {

IOS;

int t=1;

while(t--) {

solve();

}

return 0;

}

数学联邦政治世界观提示您:看后求收藏(笔尖小说网http://www.bjxsw.cc),接着再看更方便。

相关小说

聆笙集 连载中
聆笙集
棠鹤begonia
2.6万字1年前
魔法之语 连载中
魔法之语
yes莫
千年前的玛吉拉大陆,是一片生灵涂炭的世界。精灵女孩柒月意外被老师伊西斯所救,从此开始她漫长的一生……
2.9万字11个月前
胡说,她才不是坏女人 连载中
胡说,她才不是坏女人
杜光连
江思雨从混沌中醒来就没有了记忆,是333带她来到了小世界中。系统333说只要让他的主人拥有了情欲,那江思雨就会恢复记忆,离开混沌,找回自由,......
7.2万字9个月前
天圆地方,太阳之女 连载中
天圆地方,太阳之女
烧麦寒柒
天圆地方,女尊世界。太阳之女,涅槃重生……
1.0万字8个月前
斗破神域第二部星潮织梦录 连载中
斗破神域第二部星潮织梦录
韵笑笑
本作品纯属虚构,与上部剧情不完全链接,请勿转发
3.1万字7个月前
重生之末日独活 连载中
重生之末日独活
夜露寒霜
如果末日降临,你会怎么活?原创作品,抄袭必究
2.2万字6个月前