Пример #1
0
// Solve solves the given sudoku puzzle and returns whether it was successful
func (b *Board) Solve() bool {
	root := dlx.NewRoot()
	sideLen := b.sideLen()
	solution := make(chan int, sideLen*sideLen)
	solved := false

	b.genSparseMatrix(root)
	for r, row := range b.grid {
		for c, v := range row {
			if v != 0 {
				n := dlx.Find(b.possibility(r, c, v-1), root)
				if n == nil {
					return false
				}
				dlx.Cover(n)
			}
		}
	}

	func() {
		defer close(solution)
		solved = dlx.Solve(root, solution)
	}()

	for poss := range solution {
		b.togglePossibility(poss)
	}
	return solved
}
Пример #2
0
// Solve solves the given sudoku puzzle and returns whether it was successful
func (g *Grid) Solve() bool {
	root := dlx.NewRoot()
	solution := make(chan int, numCells)
	solved := false

	genSparseMatrix(root)
	for r, row := range g {
		for c, v := range row {
			if v != 0 {
				n := dlx.Find(possibility(r, c, v-1), root)
				if n == nil {
					return false
				}
				dlx.Cover(n)
			}
		}
	}

	func() {
		defer close(solution)
		solved = dlx.Solve(root, solution)
	}()

	for poss := range solution {
		g.togglePossibility(poss)
	}
	return solved
}