Flood Fill (洪水填充、泛洪填充、油漆桶)算法Java循环实现(BFS方式,非递归)

什么是Flood Fill (洪水填充、泛洪填充、油漆桶算法)

从一个区域中提取若干个连通的点与其他相邻区域区分开(或分别染成不同颜色)的经典算法。
因为其思路类似洪水从一个区域扩散到所有能到达的区域而得名。
详细解释: 维基百科Flood Fill

实现(油漆桶)四通实现

import java.util.LinkedList;

public class FloodFill {

    protected int[][] colors;   // 颜色池

    protected int x, y;         // 二维数组(图片)大小

    /* 记录点 */
    private final class Point {
        int x, y;

        Point(int x, int y) {
            this.x = x;
            this.y = y;
        }
    }

    /**
     * 设置二维数组(图片)
     * @param colors 二维数组,模拟图片每个点的颜色
     * @param x 图片x轴大小
     * @param y 图片y轴大小
     */
    public void setColors(int[][] colors, int x, int y) {
        this.colors = colors;
        this.x = x;
        this.y = y;
    }

    /**
     * floodFill(BFS),选中坐标(cx,cy)必须为有效点
     *
     * @param cx       x 坐标
     * @param cy       y 坐标
     * @param newColor 新颜色
     */
    public final void floodFill(int cx, int cy, int newColor) {
        final LinkedList<Point> q = new LinkedList<>();
        q.offer(new Point(cx, cy));
        final int color = colors[cx][cy];
        colors[cx][cy] = newColor;
        while (!q.isEmpty()) {
            final Point inner = q.poll();
            int x = inner.x;
            int y = inner.y;
            x++;
            if (x < this.x && color == colors[x][y]) {
                colors[x][y] = newColor;
                q.offer(new Point(x, y));
            }
            x -= 2;
            if (x >= 0 && color == colors[x][y]) {
                colors[x][y] = newColor;
                q.offer(new Point(x, y));
            }
            x++;
            y++;
            if (y < this.y && color == colors[x][y]) {
                colors[x][y] = newColor;
                q.offer(new Point(x, y));
            }
            y -= 2;
            if (y >= 0 && color == colors[x][y]) {
                colors[x][y] = newColor;
                q.offer(new Point(x, y));
            }
        }
    }
}

测试(简单使用)

public static void main(String[] args) {
    int[][] colors = new int[][]{
            {0, 1, 2, 1, 1, 1},
            {2, 1, 4, 1, 2, 1},
            {3, 1, 1, 1, 5, 2},
            {5, 0, 1, 0, 1, 9},
            {5, 8, 2, 0, 1, 1},
            {5, 0, 8, 0, 7, 2}
    };
    FloodFill floodFill = new FloodFill();
    floodFill.setColors(colors, 6, 6);
    floodFill.floodFill(1, 1, 6);
    for (int[] c : colors) {
        for (int dot : c)
            System.out.print(dot + ", ");
        System.out.println();
    }
}
/*
******************************
*       测试结果
******************************
*   0, 6, 2, 6, 6, 6, 
*   2, 6, 4, 6, 2, 6, 
*   3, 6, 6, 6, 5, 2, 
*   5, 0, 6, 0, 1, 9, 
*   5, 8, 2, 0, 1, 1, 
*   5, 0, 8, 0, 7, 2,
******************************
*/
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 转自:图像处理之泛洪填充算法(Flood Fill Algorithm) - 流浪的鱼 - 博客频道...
    horu阅读 6,466评论 0 1
  • 三道已知大题大家有把握拿到满分了吗?接下来就要靠小编多年积累的押(zi)题(xin)能力了!!!! 区域填充算法这...
    susu2016阅读 7,821评论 1 3
  • 本系列在简书首发,转载请注明出处,作者うちはイタチ上一篇我们了解了一下,前期准备工作,接下来我们要开始搭建开发环境...
    2eff02f813df阅读 877评论 0 0
  • 天凉了,我没有厚实的棉被 但我可以穿着袜子睡 不至于脚冷 将外套搭在薄被上 多少能挡些风寒 天凉了,我没有厚实的棉...
    a梧桐a阅读 194评论 0 2
  • 为了培养青年教师不断成长,更新教育观念,快速胜任本职工作,早日成为学校的骨干力量,10月31日,南法信中小...
    小红littlered阅读 854评论 0 0