Note that there are some explanatory texts on larger screens.

plurals
  1. POBrute force sudoku solver algorithm in Java problem
    primarykey
    data
    text
    <p>Everything seems to work fine in the algorithm besides the solve method. When it executes the program using a solvable Sudoku board, it says that it cannot be solved. I've tried everything I can think of in the solve method. I've tried debugging and it fails after the first row is tested. Any suggestions? Here is the full code so far:</p> <p><pre><code> public class SudokuSolver {</p> <pre><code> public static void initializeGrid(int[][] grid, int[][] puzzle) { for (int r = 0; r &lt; puzzle.length; r++) { for (int c = 0; c &lt; puzzle[0].length; c++) { grid [r][c] = puzzle [r][c]; } } } public static void displayGrid(int[][] grid) { for (int r = 0; r &lt; grid.length; r++) { if (r % 3 == 0) { System.out.println("+---+---+---+"); } for (int c = 0; c &lt; grid[r].length; c++) { if (c % 3 == 0) { System.out.print("|"); } displayDigits(r, c, grid); </code></pre> <p>} System.out.print("|"); System.out.println(); } System.out.println("+---+---+---+"); }</p> <p>if (grid[r][c] == 0) { System.out.print(' '); } else { System.out.print(grid[r][c]); } } public static int getEmptyCells(int[][] grid, int[][] emptyCells) { int i = 0; int numEmptyCells = 0; for (int r = 0; r &lt; grid.length; r++) { for (int c = 0; c &lt; grid[r].length; c++) { if (grid[r][c] == 0) { emptyCells[i][0] = r; emptyCells[i][1] = c; numEmptyCells++; i++; } } } return numEmptyCells; }</p> <p>private static boolean hasNoDuplicates(int[] digitsList) { for (int j = 0; j &lt; digitsList.length; j++) { for (int k = j + 1; k &lt; digitsList.length; k++) { if (digitsList[j] == digitsList[k] &amp;&amp; digitsList[j] != 0) return false; } } return true; }</p> <p>private static boolean checkCurrentRow(int[][] grid, int currentRow) {<br> int[] digitsList = new int[grid.length]; for (int c = 0; c &lt; digitsList.length; c++) { digitsList[c] = grid[currentRow][c]; } if (hasNoDuplicates(digitsList)) { return true; } return false; }</p> <p>private static boolean checkCurrentCol(int[][] grid, int currentCol) { int[] digitsList = new int[grid.length]; for (int i = 0; i &lt; digitsList.length; i++) { digitsList[i] = grid[i][currentCol]; } if (hasNoDuplicates(digitsList)) { return true; } return false; }</p> <p>private static boolean checkCurrentRegion(int[][] grid, int currentRow, int currentCol) {<br> int[] digitsList = new int[grid.length]; currentRow = (currentRow / 3) * 3; currentCol = (currentCol / 3) * 3; int i = 0; for (int r = 0; r &lt; 3; r++) { for (int c = 0; c &lt; 3; c++) { digitsList[i] = grid[currentRow + r][currentCol + c]; i++; } } if (hasNoDuplicates(digitsList)) { return true; } return false; }</p> <p>public static boolean isConsistent(int[][] grid, int currentRow, int currentCol) { if (checkCurrentRow(grid, currentRow) &amp;&amp; checkCurrentCol(grid, currentCol) &amp;&amp; checkCurrentRegion(grid, currentRow, currentCol)) { return true; } return false; }</p> <p>public static boolean solvePuzzle(int[][] grid, int[][] emptyCells, int numEmptyCells) { int i = 0; int j = 0; int currentCellDigit = grid[emptyCells[i][0]][emptyCells[i][1]]; while (j &lt; numEmptyCells) { if (currentCellDigit != 9) { currentCellDigit++; grid[emptyCells[i][0]][emptyCells[i][1]] = currentCellDigit; if (isConsistent(grid, emptyCells[i][0], emptyCells[i][1])) { grid[emptyCells[i][0]][emptyCells[i][1]] = currentCellDigit; i++; j++; } else { grid[emptyCells[i][0]][emptyCells[i][1]] = currentCellDigit - 1; } } else { currentCellDigit = 0; currentCellDigit = grid[emptyCells[i][0]][emptyCells[i][1]]; i--; j--; if (j &lt; 0) { return false; } } }</p> <p>return true;<br> }</p> <p>public static void main(String[] args) {</p> <p>final int SIZE = 9; int[][] puzzle = { {0,2,9,0,0,3,0,0,5}, {5,0,7,0,0,0,0,9,0}, {6,0,0,0,0,9,4,2,0}, {3,0,2,0,0,4,0,0,0}, {0,0,5,0,3,0,7,0,0}, {0,0,0,5,0,0,6,0,2}, {0,9,8,4,0,0,0,0,3}, {0,3,0,0,0,0,1,0,6}, {2,0,0,3,0,0,9,4,0} };</p> <p>int[][] grid = new int[SIZE][SIZE]; int[][] emptyCellsList = new int[SIZE*SIZE][2]; int numEmptyCells = 0;</p> initializeGrid(grid, puzzle); numEmptyCells = getEmptyCells(grid, emptyCellsList); System.out.println("The puzzle:"); displayGrid(puzzle); if (solvePuzzle(grid, emptyCellsList, numEmptyCells)) { System.out.println("has been solved:"); displayGrid(grid); } else { System.out.println("cannot be solved!"); } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload