Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Solve algorithm
- func (s *Sudoku) Solve() bool {
- // Find the next pending position in the board
- pending, row, col := s.pendingPositions()
- // If there aren't more pending positions we did
- // and the problem is solved
- if !pending {
- return true
- }
- // If not, let's try to find a solution for the pending position
- // in row and col. We will try with digits from 1 to 9
- for i := 1; i <= 9; i++ {
- // Check is the current digit is valid for the given position
- // This function checks row, column and box validity
- if s.valid(i, row, col) {
- // If is valid assign this value to the given position
- s.grid[row][col] = i
- // Recursive call to Solve method. If the Sudoku
- // is done return true
- if s.Solve() {
- return true
- }
- // If the current value does not fixed the Sudoku we
- // undo the changes (Backtracking comes to scene!!!)
- // and we try with the next one
- s.grid[row][col] = PENDING
- }
- }
- // We don't find any solution for the Sudoku problem
- return false
- }
Add Comment
Please, Sign In to add comment