Java 递归洪水填充-检查边界

Java 递归洪水填充-检查边界,java,recursion,flood-fill,Java,Recursion,Flood Fill,我在矩阵中有一个合法邻居的递归泛洪填充(合法邻居是具有相同颜色的邻居),泛洪填充并没有填充数组中的所有合法邻居。 我用于测试的板是: 输出为: 编辑 如果我将地图更改为: int[][] map={{4,0,0,0}, {4,4,0,0}, {0,4,0,0}, {0,4,0,0}}; 输出将为: 4000 4900 0900 0900 剩下的两个4号也需要填写。 我的递归函数是: 这不是最好的答案,但我不能删除我的提交 publi

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

输出为:

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

int[][] map={{4,0,0,0},
         {4,4,0,0},
         {0,4,0,0},
         {0,4,0,0}};
输出将为:

4000
4900
0900
0900
剩下的两个4号也需要填写。 我的递归函数是:


这不是最好的答案,但我不能删除我的提交

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("}");
        }
    }
}
公共类填充
{
公共静态空白填充(int[][]映射、int列、int行、int颜色、int oldColor)
{
System.out.println(“行为:“+row+”列为:“+col”);
if((row=map.length)|(col=map.length))返回;
if(映射[行][列]==颜色)
返回;
if(映射[行][列]==oldColor)
{
地图[行][列]=颜色;
}

如果(col+1您有几个错误。首先,您的保护不包括第0行和第0列,因此这是您无法获得预期结果的原因之一

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

最后,您的方法需要参数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();
    }
  }
}
公共级洪水填筑{
静态int[]map1={{4,0,0,0},{4,4,4},{0,4,0,4},{0,4,0,0};
静态int[]map2={0,4,4,4},{0,4,0,4},{0,4,0,4},{9,9,4};
公共静态空白填充(int[][]映射、int行、int列、int颜色、int oldColor){
if(映射[行][列]==oldColor){
地图[行][列]=颜色;
if(列+10)
填充(地图、行、列1、颜色、旧颜色);
if(行+1<映射长度)
填充(地图、行+1、列、颜色、旧颜色);
如果(行>0)
填充(地图,第1行,列,颜色,旧颜色);
}
}
公共静态void main(字符串[]args){
填海工程(map1);
填海工程(map2);
}
专用静态空隙洪水填充(int[][]地图){
显示(地图,“首字母”);
填充(地图,1,1,9,4);
显示(地图,“填写”);
}
私有静态无效显示(int[][]映射,字符串标签){
系统输出打印项次(标签);
对于(int[]行:映射){
for(int val:row){
系统输出打印(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);
  }
}
publicstaticvoidfill2(int[][]映射,int行,int列,int颜色,int oldColor){
如果(行<0 | |行>=map.length | |列<0 | |列>=map[row].length)
返回;
if(映射[行][列]==oldColor){
地图[行][列]=颜色;
填充2(地图、行、列+1、颜色、旧颜色);
填充2(地图、行、列-1、颜色、旧颜色);
填充2(地图,行+1,列,颜色,旧颜色);
fill2(地图,第1行,列,颜色,oldColor);
}
}

看起来不错,您期望的输出是什么?如果您期望的是4(0,0)要同时填充,它将不会被填充,因为您的算法只计算直接相邻的单元格,而不是对角线上的相邻单元格。好的,如果您看到行1=我得到stackoverflow..这是正确的条件,不是吗?@nullix添加了一个SSCCE,据我所知,它可以与您的输入一起工作。
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
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("}");
        }
    }
}
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);
  }
}