floodfill算法
来源:互联网 发布:常用办公软件是什么 编辑:程序博客网 时间:2024/09/21 08:52
From: http://hi.baidu.com/qteqpid_pku/blog/item/fcb366eeeb0d182f2cf53464.html
+ From wikipedia
Flood fill, also called seed fill, is an algorithm that determines the area connected to a given node in a multi-dimensional array. It is used in the "bucket" fill tool of paint programs to determine which parts of a bitmap to fill with color
+ the algorithms
The flood fill algorithm takes three parameters: a start node, a target color, and a replacement color. The algorithm looks for all nodes in the array which are connected to the start node by a path of the target color, and changes them to the replacement color. There are many ways in which the flood-fill algorithm can be structured, but they all make use of a queue or stack data structure, explicitly or implicitly.
从上面的算法介绍可知,凡是会搜索的同学就能很轻易地掌握floodfill。因为floodfill算法从大体而言可以细分为两种算法思想,一种是DFS,一种是BFS。以下讲介绍两大种常用的算法,并简单分析其中用到的dfs和bfs。
1. per-pixel fill (点点填充)
2. scanline fill (扫描线填充)
//stack friendly and fast floodfill algorithm(递归深搜的写法)
void floodFillScanline(int x, int y, int newColor, int oldColor)
{
if(oldColor == newColor) return;
if(screenBuffer[x][y] != oldColor) return;
int y1;
//draw current scanline from start position to the top
y1 = y;
while(y1 < h && screenBuffer[x][y1] == oldColor)
{
screenBuffer[x][y1] = newColor;
y1++;
}
//draw current scanline from start position to the bottom
y1 = y - 1;
while(y1 >= 0 && screenBuffer[x][y1] == oldColor)
{
screenBuffer[x][y1] = newColor;
y1--;
}
//test for new scanlines to the left
y1 = y;
while(y1 < h && screenBuffer[x][y1] == newColor)
{
if(x > 0 && screenBuffer[x - 1][y1] == oldColor)
{
floodFillScanline(x - 1, y1, newColor, oldColor);
}
y1++;
}
y1 = y - 1;
while(y1 >= 0 && screenBuffer[x][y1] == newColor)
{
if(x > 0 && screenBuffer[x - 1][y1] == oldColor)
{
floodFillScanline(x - 1, y1, newColor, oldColor);
}
y1--;
}
//test for new scanlines to the right
y1 = y;
while(y1 < h && screenBuffer[x][y1] == newColor)
{
if(x < w - 1 && screenBuffer[x + 1][y1] == oldColor)
{
floodFillScanline(x + 1, y1, newColor, oldColor);
}
y1++;
}
y1 = y - 1;
while(y1 >= 0 && screenBuffer[x][y1] == newColor)
{
if(x < w - 1 && screenBuffer[x + 1][y1] == oldColor)
{
floodFillScanline(x + 1, y1, newColor, oldColor);
}
y1--;
}
}
//The scanline floodfill algorithm using our own stack routines, faster(广搜队列的写法)
void floodFillScanlineStack(int x, int y, int newColor, int oldColor)
{
if(oldColor == newColor) return;
emptyStack();
int y1;
bool spanLeft, spanRight;
if(!push(x, y)) return;
while(pop(x, y))
{
y1 = y;
while(y1 >= 0 && screenBuffer[x][y1] == oldColor) y1--;
y1++;
spanLeft = spanRight = 0;
while(y1 < h && screenBuffer[x][y1] == oldColor )
{
screenBuffer[x][y1] = newColor;
if(!spanLeft && x > 0 && screenBuffer[x - 1][y1] == oldColor)
{
if(!push(x - 1, y1)) return;
spanLeft = 1;
}
else if(spanLeft && x > 0 && screenBuffer[x - 1][y1] != oldColor)
{
spanLeft = 0;
}//写这一部分是防止因为同一列上有断开的段而造成的可能的“没填充”
if(!spanRight && x < w - 1 && screenBuffer[x + 1][y1] == oldColor)
{
if(!push(x + 1, y1)) return;
spanRight = 1;
}
else if(spanRight && x < w - 1 && screenBuffer[x + 1][y1] != oldColor)
{
spanRight = 0;
} //写这一部分是防止因为同一列上有断开的段而造成的可能的“没填充”
y1++;
}
}
}
以上两个小算法填充的方向都是纵向填充,你也可以修改成横向填充。
- floodfill算法
- floodfill算法
- floodfill算法
- floodfill算法
- floodfill算法
- FloodFill算法
- FloodFill填充算法
- Ural1033 FloodFill 算法
- FloodFill算法的优化
- 算法:回溯算法之floodfill
- 好用的算法,FloodFill
- FloodFill(洪水填充 )算法
- 【转载】FloodFill算法的优化
- FloodFill(泛洪算法)
- floodfill
- floodfill
- floodfill
- FloodFill(漫水填充)算法
- 聚类算法的评价方法
- 第三代合作伙伴计划(3GPP,3rd Generation Partnership Project)简介
- 数组名做形参和一维指针没区别的验证
- Ubuntu中的用户管理(三)用户管理
- 【线段树】 hdu1698 Just a Hook
- floodfill算法
- platform设备驱动
- IOS
- jstl
- 获取当前显示的activity
- c 可变参数方法
- 正则表达式
- Shuffle an Array or a List - Algorithm in Java
- 读<<夏日南亭怀辛大>>