@@ -40,18 +40,6 @@ func (s *solver) init() {
40
40
s .next = make ([]int , 0 , 81 )
41
41
}
42
42
43
- func (s * solver ) copy () solver {
44
- // Given that s.next is empty, sharing the same underlying array is fine.
45
- // Indeed, mark() only pushes next steps in the queue and then pops them.
46
- // Since search() explores options sequentially, reusing the same memory
47
- // space for next steps doesn't cause problems.
48
- if len (s .next ) > 0 {
49
- panic ("must process next before making copy" )
50
- }
51
- copy := * s
52
- return copy
53
- }
54
-
55
43
func (s * solver ) load (grid * Grid ) bool {
56
44
for cell , value := range grid {
57
45
if value == 0 {
@@ -119,11 +107,20 @@ func (s *solver) search(grids []Grid) []Grid {
119
107
return grids
120
108
}
121
109
110
+ // Since s.next is empty, sharing the underlying array with a copy is OK.
111
+ // Indeed, mark() only pushes next steps in the queue and then pops them.
112
+ // Since search() explores options sequentially, reusing the same memory
113
+ // space for next steps doesn't cause problems.
114
+ if len (s .next ) > 0 {
115
+ panic ("must process next before searching" )
116
+ }
117
+
122
118
// Try all possible values of the cell with the fewest choices.
119
+ var copy solver
123
120
cell := s .candidate ()
124
121
for value := uint8 (1 ); value < uint8 (10 ); value ++ {
125
122
if s .conflicts [cell ]& (1 << value ) == 0 {
126
- copy := s . copy ()
123
+ copy = * s
127
124
if copy .mark (cell , value ) {
128
125
grids = copy .search (grids )
129
126
}
0 commit comments