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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
pub type Index = usize;
pub type Row = Vec<Index>;
struct Entry {
left: Index,
right: Index,
up: Index,
down: Index,
x1: Index,
x2: Index,
}
impl Default for Entry {
fn default() -> Entry {
Entry {
left: 0,
right: 0,
up: 0,
down: 0,
x1: 0,
x2: 0,
}
}
}
impl Entry {
fn new() -> Entry {
Default::default()
}
}
pub struct Solver {
es: Vec<Entry>,
sol_rows: Vec<Index>,
finished: bool,
}
impl Solver {
pub fn new<I: Iterator<Item = Row>>(ncols: usize, rows: I) -> Solver {
let mut es = Vec::new();
Solver::add_headers(&mut es, ncols);
let mut bottoms = Vec::new();
bottoms.extend(2..ncols + 2);
for row in rows {
Solver::add_row(&mut es, &row, &mut bottoms);
}
let mut idx = 2;
while idx != 1 {
es[idx].up = bottoms[idx - 2];
es[bottoms[idx - 2]].down = idx;
idx = es[idx].right;
}
Solver {
es,
sol_rows: Vec::new(),
finished: false,
}
}
fn add_headers(es: &mut Vec<Entry>, ncols: usize) {
es.push(Entry::new());
es.push(Entry {
right: 2,
..Default::default()
});
for i in 1..ncols + 1 {
es.push(Entry {
left: i,
right: i + 2,
x1: i - 1,
..Default::default()
});
}
es[1].left = ncols + 1;
es[ncols + 1].right = 1;
}
fn add_row(es: &mut Vec<Entry>, row: &[Index], bottoms: &mut Vec<Index>) {
let row_start = es.len();
for &col_num in row {
let idx = es.len();
es.push(Entry {
x1: col_num + 2,
left: idx - 1,
right: idx + 1,
up: bottoms[col_num],
..Default::default()
});
es[bottoms[col_num]].down = idx;
bottoms[col_num] = idx;
es[col_num + 2].x2 += 1;
}
let row_end = es.len() - 1;
es[row_end].right = row_start;
es[row_start].left = row_end;
es[row_start].x2 = 1;
}
fn choose_column(&self) -> Index {
let mut min_size = Index::max_value();
let mut min_hdr = 0;
let mut hdr = self.es[1].right;
while hdr != 1 {
if self.es[hdr].x2 < min_size {
min_hdr = hdr;
min_size = self.es[hdr].x2;
}
hdr = self.es[hdr].right;
}
min_hdr
}
fn cover(&mut self, hdr: Index) {
let hdr_right = self.es[hdr].right;
self.es[hdr_right].left = self.es[hdr].left;
let hdr_left = self.es[hdr].left;
self.es[hdr_left].right = self.es[hdr].right;
let mut row = self.es[hdr].down;
while row != hdr {
let mut col = self.es[row].right;
while col != row {
let col_hdr = self.es[col].x1;
self.es[col_hdr].x2 -= 1;
let col_up = self.es[col].up;
self.es[col_up].down = self.es[col].down;
let col_down = self.es[col].down;
self.es[col_down].up = self.es[col].up;
col = self.es[col].right;
}
row = self.es[row].down;
}
}
fn uncover(&mut self, hdr: Index) {
let mut row = self.es[hdr].up;
while row != hdr {
let mut col = self.es[row].left;
while col != row {
let col_hdr = self.es[col].x1;
self.es[col_hdr].x2 += 1;
let col_up = self.es[col].up;
self.es[col_up].down = col;
let col_down = self.es[col].down;
self.es[col_down].up = col;
col = self.es[col].left;
}
row = self.es[row].up;
}
let hdr_right = self.es[hdr].right;
self.es[hdr_right].left = hdr;
let hdr_left = self.es[hdr].left;
self.es[hdr_left].right = hdr;
}
pub fn solve(&mut self, partial: Vec<Row>, sols: &mut Solutions) {
self.finished = false;
self.sol_rows.clear();
for row in &partial {
for col_num in row {
self.cover(col_num + 2);
}
}
self.solveception(&partial, sols);
for row in partial.iter().rev() {
for col_num in row.iter().rev() {
self.uncover(col_num + 2);
}
}
}
fn solveception(&mut self, partial: &[Row], sols: &mut Solutions) {
if self.es[1].right == 1 {
self.finished = {
let sol = Solution {
solver: self,
rows: self.sol_rows.iter(),
partial: partial.iter(),
};
!sols.push(sol)
};
return;
}
let hdr = self.choose_column();
self.cover(hdr);
let mut row_idx = self.es[hdr].down;
while row_idx != hdr {
self.sol_rows.push(row_idx);
let mut col_idx = self.es[row_idx].right;
while col_idx != row_idx {
let col_hdr = self.es[col_idx].x1;
let col_num = self.es[col_hdr].x1;
self.cover(col_num + 2);
col_idx = self.es[col_idx].right;
}
self.solveception(partial, sols);
col_idx = self.es[row_idx].left;
while col_idx != row_idx {
let col_hdr = self.es[col_idx].x1;
let col_num = self.es[col_hdr].x1;
self.uncover(col_num + 2);
col_idx = self.es[col_idx].left;
}
self.sol_rows.pop();
if self.finished {
break;
}
row_idx = self.es[row_idx].down;
}
self.uncover(hdr);
}
}
pub struct Solution<'a, 'b> {
solver: &'a Solver,
partial: ::std::slice::Iter<'b, Row>,
rows: ::std::slice::Iter<'a, Index>,
}
impl<'a, 'b> Iterator for Solution<'a, 'b> {
type Item = Row;
fn next(&mut self) -> Option<Self::Item> {
match self.partial.next() {
Some(row) => Some(row.clone()),
None => match self.rows.next() {
None => None,
Some(&row_idx) => Some(self.get_row(row_idx)),
},
}
}
}
impl<'a, 'b> Solution<'a, 'b> {
fn get_row(&self, row_idx: Index) -> Row {
let mut row_idx = row_idx;
let es = &self.solver.es;
while es[row_idx].x2 == 0 {
row_idx = es[row_idx].left;
}
let mut row = vec![es[es[row_idx].x1].x1];
let mut col_idx = es[row_idx].right;
while col_idx != row_idx {
row.push(es[es[col_idx].x1].x1);
col_idx = es[col_idx].right;
}
row
}
}
pub trait Solutions {
fn push(&mut self, sol: Solution) -> bool;
}