Repository navigation
Expand file tree
/
Copy pathsolver.go
More file actions
148 lines (131 loc) · 3.17 KB
/
Copy pathsolver.go
File metadata and controls
148 lines (131 loc) · 3.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package sudoku
import "math/bits"
var relations [81][20]int
func init() {
// Precompute relation map — this takes about 16µs, which is fine
for i1 := 0; i1 < 9; i1++ {
for j1 := 0; j1 < 9; j1++ {
i := 0
for i2 := 0; i2 < 9; i2++ {
for j2 := 0; j2 < 9; j2++ {
if (i1 == i2) != (j1 == j2) ||
// i1 != i2 implies j1 != j2 at this point
(i1/3 == i2/3 && j1/3 == j2/3 && i1 != i2) {
relations[9*i1+j1][i] = 9*i2 + j2
i++
}
}
}
}
}
}
const conflict uint16 = (1 << 10) - (1 << 1) // set bits 1 to 9
type solver struct {
next []int
progress uint8
grid Grid
conflicts [81]uint16 // bitmask - uses bits 1 to 9 out of 16
}
func (s *solver) copy() solver {
if len(s.next) > 0 {
panic("must process next before making copy")
}
copy := *s
copy.next = nil // avoid sharing underlying array
return copy
}
func (s *solver) load(grid *Grid) bool {
for cell, value := range grid {
if value == 0 {
continue
}
if !s.mark(cell, value) {
return false
}
}
return true
}
func (s *solver) mark(cell int, value uint8) bool {
// If this value is already known, there's nothing to do.
// This happens if the input is over-constrained.
if s.grid[cell] == value {
return true
}
// If we hit an incompatibility, this value doesn't work.
if s.conflicts[cell]&(1<<value) != 0 {
return false
}
// Assign value.
s.conflicts[cell] = conflict
s.grid[cell] = value
s.progress++
// Apply constraints.
for _, related := range relations[cell] {
// If the constraint was already known, move on.
if s.conflicts[related]&(1<<value) != 0 {
continue
}
// Record the constraint.
s.conflicts[related] |= 1 << value
// If we create an incompatibility, this value doesn't work.
if s.conflicts[related] == conflict {
return false
}
// If we can determine the value, add it to the queue.
if bits.OnesCount16(s.conflicts[related]) == 8 {
s.next = append(s.next, related)
}
}
// If new values become known, mark them recursively.
for len(s.next) > 0 {
cell, s.next = s.next[0], s.next[1:]
value = uint8(bits.TrailingZeros16(s.conflicts[cell] ^ conflict))
if !s.mark(cell, value) {
return false
}
}
// No incompatibility was found.
return true
}
func (s *solver) search(grids []Grid) []Grid {
// If the grid is complete, there is a solution in this branch.
if s.progress == 81 {
grids = append(grids, s.grid)
return grids
}
// Try all possible values of the cell with the fewest choices.
cell := s.candidate()
for value := uint8(1); value < uint8(10); value++ {
if s.conflicts[cell]&(1<<value) == 0 {
copy := s.copy()
if copy.mark(cell, value) {
grids = copy.search(grids)
}
}
}
return grids
}
func (s *solver) candidate() int {
// Find the cell with the most conflicts.
candidate := 0
score := 0
for cell, value := range s.grid {
if value == 0 {
conflicts := bits.OnesCount16(s.conflicts[cell])
if conflicts > score {
candidate = cell
score = conflicts
}
}
}
return candidate
}
// Solve a grid. Return a slice of 0, 1, or several solutions.
func Solve(g *Grid) []Grid {
var s solver
var grids []Grid
if s.load(g) {
grids = s.search(grids)
}
return grids
}