Sudoku Pseudo code

advertisement
Sudoku Pseudo code
1. decide the prototype
2. verbalize the solution in terms of a recursive
call to the prototype
3. write the code for the recursion being careful
NOT to mentally trace the recursive call
4. make sure you have base cases handled
5. make sure you USE any return values
Sudoku Pseudo code
1. decide the prototype
bool hasSolution() returns a true if the
partial grid given you can be completed AND
changes the grid to reflect a solution
2. verbalize the solution in terms of a recursive
call to the prototype
Verbalize the solution making sure you
use hasSolution in your solution
• If the grid is complete, you have your solution
• Otherwise, find ONE empty cell.
• Try each possibility for the empty cell. Insert
that value into the grid If hasSolution returns
true for any possible value return true
• otherwise remove your bad value from the
cell and return false
check to see if all the cells have been filled in
already. If so, return true.
Find an unfilled cell. Call i,j the location of the
unfilled cell.
For (choice=1 to 9)
{ if isOK(i,j,choice)
{ change grid[i][j] = choice
if hasSolution() return true.
grid[i][j]=0 //clean up after yourself
}
}
return false
Download