【问题标题】:Recursive Flood Fill - Checking Boundries递归洪水填充 - 检查边界
【发布时间】:2012-12-19 21:01:16
【问题描述】:

我在矩阵中有一个合法邻居的递归洪水填充(合法邻居是具有相同颜色的邻居),洪水没有填充数组中的所有合法邻居。 我用于测试的板是:

int[][] map={{4,0,0,0},
             {0,4,0,0},
             {0,4,0,0},
             {0,4,0,0}};

   fill(map,1,1,9,4);// calling to function.

输出是:

4000
0900
0900
0900

编辑 如果我将地图更改为:

int[][] map={{4,0,0,0},
         {4,4,0,0},
         {0,4,0,0},
         {0,4,0,0}};

输出将是:

4000
4900
0900
0900

剩下的两个 4 数字也需要填写。 我的递归函数是:

public static void fill(int[][] map, int row, int col, int color,int oldColor) 

   {

System.out.println("row is: "+row+"col is:"+col);
if ((row <= 0) || (row >= map.length) || (col <= 0) || (col >= map.length) ) return; 

if(map[row][col]==color)
        return;

if(map[row][col]==oldColor)
    {
        map[row][col]=color;
    }
if(col+1<=map.length)
      fill(map, col+1, row,color,oldColor);
 if((col-1)<=0) 
      fill(map,col-1, row,color,oldColor);

  if(row+1<=map.length)
      fill(map, col, row+1,color,oldColor);
  if((row-1)<=0)
      fill(map, col, row-1,color,oldColor);

   } 

更改代码

public static void fill(int[][] map, int row, int col, int color,int oldColor) {
    System.out.println("row is: "+row+"col is:"+col);
if ((row < 0) || (row > map.length) || (col < 0) || (col > map.length) || map[row]                        [col]!=oldColor ) return; 

if(map[row][col]==color)
        return;

if(map[row][col]==oldColor)
    {
        map[row][col]=color;
    }

fill(map, col, row-1,color,oldColor);
fill(map, col+1, row,color,oldColor);
    fill(map, col, row+1,color,oldColor);
    fill(map,col-1, row,color,oldColor);
  }

现在的输出是:

9000
9900
0900
0400

【问题讨论】:

  • 看起来正确,您期望什么输出?如果您希望 (0,0) 处的 4 也被填充,那么它不会被填充,因为您的算法只计算直接相邻的单元格,而不是对角线上的相邻单元格。
  • 好的,如果你可以看到第 1 行= 我会得到 stackoverflow.. 这是适合它的条件,不是吗?

标签: java recursion flood-fill


【解决方案1】:

你有几个错误。首先,您的守卫排除了第 0 行和第 0 列,所以这就是您没有得到预期结果的原因之一。

现在,修复您将遇到堆栈溢出的问题,因为您将尝试填充所有邻居,无论它们具有哪种颜色。这意味着您将永远访问颜色为 0 的所有单元格。您只想填充具有 oldColor 的邻居。

最后,您的方法需要参数 row, column,但您使用 column, row 递归调用它,因此您切换每个堆栈级别的索引。

修正了您可以获得一个更简单的方法而无需保护 if。如果您希望行的长度不同,则需要再次添加防护。

显示一个独立的示例,在填充它们之前和之后打印地图。

public class FloodFill {

  static int[][] map1 ={{4,0,0,0}, {4,4,4,4}, {0,4,0,4}, {0,4,0,0}};
  static int[][] map2 ={{0,4,4,4}, {0,4,0,4}, {0,4,0,4}, {9,9,9,4}};

  public static void fill(int[][] map, int row, int col, int color, int oldColor) {
    if (map[row][col] == oldColor) {
      map[row][col] = color;
      if (col + 1 < map[row].length)
        fill(map, row, col + 1, color, oldColor);           
      if (col > 0)
        fill(map, row, col - 1, color, oldColor);           
      if (row + 1 < map.length)
        fill(map, row + 1, col, color, oldColor);
      if (row > 0)
        fill(map, row - 1, col, color, oldColor);
    }
  }

  public static void main(String[] args) {
    floodfill(map1);
    floodfill(map2);
  }

  private static void floodfill(int[][] map) {
    show(map, "Initial");
    fill(map, 1, 1, 9, 4);
    show(map, "Filled");
  }

  private static void show(int[][] map, String label) {
    System.out.println(label);
    for (int[] row : map) {
      for (int val : row) {
        System.out.print(val + " ");
      }
      System.out.println();
    }
  }
}

另一种带有保护的填充,然后还可以处理不同长度的行。

public static void fill2(int[][] map, int row, int col, int color, int oldColor) {
  if (row < 0 || row >= map.length || col < 0 || col >= map[row].length) 
    return;
  if (map[row][col] == oldColor) {
    map[row][col] = color;
    fill2(map, row, col + 1, color, oldColor);          
    fill2(map, row, col - 1, color, oldColor);          
    fill2(map, row + 1, col, color, oldColor);
    fill2(map, row - 1, col, color, oldColor);
  }
}

【讨论】:

  • @nullix 添加了一个 SSCCE,据我所知,它适用于您的输入。
【解决方案2】:

这不是最佳答案,但我无法删除我的提交。

public class Fill
{

    public static void fill(int[][] map, int col, int row, int color,int oldColor) 
    {

        System.out.println("row is: "+row+"col is:"+col);
        if ((row <= 0) || (row >= map.length) || (col <= 0) || (col >= map.length) ) return; 

        if(map[row][col]==color)
            return;

        if(map[row][col]==oldColor)
        {
            map[row][col]=color;
        }

        if(col+1<=map.length) {
            fill(map, col+1, row,color,oldColor);
        }

        if((col-1)<=0) { 
            fill(map,col-1, row,color,oldColor);
        }

        if(row+1<=map.length) {
            fill(map, col, row+1,color,oldColor);
        }

        if((row-1)<=0) {
            fill(map, col, row-1,color,oldColor);
        }

    } 


    public static void main(String pArgs[])
    {
        int[][] map={{4,0,0,0},
             {0,4,0,0},
             {0,4,0,0},
             {0,4,0,0}};

        printMap(map);
        fill(map,1,1,9,4);// calling to function.
        printMap(map);
    }

    static void printMap(int[][] map)
    {
        for (int i=0; i < 4; i++) {
            System.out.print("{");
            for (int j=0; j<4; j++) {
                System.out.print( map[i][j] + "," );
            }
            System.out.println("}");
        }
    }
}

【讨论】:

    猜你喜欢
    • 2014-02-16
    • 1970-01-01
    • 2021-12-28
    • 1970-01-01
    • 2019-10-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多