Flood Fill 算法

目录

介绍

概念

参数

实现方式

四邻域填充

dfs实现

bfs实现

八邻域填充

dfs实现

bfs实现

 扫描线算法


--------------------------------------------------华丽的分割线------------------------------------------------------------


介绍

概念

Flood Fill算法,又称洪水填充,泛洪算法

其思想是:从一个种子点开始,将与其连通(可达且颜色相同/相近)的点全部染上另外一种颜色,直到所有可达节点都被染色为止。

参数

flood fill需要三个参数:待填充图像,起点,新颜色

实现方式

flood fill常用的有:四邻域flood fill,八邻域flood fill,扫描线算法

其按实现方式又可分为:递归实现(dfs),队列实现(bfs),栈实现

因为自己太菜篇幅有限,本篇只介绍dfs和bfs实现

四邻域填充

四邻域填充的思想是:给定一个点,将该点染色后,将其周围上下左右四个点进行染色(要染色的点颜色须和起点颜色相同/相近)

dfs实现

#include<iostream>
using namespace std;
const int N = 145;
int image[N][N];
int n, m;
int dir[4][2] = { -1,0,0,1,1,0,0,-1 };
// 检查(x,y)是否可达
bool check(int x, int y, int old_color, int new_color) {
	return x >= 0 && x < n && y >= 0 && y < m && image[x][y] == old_color && image[x][y] != new_color;
}
void flood_fill(int x, int y, int old_color, int new_color) {
	if (old_color == new_color) return;  // 没有这个条件,在old_color和new_color相同时会无限递归
	image[x][y] = new_color;  //填充当前点
	for (int i = 0; i < 4; i++) {  // 遍历四个方向
		int nx = x + dir[i][0], ny = y + dir[i][1];
		if (check(nx, ny, old_color, new_color)) flood_fill(nx, ny, old_color, new_color);
	}
}

int main() {
	// 输入n*m的图像
	cin >> n >> m;
	for (int i = 0; i < n; i++)
		for (int j = 0; j < m; j++) cin >> image[i][j];
	// 输入起点和新颜色
	int sx, sy, new_color;
	cin >> sx >> sy >> new_color;
	// 洪水填充
	flood_fill(sx, sy, image[sx][sy], new_color);
	// 输出图像
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < m; j++) cout << image[i][j] << " ";
		cout << endl;
	}
	return 0;
}

然而,使用dfs有一个致命缺陷:如果图像很大,填充时可能发生栈溢出

bfs实现

#include<iostream>
#include<queue>
using namespace std;
const int N = 145;
int image[N][N];
int n, m;
int dir[4][2] = { -1,0,0,1,1,0,0,-1 };
struct node {
	int x, y;
	node(int x, int y): x(x), y(y){}
};
queue<node> q;
// 检查(x,y)是否可达
bool check(int x, int y, int old_color, int new_color) {
	return x >= 0 && x < n && y >= 0 && y < m && image[x][y] == old_color && image[x][y] != new_color;
}
// 给点(x,y)染色
void stain(int x, int y, int color) {
	q.emplace(x, y);
	image[x][y] = color;
}
// start表示起点,new_color表示新颜色
void flood_fill(node start, int new_color) {
	int sx = start.x, sy = start.y;
	int old_color = image[sx][sy];
	stain(sx, sy, new_color);
	while (q.size()) {
		node t = q.front();
		q.pop();
		for (int i = 0; i < 4; i++) {
			int nx = t.x + dir[i][0], ny = t.y + dir[i][1];
			if (check(nx, ny, old_color, new_color)) stain(nx, ny, new_color);
		}
	}
}

int main() {
	// 输入n*m的图像
	cin >> n >> m;
	for (int i = 0; i < n; i++)
		for (int j = 0; j < m; j++) cin >> image[i][j];
	// 输入起点和新颜色
	int sx, sy, new_color;
	cin >> sx >> sy >> new_color;
	// 洪水填充
	flood_fill(node(sx, sy), new_color);
	// 输出图像
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < m; j++) cout << image[i][j] << " ";
		cout << endl;
	}
	return 0;
}

八邻域填充

八邻域填充和四邻域填充类似,不过从一个点可以扩展到周围八个方向:上,下,左,右,左上,左下,右上,右下

dfs实现

#include<iostream>
using namespace std;
const int N = 145;
int image[N][N];
int n, m;
int dir[8][2] = {
	{-1, -1},
	{-1, 0},
	{-1, 1},
	{0, -1},
	{0, 1},
	{1, -1},
	{1, 0},
	{1, 1}
};
// 检查(x,y)是否可达
bool check(int x, int y, int old_color, int new_color) {
	return x >= 0 && x < n&& y >= 0 && y < m&& image[x][y] == old_color && image[x][y] != new_color;
}
void flood_fill(int x, int y, int old_color, int new_color) {
	if (old_color == new_color) return;  // 没有这个条件,在old_color和new_color相同时会无限递归
	image[x][y] = new_color;  //填充当前点
	for (int i = 0; i < 8; i++) {  // 遍历四个方向
		int nx = x + dir[i][0], ny = y + dir[i][1];
		if (check(nx, ny, old_color, new_color)) flood_fill(nx, ny, old_color, new_color);
	}
}

int main() {
	// 输入n*m的图像
	cin >> n >> m;
	for (int i = 0; i < n; i++)
		for (int j = 0; j < m; j++) cin >> image[i][j];
	// 输入起点和新颜色
	int sx, sy, new_color;
	cin >> sx >> sy >> new_color;
	// 洪水填充
	flood_fill(sx, sy, image[sx][sy], new_color);
	// 输出图像
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < m; j++) cout << image[i][j] << " ";
		cout << endl;
	}
	return 0;
}

bfs实现

#include<iostream>
#include<queue>
using namespace std;
const int N = 145;
int image[N][N];
int n, m;
int dir[8][2] = {
	{-1, -1},
	{-1, 0},
	{-1, 1},
	{0, -1},
	{0, 1},
	{1, -1},
	{1, 0},
	{1, 1}
};
struct node {
	int x, y;
	node(int x, int y) : x(x), y(y) {}
};
queue<node> q;
// 检查(x,y)是否可达
bool check(int x, int y, int old_color, int new_color) {
	return x >= 0 && x < n&& y >= 0 && y < m&& image[x][y] == old_color && image[x][y] != new_color;
}
// 给点(x,y)染色
void stain(int x, int y, int color) {
	q.emplace(x, y);
	image[x][y] = color;
}
// start表示起点,new_color表示新颜色
void flood_fill(node start, int new_color) {
	int sx = start.x, sy = start.y;
	int old_color = image[sx][sy];
	stain(sx, sy, new_color);
	while (q.size()) {
		node t = q.front();
		q.pop();
		for (int i = 0; i < 8; i++) {
			int nx = t.x + dir[i][0], ny = t.y + dir[i][1];
			if (check(nx, ny, old_color, new_color)) stain(nx, ny, new_color);
		}
	}
}

int main() {
	// 输入n*m的图像
	cin >> n >> m;
	for (int i = 0; i < n; i++)
		for (int j = 0; j < m; j++) cin >> image[i][j];
	// 输入起点和新颜色
	int sx, sy, new_color;
	cin >> sx >> sy >> new_color;
	// 洪水填充
	flood_fill(node(sx, sy), new_color);
	// 输出图像
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < m; j++) cout << image[i][j] << " ";
		cout << endl;
	}
	return 0;
}

 扫描线算法

限于篇幅和作者能力,本篇不解释

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值