Beispiel #1
0
// Reference implementation of a Sudoku solver
// https://en.wikipedia.org/wiki/Sudoku_solving_algorithms#Backtracking
func (s *Solver) backtrack(b *puzzle.Board) (*puzzle.Board, bool) {
	if b.Solved() {
		return b, true
	}

	for row := 0; row < 9; row++ {
		for col := 0; col < 9; col++ {
			if b.ValueAt(row, col) == 0 {
				for i := 1; i < 10; i++ {
					if b.Allowed(row, col, i) {
						if bb, s := s.backtrack(b.SetAndCopy(row, col, i)); s {
							return bb, s
						}
					}
				}

				// None of the numbers [1,9] can be inserted in this empty cell
				// so the board is unsolvable
				return b, false
			}
		}
	}

	return b, false
}