java - Given 2D array row/column index, how to formulate the Sudoku "sub-grid" index? -
i'm working on implementing sudoku grid in java, , last thing i've been unable reduce below logic mathematical formula.
this follow question: how sudoku 2d-array index, given "sub-grid" , "cell-in-the-sub-grid" indexes?
here function, works well:
public static final int getgridindexforcell(int rowidx_0to8, int colidx_0to8) { int idx = -1; if(rowidx_0to8 < 3) { //grid-row 1 idx = ((colidx_0to8 < 3) ? 0 : ((colidx_0to8 < 6) ? 1 : 2)); } else if(rowidx_0to8 < 6) { //grid-row 2 idx = ((colidx_0to8 < 3) ? 3 : ((colidx_0to8 < 6) ? 4 : 5)); } else { //grid-row 3 idx = ((colidx_0to8 < 3) ? 6 : ((colidx_0to8 < 6) ? 7 : 8)); } return idx; } it returns "grid index", based on provided row , column indexes of underlying two-dimensional array.
here's mean grid (i call entire thing "board"):
| | 0 | 1 | 2 | | ------------------------- | | 3 | 4 | 5 | | ------------------------- | | 6 | 7 | 8 | | where each grid has 9 cells, indexed this
0 1 2 3 4 5 6 7 8 i'd appreciate annotated answers, as possible, because i'm not getting yet.
so formula want is
grid_index = (column / 3) + (row / 3) * 3 = column / 3 + row - row % 3 that grid_index grows 1 each time columns grow 3 , grid_index grows 3 each time row grows 3. again division done integer.
Comments
Post a Comment