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;
}