给一副二维整数数组图像渲染
这里也可以用沉岛思想
判断一个位置的上下左右是否是新的颜色,是则换成旧的
public int[][] floodFill(int[][] image, int sr, int sc, int newColor) {
int a = image[sr][sc];
if(a != newColor){
search(image,sr,sc,a,newColor);
}
return image;
}
public void search(int[][] image,int i,int j,int a,int newColor){
if( i<0 || i==image.length || j<0 || j==image[0].length || image[i][j]!=a){
return;
}
image[i][j] = newColor;
search(image,i+1,j,a, newColor);
search(image,i-1,j,a, newColor);
search(image,i,j+1,a, newColor);
search(image,i,j-1,a, newColor);
}
这篇博客探讨了一种基于深度优先搜索(DFS)的图像渲染填充算法。通过‘沉岛思想’,从指定起点开始,遍历图像中相邻的不同颜色区域,将它们替换为新的颜色。算法首先检查给定点的颜色,如果与目标颜色不同,则进行递归搜索并替换相邻的颜色。这种方法在图形处理和图像编辑中有着广泛应用。
218

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



