JAVA实践伪·漫水填充法

来源:互联网 发布:网络天天分红投资公司 编辑:程序博客网 时间:2024/05/16 12:38

前言

最近在看《啊哈,算法》,这是第四章第五节的内容。

漫水填充法是个What?

Windows的画图程序中有个油漆桶工具,其功能是用颜色填充一个封闭的区域。

比如画了一个封闭的圈,使用油漆桶工具在圈内白色的区域点一下,整个圈里面就被填充成了默认的黑色。点击的那一刻,选中该位置的颜色作为种子颜色,将周围与种子颜色相近的点作为一个区域,然后填充该区域。

PS下的魔棒也是同理,点击一个位置之后,将周围颜色相近的点入队,然后又根据入队的点的颜色作为种子,继续向周围扩展,颜色不超过某个阈值的点就全被选中了。

以上功能是基于漫水填充算法,在选定一个颜色作为种子,后把周边与种子颜色相似的也选中。

功能

给定地图:

 *        {1, 2, 1, 0, 0, 0, 0, 0, 2, 3}, *        {3, 0, 2, 0, 1, 2, 1, 0, 1, 2}, *        {4, 0, 1, 0, 1, 2, 3, 2, 0, 1}, *        {3, 2, 0, 0, 0, 1, 2, 4, 0, 0}, *        {0, 0, 0, 0, 0, 0, 1, 5, 3, 0}, *        {0, 1, 2, 1, 0, 1, 5, 4, 3, 0}, *        {0, 1, 2, 3, 1, 3, 6, 2, 1, 0}, *        {0, 0, 3, 4, 8, 9, 7, 5, 0, 0}, *        {0, 0, 0, 3, 7, 8, 6, 0, 1, 2}, *        {0, 0, 0, 0, 0, 0, 0, 0, 1, 0} * 一个10*10的矩阵 * 1-9表示陆地 * 0表示海洋 * 上下左右相链接的陆地形成一个独立的小岛 * 计算有多少个独立的小岛

参考思路

/** * *        {1, 2, 1, 0, 0, 0, 0, 0, 2, 3}, *        {3, 0, 2, 0, 1, 2, 1, 0, 1, 2}, *        {4, 0, 1, 0, 1, 2, 3, 2, 0, 1}, *        {3, 2, 0, 0, 0, 1, 2, 4, 0, 0}, *        {0, 0, 0, 0, 0, 0, 1, 5, 3, 0}, *        {0, 1, 2, 1, 0, 1, 5, 4, 3, 0}, *        {0, 1, 2, 3, 1, 3, 6, 2, 1, 0}, *        {0, 0, 3, 4, 8, 9, 7, 5, 0, 0}, *        {0, 0, 0, 3, 7, 8, 6, 0, 1, 2}, *        {0, 0, 0, 0, 0, 0, 0, 0, 1, 0} * * 首先要知道如何确定一个岛 * 随机选一个位置,比如第10行第9列。 * 以该位置为种子,向周边扩展。 * 判断凡是大于0的,则是陆地 * 扩展之后 * 有 *  第9行第9列的1 *  第9行第10列的2 * 这两个陆地连着,加上自己,那就是三个陆地形成了一个岛。 * * 如何扩展的呢?宽度优先搜索、深度优先搜索,喜欢哪个选哪个。 * * 既然知道了如何确定一个岛,接下来就是如何确定一块地图内岛的数量 * * 只需要在确定一个岛时多做一步 * 将该地图中经过检查的大于0的陆地改为负数 * * 判断一个图内的岛时 * 选择第一个点,判断是否大于0 * 是 *      进行深度/宽度搜索周边大于0的陆地形成一个岛 *      将搜索过的大于0的陆地改为-1 * 否 * 选择下一个点,判断是否大于0 * 是 *      进行深度/宽度搜索周边大于0的陆地形成一个岛 *      将搜索过的大于0的陆地改为-2(依次递减) * 否 * 选择下一个点 * * 将所有可选择的点选择之后, * 依次递减的数字转成正数再减一 * 就是岛的数量 * */

代码实现

public class IslandExplorer{    static int[][] map = {            {1, 2, 1, 0, 0, 0, 0, 0, 2, 3},            {3, 0, 2, 0, 1, 2, 1, 0, 1, 2},            {4, 0, 1, 0, 1, 2, 3, 2, 0, 1},            {3, 2, 0, 0, 0, 1, 2, 4, 0, 0},            {0, 0, 0, 0, 0, 0, 1, 5, 3, 0},            {0, 1, 2, 1, 0, 1, 5, 4, 3, 0},            {0, 1, 2, 3, 1, 3, 6, 2, 1, 0},            {0, 0, 3, 4, 8, 9, 7, 5, 0, 0},            {0, 0, 0, 3, 7, 8, 6, 0, 1, 2},            {0, 0, 0, 0, 0, 0, 0, 0, 1, 0}    };    static int maxLine = map.length;    static int maxRow = map[0].length;    public static void main(String[] args) {        int color = -1;        for (int i = 0; i < maxLine; i++) {            for (int j = 0; j < maxRow; j++) {                if (map[i][j] > 0) {                    bfs(i+1, j+1, color--);                }            }        }        showArray();        System.out.println("小岛数量:" + (-++color));    }    public static void bfs(int startLine, int startRow, int color) {        //移动的方向        int[][] next = {                {1, 0},     //右                {0, -1},    //下                {-1, 0},    //左                {0, 1}      //上        };        //记录坐标的节点        Node tNode = new Node(startLine, startRow);        //标记已经扩展过的点        int[][] mark = new int[maxLine][maxRow];        //将符合要求的点纳入队列        Node[] lands = new Node[maxLine * maxRow];        int head = 0;        int tail = 0;        lands[tail++] = tNode;        mark[startLine - 1][startRow - 1] = 1;        //将该点着色        map[startLine - 1][startRow - 1] = color;        //System.out.println("Line:" + startLine + ", Row:" + startRow);        while (head < tail) {            for (int i = 0; i < 4; i++) {                startLine = lands[head].getLine() + next[i][0];                startRow = lands[head].getRow() + next[i][1];                if (startLine > maxLine || startRow > maxRow || startLine < 1 || startRow < 1) {                    continue;                }                //System.out.println("Line:" + startLine + ", Row:" + startRow);                if (map[startLine - 1][startRow - 1] > 0 && mark[startLine - 1][startRow - 1] == 0) {                    //符合要求的入队                    tNode = new Node(startLine, startRow);                    map[startLine - 1][startRow - 1] = color;                    lands[tail++] = tNode;                    //标记该点已经过检查                    mark[startLine - 1][startRow - 1] = 1;                }            }            head++;        }    }    private static class Node {        private int line, row;        Node(int line, int row) {            this.line = line;            this.row = row;        }        public int getLine() {            return line;        }        public int getRow() {            return row;        }    }    public static void showArray() {        for (int i = 0; i < maxLine; i++) {            for (int j = 0; j < maxRow; j++) {                System.out.print(map[i][j] + "\t");            }            System.out.println();        }    }}

结果

-1  -1  -1  0   0   0   0   0   -2  -2  -1  0   -1  0   -3  -3  -3  0   -2  -2  -1  0   -1  0   -3  -3  -3  -3  0   -2  -1  -1  0   0   0   -3  -3  -3  0   0   0   0   0   0   0   0   -3  -3  -3  0   0   -3  -3  -3  0   -3  -3  -3  -3  0   0   -3  -3  -3  -3  -3  -3  -3  -3  0   0   0   -3  -3  -3  -3  -3  -3  0   0   0   0   0   -3  -3  -3  -3  0   -4  -4  0   0   0   0   0   0   0   0   -4  0   小岛数量:4

一点收获

这让我想起了游戏《消消乐》,将几个相同的图移动到一起,然后它们就被消除了。

莫大的灵感啊!基于这个原理,消消乐似乎也可以实现了。

一个二维矩阵,将被移动的图案作为种子,向周围搜索即可。

不知道对不对,有时间会去尝试实现一个简易版的消消乐。

不知道是几年后去实现(逃

END

0 0
原创粉丝点击