-
Notifications
You must be signed in to change notification settings - Fork 0
/
digit_chain.cpp
347 lines (302 loc) · 8.68 KB
/
digit_chain.cpp
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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
#include <iostream>
#include "digit_chain.h"
const int DC_BLACK = 0;
const int DC_WHITE = 1;
DigitVertex::DigitVertex() {
spot = Spot();
color = 0;
peers = std::vector<int>();
links = std::vector<StrongLink>();
}
DigitVertex::DigitVertex(Spot _spot, int _color) {
spot = _spot;
color = _color;
peers = std::vector<int>();
links = std::vector<StrongLink>();
}
void DigitVertex::connect(StrongLink& link, int peer) {
peers.push_back(peer);
links.push_back(link);
}
DigitChain::DigitChain() {
val = 0;
black = true;
white = true;
verts = std::vector<DigitVertex>();
}
DigitChain::DigitChain(int _val) {
val = _val;
black = true;
white = true;
verts = std::vector<DigitVertex>();
}
DigitChain::DigitChain(int _val, StrongLink& link) {
val = _val;
black = true;
white = true;
if(link.val != val) {
std::cout << "DigitChain(int _val, StrongLink& link) SEVERE WARNING: creating DigitChain with wrong val?" << std::endl;
}
verts = std::vector<DigitVertex>();
DigitVertex vert_a(link.a, DC_BLACK);
DigitVertex vert_b(link.b, DC_WHITE);
verts.push_back(vert_a);
verts.push_back(vert_b);
verts[0].connect(link, 1);
verts[1].connect(link, 0);
}
bool DigitChain::has_spot(Spot p) {
return idx_of_digit_vertex(verts, p) != -1;
}
void DigitChain::add_strong_link(StrongLink& link) {
int idx_a = idx_of_digit_vertex(verts, link.a);
int idx_b = idx_of_digit_vertex(verts, link.b);
if(idx_a == -1 && idx_b == -1) {
std::cout << "DigitChain::add_strong_link not connected" << std::endl;
return;
}
if(idx_a >= 0 && idx_b >= 0) {
if(in_ints(verts[idx_a].peers, idx_b)) {
// already connected
return;
}
verts[idx_a].connect(link, idx_b);
verts[idx_b].connect(link, idx_a);
return;
}
int idx_bridge;
Spot new_spot;
if(idx_a >= 0) {
idx_bridge = idx_a;
new_spot = link.b;
} else {
idx_bridge = idx_b;
new_spot = link.a;
}
int idx_new = verts.size();
verts.push_back(DigitVertex(new_spot, dc_other_color(verts[idx_bridge].color)));
verts[idx_bridge].connect(link, idx_new);
verts[idx_new].connect(link, idx_bridge);
}
void DigitChain::scan_false_color() {
//rows
for(int i=0; i < 9; i++) {
int count_white = 0;
int count_black = 0;
for(int j=0; j < 9; j++) {
int idx = idx_of_digit_vertex(verts, Spot(i, j));
if(idx == -1) { continue; }
if(verts[idx].color == DC_BLACK) {
count_black++;
} else if(verts[idx].color == DC_WHITE) {
count_white++;
}
}
if(count_black > 1) {
black = false;
}
if(count_white > 1) {
white = false;
}
}
//cols
for(int i=0; i < 9; i++) {
int count_white = 0;
int count_black = 0;
for(int j=0; j < 9; j++) {
int idx = idx_of_digit_vertex(verts, Spot(j, i));
if(idx == -1) { continue; }
if(verts[idx].color == DC_BLACK) {
count_black++;
} else if(verts[idx].color == DC_WHITE) {
count_white++;
}
}
if(count_black > 1) {
black = false;
}
if(count_white > 1) {
white = false;
}
}
//boxes
for(int i=0; i < 9; i++) {
int count_white = 0;
int count_black = 0;
for(int j=0; j < 9; j++) {
int idx = idx_of_digit_vertex(verts, spot_of_boxid(i, j));
if(idx == -1) { continue; }
if(verts[idx].color == DC_BLACK) {
count_black++;
} else if(verts[idx].color == DC_WHITE) {
count_white++;
}
}
if(count_black > 1) {
black = false;
}
if(count_white > 1) {
white = false;
}
}
}
std::vector<Spot> DigitChain::chain_spots() {
std::vector<Spot> ret;
for(int i=0; i < verts.size(); i++) {
ret.push_back(verts[i].spot);
}
return ret;
}
std::vector<Spot> DigitChain::both_cover(Bits* bits) {
std::vector<Spot> ret;
for(int i=0; i < 81; i++) {
/*
if(row_of_idx(i) == 7 && col_of_idx(i) == 0 && val == 4) {
std::cout << "x" << std::endl;
}
*/
Spot p = spot_of_idx(i);
if(!bits->has_candidate(p, val)) { continue; }
if(has_spot(p)) { continue; }
bool touch_white = false;
bool touch_black = false;
for(int j=0; j < verts.size(); j++) {
Spot px = verts[j].spot;
if( (p.row == px.row) || (p.col == px.col) || (boxid_of_spot(p) == boxid_of_spot(px)) ) {
if(verts[j].color == DC_BLACK) {
touch_black = true;
} else if(verts[j].color == DC_WHITE) {
touch_white = true;
}
}
}
if(touch_white && touch_black) {
ret.push_back(p);
}
}
return ret;
}
DigitChains::DigitChains() {
val = 0;
chains = std::vector<DigitChain>();
}
DigitChains::DigitChains(int _val) {
val = _val;
chains = std::vector<DigitChain>();
}
void DigitChains::add_strong_link(StrongLink& link) {
//std::cout << "add_strong_link: adding: " << link.a << " " << link.b << std::endl;
if(link.val != val) { return; }
int idx_a = idx_of_digit_chain(chains, link.a);
int idx_b = idx_of_digit_chain(chains, link.b);
if(idx_a == -1 && idx_b == -1) {
//std::cout << "\t\t\tnew" << std::endl;
//std::cout << "add_strong_link new chain" << std::endl;
// add new chain
chains.push_back(DigitChain(link.val, link));
return;
}
if(idx_a >= 0 && idx_b >= 0 && idx_a != idx_b) {
//std::cout << "\t\t\tmerge" << std::endl;
//std::cout << "add_strong_link merge chain, indexes " << idx_a << " " << idx_b << std::endl;
// merge chains!
chains.push_back(bridge_digit_chains(chains[idx_a], chains[idx_b], link));
//std::cout << "\tcompleted bridge_digit_chains" << std::endl;
chains.erase(chains.begin() + (idx_a > idx_b ? idx_a : idx_b));
chains.erase(chains.begin() + (idx_a > idx_b ? idx_b : idx_a));
return;
}
//std::cout << "\t\t\tappend" << std::endl;
// only one chain matches
//std::cout << "add_strong_link one chain: linking on" << std::endl;
int idx = (idx_a == -1 ? idx_b : idx_a);
chains[idx].add_strong_link(link);
}
void DigitChains::add_links(Bits* bits) {
//std::cout << "add_links: val: " << val << std::endl;
std::vector<StrongLink> links = strong_links_of_val(bits, val);
for(int i=0; i < links.size(); i++) {
//std::cout << "DCS:add_links doing add_strong_link" << std::endl;
//std::cout << "\t\tadding: " << links[i].a << " " << links[i].b << std::endl;
add_strong_link(links[i]);
//std::cout << "DCS:add_links completed add_strong_link" << std::endl;
//std::cout << "\t" << chains.size() << std::endl;
}
}
int dc_other_color(int color) {
if(color == DC_BLACK) {
return DC_WHITE;
} else if(color == DC_WHITE) {
return DC_BLACK;
}
std::cout << "Bad color" << std::endl;
return -1;
}
int idx_of_digit_vertex(std::vector<DigitVertex>& verts, Spot p) {
for(int i=0; i < verts.size(); i++) {
//std::cout << "idx_of_digit_vertex, checking " << p << " = " << verts[i].spot << std::endl;
if(spot_equal(p, verts[i].spot)) {
//std::cout << i << std::endl;
return i;
}
}
//std::cout << -1 << std::endl;
return -1;
}
DigitChain bridge_digit_chains(DigitChain& dca, DigitChain& dcb, StrongLink& link) {
//std::cout << "bridge_digit_chains" << std::endl;
DigitChain dcc(dca.val);
Spot aspot;
Spot bspot;
if(dca.has_spot(link.a)) {
aspot = link.a;
bspot = link.b;
} else if(dcb.has_spot(link.b)){
aspot = link.b;
bspot = link.a;
} else {
std::cout << "!! Should not be bridging (merging) digitchains" << std::endl;
}
int a_idx_spot = idx_of_digit_vertex(dca.verts, aspot);
int b_idx_spot = idx_of_digit_vertex(dcb.verts, bspot);
// whether to reverse B's colors
bool reverse = (dca.verts[a_idx_spot].color == dcb.verts[b_idx_spot].color);
//std::cout << "bridge_digit_chains: init complete" << std::endl;
// add A's verts
for(int i=0; i < dca.verts.size(); i++) {
dcc.verts.push_back(DigitVertex(dca.verts[i].spot, dca.verts[i].color));
for(int j=0; j < dca.verts[i].peers.size(); j++) {
dcc.verts[i].peers.push_back(dca.verts[i].peers[j]);
dcc.verts[i].links.push_back(dca.verts[i].links[j]);
}
}
//std::cout << "bridge_digit_chains: A's verts added" << std::endl;
int offset = dca.verts.size();
// add B's verts
for(int i=0; i < dcb.verts.size(); i++) {
dcc.verts.push_back(DigitVertex(dcb.verts[i].spot, (reverse ? dc_other_color(dcb.verts[i].color) : dcb.verts[i].color)));
for(int j=0; j < dcb.verts[i].peers.size(); j++) {
dcc.verts[i + offset].peers.push_back(dcb.verts[i].peers[j] + offset);
dcc.verts[i + offset].links.push_back(dcb.verts[i].links[j]);
}
}
//std::cout << "bridge_digit_chains: B's verts added" << std::endl;
// connect link
int b_idx_off = b_idx_spot + offset;
dcc.verts[a_idx_spot].connect(link, b_idx_off);
//std::cout << "bridge_digit_chains: X" << std::endl;
dcc.verts[b_idx_off].connect(link, a_idx_spot);
//std::cout << "bridge_digit_chains: connected" << std::endl;
return dcc;
}
int idx_of_digit_chain(std::vector<DigitChain>& chains, Spot p) {
//std::cout << "idx_of_digit_chain, spot: " << p << std::endl;
for(int i=0; i < chains.size(); i++) {
if(idx_of_digit_vertex(chains[i].verts, p) >= 0) {
//std::cout << i << std::endl;
return i;
}
}
//std::cout << -1 << std::endl;
return -1;
}