*/
/**
* 思路:假设要对一个像素(比如红色)调用paintFill,即对周围的像素逐一调用paintFill,
* 向外扩张,一旦碰到非红色的像素就停止填充。
*
* 注意:碰到图像问题,要注意screen[y][x]中x和y的顺序。x表示水平轴(即自左向右),实际上对应于列数,而非行数。y的值等于行数。
* @param screen
* @param x
* @param y
* @param ncolor
* @return
*/
public static boolean paintFill(Color[][] screen,int x,int y,Color ncolor){
if(screen[y][x]==ncolor)
return false;
return paintFill(screen, x, y, screen[y][x], ncolor);
}
public static boolean paintFill(Color[][] screen,int x,int y,Color ocolor,Color ncolor){
if(x<0||x>=screen[0].length||y<0||y>=screen.length)
return false;
if(screen[y][x]==ocolor){
screen[y][x]=ncolor;
paintFill(screen, x-1, y, ocolor, ncolor);//左
paintFill(screen, x+1, y, ocolor, ncolor);//右
paintFill(screen, x, y-1, ocolor, ncolor);//上!!!
paintFill(screen, x, y+1, ocolor, ncolor);//下!!!
}
return true;
}enum Color{
Black,White,Red,Yellow,Green
}版权声明:本文为博主原创文章,未经博主允许不得转载。
9.9递归和动态规划(七)——实现许多图片编辑软件都支持的“填充颜色”功能
原文:http://blog.csdn.net/shangqing1123/article/details/47442981