コード例 #1
0
ファイル: main.go プロジェクト: ng-vu/Exercise
func main() {
	var input = [9][9]int{
		{0, 7, 8, 1, 0, 0, 0, 2, 0},
		{1, 0, 0, 0, 6, 2, 0, 0, 3},
		{5, 0, 0, 0, 9, 0, 0, 0, 0},
		{8, 0, 0, 0, 0, 0, 4, 0, 6},
		{0, 6, 1, 0, 7, 0, 0, 9, 0},
		{0, 9, 0, 0, 0, 0, 3, 0, 0},
		{0, 0, 0, 5, 0, 4, 2, 0, 7},
		{6, 0, 0, 0, 8, 0, 0, 3, 0},
		{0, 5, 0, 7, 0, 0, 9, 0, 0}}
	_ = input
	var input2 = [9][9]int{
		{8, 0, 0, 0, 0, 1, 0, 0, 0},
		{0, 0, 0, 0, 0, 0, 0, 0, 0},
		{0, 0, 0, 7, 0, 0, 0, 5, 0},
		{0, 5, 0, 0, 0, 0, 0, 0, 0},
		{0, 0, 0, 0, 0, 9, 0, 0, 0},
		{0, 0, 0, 0, 0, 0, 0, 0, 0},
		{9, 0, 7, 0, 0, 0, 0, 0, 0},
		{0, 0, 0, 8, 0, 0, 3, 2, 0},
		{2, 0, 0, 0, 0, 0, 0, 0, 0}}
	_ = input2
	var output = [9][9]int{
		{3, 7, 8, 1, 4, 5, 6, 2, 9},
		{1, 4, 9, 8, 6, 2, 7, 5, 3},
		{5, 2, 6, 3, 9, 7, 1, 4, 8},
		{8, 3, 5, 9, 2, 1, 4, 7, 6},
		{2, 6, 1, 4, 7, 3, 8, 9, 5},
		{7, 9, 4, 6, 5, 8, 3, 1, 2},
		{9, 8, 3, 5, 1, 4, 2, 6, 7},
		{6, 1, 7, 2, 8, 9, 5, 3, 4},
		{4, 5, 2, 7, 3, 6, 9, 8, 1}}

	// Init the input sudoku
	board := sudoku.InitSudoku(input)
	_ = board
	// Init the solution sudoku
	solution := sudoku.InitSudoku(output)
	_ = solution
	fmt.Println(board)

	var solution_list []sudoku.Sudoku
	sudoku.GetSolutions(&board, &solution_list)
	fmt.Println(sudoku.CheckSingle(&board))
	var generate = sudoku.GenerateSudoku()
	var sol_num, _ = sudoku.CheckSingle(&generate)
	fmt.Println("Sudoku generated with number of solution:", sol_num, "unassign block: ", sudoku.CountEmptyBlockWithMultiVal(generate))
	fmt.Println(generate)

	sudoku.BacktrackSolve(&generate)
	fmt.Println(generate)
}
コード例 #2
0
ファイル: sudoku_test.go プロジェクト: ng-vu/Exercise
func TestBacktrackSolve(t *testing.T) {
	sample_sudoku := sudoku.InitSudoku(sample_board)
	sample_sudoku_solution := sudoku.InitSudoku(sample_board_solution)
	result := sudoku.BacktrackSolve(&sample_sudoku)
	if result == false {
		t.Errorf("BacktrackSolve dont give solution")
	}
	for i, r := range sample_sudoku {
		for j, a := range r {
			if sample_sudoku_solution[i][j].Val != a.Val {
				t.Errorf("BacktrackSolve give wrong solution at block %v", 9*i+j)
			}
		}
	}
}
コード例 #3
0
ファイル: sudoku_test.go プロジェクト: ng-vu/Exercise
func BenchmarkBacktrackSolve(b *testing.B) {
	for i := 0; i < b.N; i++ {
		sample_sudoku := sudoku.InitSudoku(sample_board)
		sudoku.BacktrackSolve(&sample_sudoku)
	}
}