-
Notifications
You must be signed in to change notification settings - Fork 4
Sudoku Solution Algorithm
This solution requires you to have a wrapper method that initializes your helper method (of the same name) to start at the origin (the upper-left corner) of the 2-D array parameter. The wrapper method is called by the Test class (or any jUnit test case that Anthony might create) and is necessary for the magic of recursion to occur.
Your helper method (an overloaded method which also keeps track of a row and a column parameter) is where the Sudoku solver does a majority of the work. Keep in mind that this method is solving a Sudoku puzzle, which is a 9 x 9 array. Also keep in mind that the checkSudoku method that Edo provided designates the literal integer '0' as a blank cell. Remember that in order for recursion to work, there must be at least one parameter that changes with each recursive call. For debugging purposes, you will want to set the printErrors parameter of the checkSudoku method to true when you use it.
- The base case of this method does the following:
If the column has reached its bound
-
Increment the row.
-
Reset the column (to the start of the next row)
-
If the row has reached its bound, return true;
- Then, if the current cell of the Sudoku is blank, do the following in a loop:
-
Assign the current cell of the sudoku to a guess (an integer variable that is passed through the loop).
-
If the guess turns out to be a legal value (by passing all of the rules in the checkSudoku method)
Note: The following is a nested if statement and recursively calls your helper method.
- If the recursive call to the next column in that row is a solution, return true;
Make sure to terminate the for loop.
- Otherwise (if the current cell is filled with a number), return a recursive call to the next cell to see if it is a solution.