-
Notifications
You must be signed in to change notification settings - Fork 0
/
16x16.cpp
511 lines (453 loc) · 11.9 KB
/
16x16.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
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
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
//Use suppressed optimizations for faster execution times
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#include <iostream>
#include <vector>
using namespace std;
#define N 16
#define Nn 4
#define UNASSIGNED 0
//Setup global grid for use
int grid[N][N];
int order[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
int tracker[16] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16};
void genOrder()
{
//sorting
for (int i = 0; i < N; i++)
{
for (int j = i + 1; j < N; j++)
{
if (order[i] < order[j])
{
int temp = 0;
temp = order[i];
order[i] = order[j];
order[j] = temp;
temp = tracker[i];
tracker[i] = tracker[j];
tracker[j] = temp;
}
}
}
}
bool isSafe(int row, int col)
{
//Used in the hidden and naked single
//Check row and col
int copy = grid[row][col];
for (int i = 0; i < N; i++)
{
if (grid[row][i] == copy)
{
if (i != col)
{
return true;
}
}
if (grid[i][col] == copy)
{
if (i != row)
{
return true;
}
}
}
//Check block
int bRow = row / Nn;
int bCol = col / Nn;
for (int calcR = bRow * Nn; calcR <= bRow * Nn + (Nn - 1); calcR++)
{
for (int calcC = bCol * Nn; calcC <= bCol * Nn + (Nn - 1); calcC++)
{
if (grid[calcR][calcC] == copy)
{
if (calcR != row)
{
if (calcC != col)
{
return true;
}
}
}
}
}
return false;
}
int getEmptyCells()
{
//Return the count of empty cells as an integer
int count = 0;
for (int row = 0; row < N; row++)
{
for (int col = 0; col < N; col++)
{
if (grid[row][col] == UNASSIGNED)
{
count++;
}
}
}
return count;
}
void nakedSingle()
{
//Naked Single means that in a specific cell only one digit remains possible
//(the last remaining candidate has no other candidates to hide behind and is thus naked)
//The digit must then go into that cell.
int pCand = 0;
while (pCand != getEmptyCells())
{
pCand = getEmptyCells();
for (int row = 0; row < N; row++)
{
for (int col = 0; col < N; col++)
{
int pGuesses = 0;
int value = 0;
if (grid[row][col] == UNASSIGNED)
{
for (int guessVal = 0; guessVal < N; guessVal++)
{
grid[row][col] = tracker[guessVal];
if (!isSafe(row, col))
{
value = tracker[guessVal];
pGuesses++;
}
}
if (pGuesses != 1)
{
grid[row][col] = UNASSIGNED;
}
else
{
grid[row][col] = value;
}
}
}
}
}
}
void hiddenSingle()
{
//Hidden Single means that for a given digit and block only one cell is left to place that digit
//The cell itself has more than one candidate left, the correct digit is thus hidden amongst the rest
int pCand;
//Col
pCand = 0;
while (pCand != getEmptyCells())
{
pCand = getEmptyCells();
for (int row = 0; row < N; row++)
{
for (int value = 0; value < N; value++)
{
int colI = 0;
int pos = 0;
for (int col = 0; col < N; col++)
{
if (grid[row][col] == UNASSIGNED)
{
grid[row][col] = tracker[value];
if (!isSafe(row, col))
{
colI = col;
pos++;
}
grid[row][col] = UNASSIGNED;
}
}
if (pos == 1)
{
grid[row][colI] = tracker[value];
}
}
}
}
//Row
pCand = 0;
while (pCand != getEmptyCells())
{
pCand = getEmptyCells();
for (int col = 0; col < N; col++)
{
for (int value = 0; value < N; value++)
{
int rowI = 0;
int pos = 0;
for (int row = 0; row < N; row++)
{
if (grid[row][col] == UNASSIGNED)
{
grid[row][col] = tracker[value];
if (!isSafe(row, col))
{
rowI = row;
pos++;
}
grid[row][col] = UNASSIGNED;
}
}
if (pos == 1)
{
grid[rowI][col] = tracker[value];
}
}
}
}
//Block
pCand = 0;
while (pCand != getEmptyCells())
{
pCand = getEmptyCells();
for (int bRow = 0; bRow < Nn; bRow++)
{
for (int bCol = 0; bCol < Nn; bCol++)
{
for (int value = 0; value < N; value++)
{
int pos = 0;
int colI = 0;
int rowI = 0;
for (int calcR = bRow * Nn; calcR < bRow * Nn + Nn; calcR++)
{
for (int calcC = bCol * Nn; calcC < bCol * Nn + Nn; calcC++)
{
if (grid[calcR][calcC] == UNASSIGNED)
{
grid[calcR][calcC] = tracker[value];
if (!isSafe(calcR, calcC))
{
rowI = calcR;
colI = calcC;
pos++;
}
grid[calcR][calcC] = UNASSIGNED;
}
}
}
if (pos == 1)
{
grid[rowI][colI] = tracker[value];
}
}
}
}
}
}
void getSingles()
{
//Combining hidden single and naked single functions
int count = 1;
do
{
count = getEmptyCells();
nakedSingle();
hiddenSingle();
} while (count != getEmptyCells());
}
struct candidates
{
//Keep track of candidates
bool TF[N] = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};
int track = N + 1;
};
candidates calcCandidates(int row, int col)
{
candidates cand;
//Row
for (int colL = 0; colL < N; colL++)
{
if (grid[row][colL] != UNASSIGNED)
{
cand.TF[grid[row][colL] - 1] = false;
}
}
//Col
for (int rowL = 0; rowL < N; rowL++)
{
if (grid[rowL][col] != UNASSIGNED)
{
cand.TF[grid[rowL][col] - 1] = false;
}
}
//Block
int bRow = row / Nn;
bRow *= Nn;
int bCol = col / Nn;
bCol *= Nn;
for (int i = 0; i < Nn; i++)
{
for (int j = 0; j < Nn; j++)
{
if (grid[bRow + i][bCol + j] != UNASSIGNED)
{
cand.TF[grid[bRow + i][bCol + j] - 1] = false;
}
}
}
int count = 0;
for (int i = 0; i < N; i++)
{
if (cand.TF[i])
{
count++;
}
}
cand.track = count;
return cand;
}
bool isComplete()
{
//Check if board is complete
for (int row = 0; row < N; row++)
{
for (int col = 0; col < N; col++)
{
if (grid[row][col] == UNASSIGNED)
{
return false;
}
}
}
return true;
}
bool solve()
{
//Start/continue at best row and col on the board
int best = 17;
int bestRow;
int bestCol;
vector<pair<int, int>> pCand;
for (int row = 0; row < N; row++)
{
for (int col = 0; col < N; col++)
{
if (grid[row][col] == UNASSIGNED)
{
candidates cand = calcCandidates(row, col);
if (cand.track == 1)
{
pCand.push_back(make_pair(row, col));
for (int value = 0; value < N; value++)
if (cand.TF[tracker[value] - 1])
{
grid[row][col] = tracker[value];
break;
}
}
else if (cand.track < best)
{
bestRow = row;
bestCol = col;
best = cand.track;
}
}
}
}
if (isComplete())
{
return true;
}
candidates cand = calcCandidates(bestRow, bestCol);
//Brute 1..N
for (int value = 0; value < N; value++)
{
if (cand.TF[tracker[value] - 1])
{
grid[bestRow][bestCol] = tracker[value];
if (solve())
{
return true;
}
grid[bestRow][bestCol] = 0;
}
}
for (int i = 0; i < pCand.size(); i++)
{
grid[pCand[i].first][pCand[i].second] = 0;
}
return false;
}
int main()
{
//Fast input
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
//Take in NxN input
for (int row = 0; row < N; row++)
{
for (int col = 0; col < N; col++)
{
char input;
cin >> input;
int num = input;
if (num < 65)
{
grid[row][col] = num - 48;
//Setup tracker for hidden and naked singles
order[(num - 48)] += 1;
}
else
{
grid[row][col] = num - 55;
//Setup tracker for hidden and naked singles
order[(num - 55)] += 1;
}
}
}
//Get order for hidden/naked singles tracker
//Basically arranges the bruteforce 1..N is terms of the population of each cell
genOrder();
//Find cells using hidden and naked singles, and add to grid
getSingles();
//Get order for recursion tracker
//Basically arranges the bruteforce 1..N is terms of the population of each cell
for (int row = 0; row < N; row++)
{
for (int col = 0; col < N; col++)
{
order[grid[row][col]] += 1;
}
}
genOrder();
//Start backtracking with updated grid
if (solve())
{
//Print the grid if solved
for (int row = 0; row < N; row++)
{
if (grid[row][0] > 9)
{
char c = (char)(grid[row][0] + 55);
cout << c;
}
else
{
cout << grid[row][0];
}
for (int col = 1; col < N; col++)
{
if (grid[row][col] > 9)
{
char c = (char)(grid[row][col] + 55);
cout << ' ' << c;
}
else
{
cout << ' ' << grid[row][col];
}
}
cout << "\n";
}
}
else
{
//Could not find a solution
cout << "No Solution"
<< "\n";
}
return 0;
}