我正在使用 Golang gofd 包,提供约束满足解决方案来解决数独问题。我创建了以下内容package mainimport ( "bitbucket.org/gofd/gofd/core" "bitbucket.org/gofd/gofd/propagator" "bitbucket.org/gofd/gofd/labeling" "encoding/json" "io/ioutil" "fmt")var ROWS = []string{"A", "B", "C", "D", "E", "F", "G", "H", "I"}var COLS = []int{1,2,3,4,5,6,7,8,9}var SQUARE1 = []string{ "A!", "A2", "A3", "B1", "B2", "B3", "C1", "C2", "C3"}var SQUARE2 = []string{ "A4", "A5", "A6", "B4", "B5", "B6", "C4", "C5", "C6"}var SQUARE3 = []string{ "A7", "A8", "A9", "B7", "B8", "B9", "C7", "C8", "C9"}var SQUARE4 = []string{ "D!", "D2", "D3", "E1", "E2", "E3", "F1", "F2", "F3"}var SQUARE5 = []string{ "D4", "D5", "D6", "E4", "E5", "E6", "F4", "F5", "F6"}var SQUARE6 = []string{ "D7", "D8", "D9", "E7", "E8", "E9", "F7", "F8", "F9"}var SQUARE7 = []string{ "G!", "G2", "G3", "H1", "H2", "H3", "I1", "I2", "I3"}var SQUARE8 = []string{ "G4", "G5", "G6", "H4", "H5", "H6", "I4", "I5", "I6"}var SQUARE9 = []string{ "G7", "G8", "G9", "H7", "H8", "H9", "I7", "I8", "I9"}var SQUARES = [][]string{ SQUARE1, SQUARE2, SQUARE3, SQUARE4, SQUARE5, SQUARE6, SQUARE7, SQUARE8, SQUARE9}type Grid struct { Grid map[string]int}func create() Grid { grid := map[string]int{} return Grid{grid}}func (g *Grid) load(filename string) { body, _ := ioutil.ReadFile(filename) err := json.Unmarshal(body, g) if err != nil { fmt.Println(err) }}func main() { store := core.CreateStore() n := 9 sudoku := map[string]core.VarId{} for _, row := range ROWS { for _, col := range COLS { varname := fmt.Sprintf("%s%d", row, col) sudoku[varname] = core.CreateIntVarFromTo(varname, store, 1, n) } }
1 回答
慕哥6287543
TA贡献1831条经验 获得超10个赞
首先,我必须承认我不是 Go 程序员,但我可能会更多地研究 Go 和 gofd 包(我不知道;感谢您的提示)。
我不知道为什么“total”使模型表现得如此,但为什么你用“!”命名一些单元格。而不是“1”,例如“A!”、“D!”和“G!”?
当我替换“!” 使用“1”,显示正确且唯一的解决方案(没有“total”变量):
consistent: true
solutionFound: true
6 3 2 8 9 7 1 5 4
4 1 7 6 2 5 8 9 3
8 5 9 4 3 1 2 7 6
5 4 3 2 7 9 6 1 8
7 2 6 3 1 8 9 4 5
9 8 1 5 4 6 7 3 2
3 7 5 9 6 2 4 8 1
1 6 4 7 8 3 5 2 9
2 9 8 1 5 4 3 6 7
我添加了以下内容来检查解决方案的唯一性:
query2 := labeling.CreateSearchAllQuery()
solutionFound2 := labeling.Labeling(store, query2, labeling.SmallestDomainFirst, labeling.InDomainMin)
if solutionFound2 {
println("The Sudoku problem has", len(query2.GetResultSet()), "solutions.")
}
- 1 回答
- 0 关注
- 163 浏览
添加回答
举报
0/150
提交
取消