Пример #1
0
func (b *Board) genSparseMatrix(root *dlx.Node) {
	sideLen := b.sideLen()
	nCells := sideLen * sideLen

	nConstraints := nCells * 4
	headers := make([]*dlx.Node, nConstraints)
	row := make([]*dlx.Node, 4)

	rowOffset := sideLen * sideLen
	colOffset := rowOffset * 2
	blkOffset := rowOffset * 3

	for i := range headers {
		headers[i] = dlx.AddHeader(root)
	}

	for r := 0; r < sideLen; r++ {
		for c := 0; c < sideLen; c++ {
			for v := 0; v < sideLen; v++ {
				poss := b.possibility(r, c, v)
				row[0] = dlx.AddNode(poss, headers[r*sideLen+c])
				row[1] = dlx.AddNode(poss, headers[rowOffset+r*sideLen+v])
				row[2] = dlx.AddNode(poss, headers[colOffset+c*sideLen+v])
				row[3] = dlx.AddNode(poss, headers[blkOffset+
					(r/b.blockRows*b.blockCols+c/b.blockCols)*sideLen+v])

				err := dlx.BuildRow(row)
				if err != nil {
					log.Fatal("sudoku: could not attach a row")
				}
			}
		}
	}
}
Пример #2
0
func genSparseMatrix(root *dlx.Node) {
	var headers [maxCols]*dlx.Node
	row := make([]*dlx.Node, 4)

	for i := range headers {
		headers[i] = dlx.AddHeader(root)
	}

	for r := 0; r < gridLen; r++ {
		for c := 0; c < gridLen; c++ {
			for v := 0; v < gridLen; v++ {
				poss := possibility(r, c, v)
				row[0] = dlx.AddNode(poss, headers[r*gridLen+c])
				row[1] = dlx.AddNode(poss, headers[rowConstraintsOff+r*gridLen+v])
				row[2] = dlx.AddNode(poss, headers[colConstraintsOff+c*gridLen+v])
				row[3] = dlx.AddNode(poss, headers[blkConstraintsOff+
					(r/blockLen+c/blockLen*blockLen)*gridLen+v])

				dlx.BuildRow(row)
			}
		}
	}
}