I had a problem to eliminate candidates in a cell sudoku, I have tried to search for candidates to fill the empty cells like below
for (int y = 0; y < 9; y++) {
for (int x = 0; x < 9; x++)
if(game.getNumber(x, y) == 0){
for(int z = 1; z <=9; z++){
if (CekRow(game, y, z) && CekColumn(game, x, z) && CekRegion(game, x, y, z)){
list[y][x][z-1] = z;
}
}
}
}
then after find empty cell and fill with the candidate, i want to eliminate possible item array like method this enter image description here. i have someone can provide the solution to eliminate array list[y][x][z].
Here's another possible data structure for a Sudoku board using the Java BitSet
class. Each cell on the board has its possibilities tracked by a BitSet
variable called possibilities
.
Also shown is an example eliminateInRow
method which eliminates numbers as possibilities from a row. The method includes a list of columns to skip in the row, to exempt the cells that originally caused us to do the elimination (for example the cells 7 and 9 mentioned by the OP in comments on the original question). There's also another case where if you find 3 cells all containing the possibilities (3,5,9) (for example), then you can eliminate 3,5,9 as possibilities from all other cells in the row (exempting the original 3 cells). This method will deal with both the 2-case and the 3-case, as it uses a List
of integers denoting columns to skip.
import java.util.BitSet;
import java.util.List;
class SudokuCell {
BitSet possibilities;
CellValue value;
SudokuCell() {
// Initially the value of this cell is undefined
value = CellValue.Undefined;
possibilities = new BitSet(9);
// Initially every value is possible
possibilities.set(0, 9);
}
// clears the possibility of 'number' from this cell
void clear(int number) {
possibilities.clear(number - 1);
// If there is only one possibility left
if (possibilities.cardinality() == 1) {
// Set value of the cell to that possibility
int intValue = possibilities.nextSetBit(0);
value = CellValue.fromInteger(intValue);
}
}
// returns the value of this square if it is defined
int getValue() throws Exception {
if (value == CellValue.Undefined) {
throw new Exception("Undefined cell value");
}
return CellValue.toInteger(value);
}
// returns whether 'num' is still possible in this cell
boolean isPossible(int num) {
return possibilities.get(num-1);
}
}
public class SudokuBoard {
SudokuCell[][] sudokuBoard;
// constructor
SudokuBoard() {
sudokuBoard = new SudokuCell[9][9];
for (int row = 0; row < 9; row++) {
for (int col = 0; col < 9; col++) {
sudokuBoard[row][col] = new SudokuCell();
}
}
}
// eliminate the possibility of 'num' from row 'row'
// skipping columns in List skipCols
void eliminateInRow(int row, int num, List<Integer> skipCols) {
for (int col = 0; col < 9; col++) {
if (!skipCols.contains(col)) {
sudokuBoard[row][col].clear(num);
}
}
}
}
Below is the code for the enum
CellValue
, which is optional:
enum CellValue {
Undefined, One, Two, Three, Four, Five, Six, Seven, Eight, Nine;
public static CellValue fromInteger(int x) {
switch (x) {
case 0:
return Undefined;
case 1:
return One;
case 2:
return Two;
case 3:
return Three;
case 4:
return Four;
case 5:
return Five;
case 6:
return Six;
case 7:
return Seven;
case 8:
return Eight;
case 9:
return Nine;
}
return null;
}
public static int toInteger(CellValue value) throws Exception {
switch (value) {
case Undefined:
throw new Exception("Undefined cell value");
case One:
return 1;
case Two:
return 2;
case Three:
return 3;
case Four:
return 4;
case Five:
return 5;
case Six:
return 6;
case Seven:
return 7;
case Eight:
return 8;
case Nine:
return 9;
}
throw new Exception("Undefined Cell Value");
}
}