Note that there are some explanatory texts on larger screens.

plurals
  1. POSudoku recursion problematic while backtraking. (Brute Force)
    primarykey
    data
    text
    <blockquote> <p><strong>Possible Duplicate:</strong><br> <a href="https://stackoverflow.com/questions/9404673/sudoku-solver-in-java-using-backtracking-and-recursion">Sudoku solver in java, using backtracking and recursion</a> </p> </blockquote> <p>I am creating a program that will solve a sudoku using recursion and brute force. My key problem is that I do not understand how I could concievably make it backtrack one it gets stuck.</p> <p>The general algorithm of the program is the following:</p> <ol> <li><p>Find the number of zeros in the sudoku.</p></li> <li><p>In the location of the first 0 (getNextEmpty method does this), insert a number(insertnumber checks to make sure a value complies with sudoku rules and returns true if it does).</p></li> <li><p>Then I make a recursive call, end when there are no more zeroes (n is the number of zeros).</p></li> <li><p>If the program reaches a point that it gets stuck, I must backtrack to change a piece. But how is this possible? </p></li> </ol> <p>The Cell class actually holds the location of the cell to be adjusted in an array of the format [row, column]. It has methods to return the row, column, or smaller grid associated with that cell.</p> <p>I am not asking for hand-holding or all the code, just a nudge in the right direction will suffice as I am legitimately interested in understanding the recursion.</p> <pre><code>public static int[][] getSolution(int[][] grid) { for (int i = 0; i &lt; 9; i++) { System.arraycopy(grid[i], 0, SolveSudoku.grid[i], 0, 9); }// end for int n = getZeroes(); return getSolution(n); }//end getSolution private static int[][] getSolution(int n) { if (n == 0) { return grid; }//end if Cell cell = getNextEmpty(); boolean fits = false; for (int i = 0; i &lt;= 9; i++) { fits = insertNumber(cell, i); if (fits) { break; }else { //I do not understand what I should do here } }//end for return getSolution(n - 1); }//end getSolution </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.
 

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