问题
I'm working on a sudoku thing, and I'm having trouble reducing one part of it.
I'm designing a SudokuBoard
object to have an underlying 2D byte array, and three different views of that array: row-units, column-units and grid units. A grid is the nine-by-nine blocks, indexed like this:
GRID INDEX
| |
0 | 1 | 2
| |
-------------------------
| |
3 | 4 | 5
| |
-------------------------
| |
6 | 7 | 8
| |
Where each grid has nine cells, indexed like this
CELL INDEX
0 1 2
3 4 5
6 7 8
Here is the 2d array coordinates for the board:
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]
I've created a function to get the array-coordinate, given the grid and cell indexes. Determining the column index was a brain-buster, but I figured it out to be
int colIdx = (cell_idx % 3) + ((grid_idx % 3) * 3);
I also figured out do determine the row index, but I am unhappy with how complicated this is.
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));
}
If anyone has ideas on how to reduce this, ideally to a mathmatical formula, I'd appreciate it.
Here's a working application that demonstrates it. Thanks for any help.
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 + "]");
}
}
回答1:
What about
rowIdx = 3*(grid_idx/3) + cell_idx/3
all division being integer divisions
回答2:
That chunk with all the ?:
operators is just grid_idx - grid_idx % 3 + cell_idx / 3
来源:https://stackoverflow.com/questions/22289144/how-to-get-the-sudoku-2d-array-index-given-its-sub-grid-and-cell-in-the-sub