コード例 #1
0
ファイル: board.go プロジェクト: oijazsh/go-sudoku
// 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
ファイル: board.go プロジェクト: xnattack/GCSolutions
// 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
}
コード例 #3
0
ファイル: board.go プロジェクト: xnattack/GCSolutions
// SolveAndRank solves the given sudoku puzzle and returns whether it is
// successful and its rank.
func (g *Grid) SolveAndRank() (bool, string) {
	root := dlx.NewRoot()
	solution := make(chan int, numCells)
	solutions, givens := 0, 0
	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)
				givens++
			}
		}
	}

	go func() {
		defer close(solution)
		solutions = dlx.SolveAll(root, solution)
	}()

	for poss := range solution {
		if givens != numCells {
			filled := g.togglePossibility(poss)
			if !filled {
				givens--
			} else {
				givens++
			}
		}
	}
	message := rankMessage(rankGivens(givens, solutions))
	return solutions > 0, message
}