洪水填充-Flood Fill
色块
https://www.luogu.com.cn/problem/U210909
https://www.bilibili.com/video/BV1nB4y197i4?spm_id_from=333.337.search-card.all.click
#include<bits/stdc++.h>
using namespace std;
const int maxn=1005;
int a[maxn][maxn];
int n,m,cnt;
int dx[]={-1,1,0,0};
int dy[]={0,0,-1,1};
//用color 去填充相邻的单元格
void fill(int x,int y,int color){
//需要填充的单元格和上一次颜色不同 退出
//超界退出
if(a[x][y]!=color || x<=0 || x>n || y<=0 || y>m){
return;
}
a[x][y]=0;//搜索过设置为0 后续不再搜索
for(int i=0;i<4;i++){//继续递归填充 上下 左右四个点
int xx=x+dx[i];
int yy=y+dy[i];
fill(xx,yy,color);
}
}
int main(){
cin>>n>>m;
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
cin>>a[i][j];
}
}
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
if(a[i][j]!=0){//未填充过 去填充 一次把以这个点为起点的周围填充完
fill(i,j,a[i][j]);
cnt++;//有几块填充几次
}
}
}
cout<<cnt;
}
01迷宫
https://www.luogu.com.cn/problem/P1141
填涂颜色
https://www.luogu.com.cn/problem/P1162
拯救oibh总部
https://www.luogu.com.cn/problem/P1506
求细胞数量
https://www.luogu.com.cn/problem/P1451
[USACO07OCT]Obstacle Course S
https://www.luogu.com.cn/problem/P1649
Flood Fill
https://www.luogu.com.cn/problem/CF1114D
[NOIP2010 提高组] 引水入城
https://www.luogu.com.cn/problem/P1514
作者:newcode 更多资源请关注纽扣编程微信公众号

从事机器人比赛、机器人等级考试、少儿scratch编程、信息学奥赛等研究学习

浙公网安备 33010602011771号