如何获得数独二维数组索引,赋予其“分格”和“细胞内式分格”指标?(How to get the Su

2019-10-19 13:09发布

我工作的一个数独的事情,我有麻烦减少其中的一部分。

我设计一个SudokuBoard对象具有一个基础2D字节数组,以及三种不同的观点,即阵列的:行单位,列单位和网格单元。 网格是九由九块,索引是这样的:

        GRID INDEX

        |         |
   0    |    1    |    2
        |         |
 -------------------------
        |         |
   3    |    4    |    5
        |         |
 -------------------------
        |         |
   6    |    7    |    8
        |         |

其中每个网格有九个单元,收录这样

CELL INDEX

   0 1 2
   3 4 5
   6 7 8

这里是该电路板的2D阵列坐标:

                    2D-ARRAY COORDINATE

[0,0] [0,1] [0,2]  |  [0,3] [0,4] [0,5]  |  [0,6] [0,7] [0,8]
[1,0] [1,1] [1,2]  |  [1,3] [1,4] [1,5]  |  [1,6] [1,7] [1,8]
[2,0] [2,1] [2,2]  |  [2,3] [2,4] [2,5]  |  [2,6] [2,7] [2,8]
-------------------+---------------------+-------------------
[3,0] [3,1] [3,2]  |  [3,3] [3,4] [3,5]  |  [3,6] [3,7] [3,8]
[4,0] [4,1] [4,2]  |  [4,3] [4,4] [4,5]  |  [4,6] [4,7] [4,8]
[5,0] [5,1] [5,2]  |  [5,3] [5,4] [5,5]  |  [5,6] [5,7] [5,8]
-------------------+---------------------+-------------------
[6,0] [6,1] [6,2]  |  [6,3] [6,4] [6,5]  |  [6,6] [6,7] [6,8]
[7,0] [7,1] [7,2]  |  [7,3] [7,4] [7,5]  |  [7,6] [7,7] [7,8]
[8,0] [8,1] [8,2]  |  [8,3] [8,4] [8,5]  |  [8,6] [8,7] [8,8]

我创建了一个函数来获取数组坐标,考虑到电网和小区索引。 确定列指数脑克星,但我想它是

int colIdx = (cell_idx % 3) + ((grid_idx % 3) * 3);

我也想通了,做决定的行索引,但我不满意多么复杂,这是。

int rowIdx = -1;
if(grid_idx < 3)  {
   rowIdx = ((cell_idx < 3) ? 0
      :  ((cell_idx < 6) ? 1 : 2));
}  else if(grid_idx < 6)  {
   rowIdx = ((cell_idx < 3) ? 3
      :  ((cell_idx < 6) ? 4 : 5));
}  else  {
   rowIdx = ((cell_idx < 3) ? 6
      :  ((cell_idx < 6) ? 7 : 8));
}

如果任何人有关于如何减少这一点,最好的工科数学公式的想法,我将不胜感激。

以下是一个演示其工作程序。 谢谢你的帮助。

  import  java.util.Arrays;
/**
   <P>{@code SudokuGridUnitCoordinates}</P>
 **/
public class SudokuGridUnitCoordinates  {
   public static final void main(String[] idxZeroGridIdx_0to8)  {
     int gridIdx = -1;
     try  {
        gridIdx = Integer.parseInt(idxZeroGridIdx_0to8[0]);
        if(gridIdx < 0  ||  gridIdx > 8)  {
           throw  new IllegalArgumentException();
        }
     }  catch(IllegalArgumentException | ArrayIndexOutOfBoundsException  x)  {
        throw  new IllegalArgumentException("The first element in idxZeroGridIdx_0to8 must be a digit zero through 8: " + Arrays.toString(idxZeroGridIdx_0to8));
     }

     printCellCoordinates(gridIdx, 0);
     printCellCoordinates(gridIdx, 1);
     printCellCoordinates(gridIdx, 2);
     printCellCoordinates(gridIdx, 3);
     printCellCoordinates(gridIdx, 4);
     printCellCoordinates(gridIdx, 5);
     printCellCoordinates(gridIdx, 6);
     printCellCoordinates(gridIdx, 7);
     printCellCoordinates(gridIdx, 8);
  }
  private static final void printCellCoordinates(int grid_idx, int cell_idx)  {
     int rowIdx = -1;
     if(grid_idx < 3)  {
        rowIdx = ((cell_idx < 3) ? 0
           :  ((cell_idx < 6) ? 1 : 2));
     }  else if(grid_idx < 6)  {
        rowIdx = ((cell_idx < 3) ? 3
           :  ((cell_idx < 6) ? 4 : 5));
     }  else  {
        rowIdx = ((cell_idx < 3) ? 6
           :  ((cell_idx < 6) ? 7 : 8));
     }
     int colIdx = (cell_idx % 3) + ((grid_idx % 3) * 3);

     System.out.println("grid=" + grid_idx + ", cell=" + cell_idx + " --> sudoku2DGridArray[" + rowIdx + ", " + colIdx + "]");
  }
}

Answer 1:

关于什么

rowIdx = 3*(grid_idx/3) + cell_idx/3

所有的刻度是整数除法



Answer 2:

所有的这一块?:运营商只是grid_idx - grid_idx % 3 + cell_idx / 3



文章来源: How to get the Sudoku 2D-array index, given its “sub-grid” and “cell-in-the-sub-grid” indexes?