2013-04-29 195 views
0

填充算法我已经广泛地搜查了论坛,并没有颇为涵盖这一点。我正在寻找在枚举类型的数组上实质上称为3D Flood Fill算法的算法。我不想改变数组元素的“颜色”,而是想改变枚举类型。这是我迄今为止,如果你认为这会起作用或者你有什么建议,你们可以让我知道吗?3D洪水在3D阵列(爪哇)

/* 
    * CellType is my enum type. BOUNDRY_BOX enum type is type that I line the whole 3D array with. So the 
    * whole inside surface of the 3D box is filled with CellType.BOUNDRY_BOX. 
    **/ 
public void fillAllVoidCells(CellType[][][] grid, CellType targetType, CellType replacementType, int x, int y, int z) 
{ 
    if ((grid[x][y][z] != targetType) && grid[x][y][z] != CellType.BOUNDRY_BOX) 
    { 
     break; 
    } 
    else 
    { 
     grid[x][y][z] = replacementType; 

     fillAllVoidCells(grid, targetType, replacementType, x + 1, y, z); // right 
     fillAllVoidCells(grid, targetType, replacementType, x - 1, y, z); // left 
     fillAllVoidCells(grid, targetType, replacementType, x, y + 1, z); // in front 
     fillAllVoidCells(grid, targetType, replacementType, x, y - 1, z); // behind 
     fillAllVoidCells(grid, targetType, replacementType, x, y, z + 1); // above 
     fillAllVoidCells(grid, targetType, replacementType, x, y, z - 1); // below 
    } 
} 

回答

0

几件事情:

  • 休息并不意味着你认为它。您应该使用return来离开函数
  • 您可能需要在调用相邻单元的函数之前检查您是否位于您的域的边界(否则它将崩溃)
  • 使用递归进行洪泛填充很棒......仅用于教育目的。使用队列更加高效
  • 一个简单的办法是尝试一下,看看它是否有效!
+0

感谢你的帮助,这是非常有用的。我已经取得了很大的进展,请您在这里提出后续问题? http://stackoverflow.com/questions/16294856/using-recursion-for-3d-array-manipulation-causing-stackoverflow-not-infinite – 2013-04-30 07:54:16