Q: Implement the “paint fill” function that one might see on many image editing programs. That is, given a screen (represented by a 2-dimensional array of Colors), a point, and a new color, fill in the surrounding area until you hit a border of that color.
A:由当前位置向上下左右扩展即可,终止条件是坐标越界或者颜色为目标颜色。
enum Color {
Black, White, Red, Yellow, Green
};
bool paint_fill(color **screen, int m, int n, int x, int y, color c){
if(x<0 || x>=m || y<0 || y>=n) return false;
if(screen[x][y] == c) return false;
else{
screen[x][y] = c;
paint_fill(screen, m, n, x-1, y, c);
paint_fill(screen, m, n, x+1, y, c);
paint_fill(screen, m, n, x, y-1, c);
paint_fill(screen, m, n, x, y+1, c);
}
return true;
}
本文介绍了一个简单的图像填充算法实现,该算法通过递归的方式改变指定起始位置周围的颜色,直至遇到边界。文中提供了详细的代码示例,展示了如何在一个二维颜色数组上应用此算法。
4596

被折叠的 条评论
为什么被折叠?



