public boolean solveSudoku() {
int row, col;
for (Map.Entry<Pair<Integer, Integer>, List<Integer>> entry : amap.entrySet()) { // iterate through map (amap)
row = entry.getKey().getKey();
col = entry.getKey().getValue();
for (Integer num : entry.getValue()) {
if (isSafe(row, col, num)) {
dpuzzle[row][col] = num;
if (solveSudoku()) { // recursive call?
return true;
} else {
dpuzzle[row][col] = 0; // replace it
}
}
}
return false;
}
return true;
}
I have implemented a Sudoku solver using the byGroup method first to eliminate the impossible candidate for each empty cell and store them to a map, the key of the Map is the (i,j)of the grid, and the arraylist in the value of Map is the possible candidates, and the solveSudoku() supposed to recursively call itself to solve the puzzle using the possible candidate from the ArrayList. However it spit out :
Exception in thread "main" java.lang.StackOverflowError
at soduku.BacktrackSolver.solveSudoku(BacktrackSolver.java:113)
at soduku.BacktrackSolver.solveSudoku(BacktrackSolver.java:121)
these are the line
for (Map.Entry<Pair<Integer, Integer>, List<Integer>> entry : amap.entrySet()) {
and
if (solveSudoku()) {
Please enlighten me, if I have made any mistake as this is my first attempt in using Java data structures.