Guest User

Untitled

a guest
Apr 22nd, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.97 KB | None | 0 0
  1. // Solve algorithm
  2. func (s *Sudoku) Solve() bool {
  3. // Find the next pending position in the board
  4. pending, row, col := s.pendingPositions()
  5. // If there aren't more pending positions we did
  6. // and the problem is solved
  7. if !pending {
  8. return true
  9. }
  10. // If not, let's try to find a solution for the pending position
  11. // in row and col. We will try with digits from 1 to 9
  12. for i := 1; i <= 9; i++ {
  13. // Check is the current digit is valid for the given position
  14. // This function checks row, column and box validity
  15. if s.valid(i, row, col) {
  16. // If is valid assign this value to the given position
  17. s.grid[row][col] = i
  18. // Recursive call to Solve method. If the Sudoku
  19. // is done return true
  20. if s.Solve() {
  21. return true
  22. }
  23. // If the current value does not fixed the Sudoku we
  24. // undo the changes (Backtracking comes to scene!!!)
  25. // and we try with the next one
  26. s.grid[row][col] = PENDING
  27. }
  28. }
  29. // We don't find any solution for the Sudoku problem
  30. return false
  31. }
Add Comment
Please, Sign In to add comment