0

我已经广泛搜索了论坛,没有什么能完全涵盖这一点。我正在寻找对枚举类型的数组进行本质上称为 3D Flood Fill 算法的操作。我不想更改数组元素的“颜色”,而是更改其中的枚举类型。这就是我到目前为止所拥有的,如果你认为这会奏效或者你有什么建议,你们能告诉我吗?

 /*
  * 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
    }
 }
4

1 回答 1

0

几件事:

  • break 并不意味着你认为它做了什么。您应该使用 return 离开该功能
  • 在调用相邻单元格的函数之前,您可能需要检查您是否位于域的边界(否则,它将崩溃)
  • 使用递归进行洪水填充非常棒......仅用于教育目的。使用队列效率更高
  • 一个简单的事情就是尝试一下,看看它是否有效!
于 2013-04-30T04:07:50.970 回答