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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
|
#include <iostream>
#include <iomanip>
#include <string>
#include <vector>
#include <deque>
#include <cstring>
#include <cstdint>
#include <cassert>
using namespace std;
struct Sudoku {
int8_t bd[81];
uint16_t poss[81];
int nfull = 0;
Sudoku() {
for (int i = 0; i < 81; i++) {
poss[i] = 0x1ff;
}
}
inline int8_t& operator[](int8_t i) {return bd[i];}
inline const int8_t& operator[](int8_t i) const {return bd[i];}
};
static istream& operator>>(istream &in, Sudoku &su) {
su.nfull = 0;
for (int i = 0; i < 81; i++) {
char c;
in >> c;
if (c == '.') {
su[i] = -1;
} else {
assert('1' <= c && c <= '9');
su[i] = c - '1';
su.nfull++;
}
}
return in;
}
__attribute__((unused))
static ostream& operator<<(ostream &os, const Sudoku &su) {
for (int y = 0; y < 9; y++) {
if (y != 0) {
os << endl;
if (y % 3 == 0) os << endl;
}
for (int x = 0; x < 9; x++) {
if (x != 0) {
os << ' ';
if (x % 3 == 0) os << ' ';
}
int8_t v = su[9 * y + x];
if (v == -1) os << '.';
else os << (int)v + 1;
// string s;
// for (int i = 0; i < 9; i++) {
// if (su.poss[9 * y + x] & (1<<i)) s += '1' + i;
// }
// os << '(' << setw(9) << s << ')';
}
}
return os;
}
template <bool checkPoss>
static bool isValid(const Sudoku &su) {
for (int y = 0; y < 9; y++) {
int i = 9 * y;
uint16_t seen = 0;
uint16_t seenPoss = 0;
for (int x = 0; x < 9; x++, i++) {
int v = su[i];
if (v != -1) {
if (seen & (1<<v)) return false;
else seen |= 1<<v;
} else {
if (checkPoss) seenPoss |= su.poss[i];
}
}
if (checkPoss) {
if ((seenPoss | seen) != 0x1ff) return false;
}
}
for (int x = 0; x < 9; x++) {
int i = x;
uint16_t seen = 0;
uint16_t seenPoss = 0;
for (int y = 0; y < 9; y++, i += 9) {
int v = su[i];
if (v != -1) {
if (seen & (1<<v)) return false;
else seen |= 1<<v;
} else {
if (checkPoss) seenPoss |= su.poss[i];
}
}
if (checkPoss) {
if ((seenPoss | seen) != 0x1ff) return false;
}
}
int i = 0;
for (int by = 0; by < 9; by += 3) {
for (int bx = 0; bx < 9; bx += 3) {
uint16_t seen = 0;
uint16_t seenPoss = 0;
int j = i;
for (int y = 0; y < 3; y++) {
for (int x = 0; x < 3; x++) {
int v = su[j];
if (v != -1) {
if (seen & (1<<v)) return false;
else seen |= 1<<v;
} else {
if (checkPoss) seenPoss |= su.poss[j];
}
j++;
}
j += 6;
}
if (checkPoss) {
if ((seenPoss | seen) != 0x1ff) return false;
}
i += 3;
}
i += 18;
}
return true;
}
static void scratchAround(Sudoku &su, int idx, int x, int y) {
uint16_t mask = 0x1ff & ~(1 << su[idx]);
for (int i = 0; i < 9; i++) {
su.poss[9 * y + i] &= mask;
su.poss[9 * i + x] &= mask;
}
int bx = x / 3 * 3, by = y / 3 * 3;
for (int yi = 0; yi < 3; yi++) {
for (int xi = 0; xi < 3; xi++) {
su.poss[9 * (by + yi) + bx + xi] &= mask;
}
}
}
static void scratchAround(Sudoku &su, int idx) {
scratchAround(su, idx, idx % 9, idx / 9);
}
static void scratchAround(Sudoku &su, int x, int y) {
scratchAround(su, 9 * y + x, x, y);
}
#if 0
struct Resolution {
bool ok; // false if the given sudoku cannot be made valid at all
int index, value;
Resolution() : ok(false) {}
Resolution(int index, int value) : ok(true), index(index), value(value) {}
};
static Resolution solveDestructive(Sudoku &su) {
// cerr << "solveDestructive(" << su.nfull << ")" << endl;
// for (int y = 0, i = 0; y < 9; y++) {
// for (int x = 0; x < 9; x++, i++) {
// if (su[i] != -1) continue;
// if (__builtin_popcount(su.poss[i]) == 1) {
// int value = __builtin_ctz(su.poss[i]);
// su[i] = value;
// scratchAround(su, i);
// }
// }
// }
if (su.nfull == 81) {
if (isValid(su)) {
return Resolution(-1, -1);
} else {
return Resolution();
}
}
vector<pair<int, int>> options; // pair(index, count)
for (int y = 0, i = 0; y < 9; y++) {
for (int x = 0; x < 9; x++, i++) {
if (su[i] != -1) continue;
int count = __builtin_popcount((uint32_t)su.poss[i]);
options.emplace_back(i, count);
}
}
sort(options.begin(), options.end(), [](const pair<int, int> &a, const pair<int, int> &b) {
return a.second < b.second;
});
if (options[0].second == 0) {
// invalid
return Resolution();
}
for (const pair<int, int> &p : options) {
int index = p.first;
vector<int> okValues;
for (int v = 0; v < 9; v++) {
if ((su.poss[index] & (1 << v)) == 0) continue;
Sudoku su2 = su;
su2[index] = v;
su2.nfull++;
// cerr << "try " << v << endl;
if (!isValid(su2)) {
// cerr << " invalid" << endl;
continue;
}
scratchAround(su2, index);
if (solveDestructive(su2).ok) {
okValues.push_back(v);
}
}
if (okValues.size() == 0) {
// invalid
return Resolution();
}
if (okValues.size() == 1) {
return Resolution(index, okValues[0]);
}
}
assert(false);
}
#endif
struct Resolution {
int index, value;
Resolution(int index, int value) : index(index), value(value) {}
Resolution() : index(-1), value(-1) {}
};
static bool operator==(const Resolution &a, const Resolution &b) {
return a.index == b.index && a.value == b.value;
}
static bool operator!=(const Resolution &a, const Resolution &b) {
return !(a == b);
}
static int calcMaxposs(const Sudoku &su) {
int mx = 0;
for (int i = 0; i < 81; i++) {
mx = max(mx, __builtin_popcount((unsigned)su.poss[i]));
}
return mx;
}
struct Item {
Sudoku su;
int maxposs;
Resolution init;
int depth;
};
static Resolution singularQueue(const deque<Item> &qu) {
assert(qu.size() > 0);
int values[81];
memset(values, 0xff, 81 * sizeof(int));
for (const Item &item : qu) {
if (values[item.init.index] == -1) {
values[item.init.index] = item.init.value;
} else {
values[item.init.index] = -2;
}
}
for (int i = 0; i < 81; i++) {
if (values[i] >= 0) return Resolution(i, values[i]);
}
return Resolution();
}
static Resolution solve(const Sudoku &su) {
// Sudoku su2 = su;
// return solveDestructive(su2);
deque<Item> qu;
for (int i = 0; i < 81; i++) {
if (su[i] != -1) continue;
for (int v = 0; v < 9; v++) {
Item item = {su, 0, {0, 0}, 1};
item.su[i] = v;
scratchAround(item.su, i);
if (isValid<true>(item.su)) {
// item.maxposs = calcMaxposs(item.su);
item.init = {i, v};
qu.push_back(move(item));
}
}
}
int lastdepth = -1;
while (qu.size() > 0) {
Resolution res = singularQueue(qu);
if (res.index != -1) {
return res;
}
Item current = move(qu.front());
qu.pop_front();
if (current.depth != lastdepth) {
cout << "depth = " << current.depth << " current: " << current.init.index << " " << current.init.value + 1 << endl;
lastdepth = current.depth;
}
bool anyvalid = false;
for (int i = 0; i < 81; i++) {
if (current.su[i] != -1) continue;
for (int v = 0; v < 9; v++) {
Item item = {current.su, 0, current.init, current.depth + 1};
item.su[i] = v;
scratchAround(item.su, i);
if (isValid<true>(item.su)) {
// item.maxposs = calcMaxposs(item.su);
qu.push_back(move(item));
anyvalid = true;
}
}
}
if (current.depth == 1 && !anyvalid) {
cout << "discarded " << current.init.index << " " << current.init.value << " at depth=1" << endl;
}
}
return Resolution();
}
static void cleanPoss(Sudoku &su) {
for (int i = 0; i < 81; i++) su.poss[i] = 0x1ff;
for (int y = 0; y < 9; y++) {
for (int x = 0; x < 9; x++) {
scratchAround(su, x, y);
}
}
}
int main() {
Sudoku su;
cin >> su;
cleanPoss(su);
Resolution res = solve(su);
cout << "resolution: " << res.index << " " << res.value + 1 << endl;
}
|