我正在使用 Golang gofd 包,提供约束满足解决方案来解决数独问题。我创建了以下内容
package main
import (
"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)
}
}
慕哥6287543
相关分类