-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsudoku_handler.h
More file actions
855 lines (716 loc) · 27.9 KB
/
sudoku_handler.h
File metadata and controls
855 lines (716 loc) · 27.9 KB
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
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
#pragma once
#include "Lib.h"
template<sudoku_size_t square_height = 3, sudoku_size_t square_width = 3>
class SudokuHandler {
// Constants
static constexpr sudoku_size_t side_len = square_height * square_width;
static constexpr sudoku_size_t tot_num_cells = side_len * side_len;
static constexpr sudoku_size_t n_stored_per_cell = side_len + 1;
static constexpr sudoku_size_t tot_storage = n_stored_per_cell * tot_num_cells;
static constexpr sudoku_size_t n_stored_per_side = n_stored_per_cell * side_len;
// The sudoku data
sudoku_data_t sud_data;
raw_sudoku_t raw_sud;
/// Initializes sudoku with a raw sudoku.
template<bool printDebugInfo = printDebugInfodefault>
sudoku_data_t init_sudoku_with_raw(const raw_sudoku_t & raw_s) {
sudoku_data_t s_data = init_sudoku();
for (sudoku_size_t ind = 0; ind < tot_num_cells; ++ind) {
const sudoku_size_t temp = raw_s[ind];
if (temp) {
s_data[ind * n_stored_per_cell] = temp;
}
}
if constexpr (printDebugInfo) std::cout << "Constructed Sudoku Data with raw Sudoku.\n";
return s_data;
}
/// Auto-fill sudoku.
template<bool printDebugInfo = printDebugInfodefault>
void auto_fill(sudoku_data_t & s_data, const bool init = false) {
for (sudoku_size_t ind = 0; ind < tot_num_cells; ++ind) {
const sudoku_size_t curr_ind = ind * n_stored_per_cell;
const sudoku_size_t cell_col_ind = ind % side_len;
const sudoku_size_t cell_row_ind = ind / side_len;
const sudoku_size_t col_ind = curr_ind % n_stored_per_side;
const sudoku_size_t row_ind = curr_ind / n_stored_per_side;
const sudoku_size_t temp = s_data[curr_ind];
if (temp == 0) {// Number not set
if (init) {
// Initialize as all possible
for (sudoku_size_t i = 0; i < side_len; ++i) {
s_data[curr_ind + i + 1] = 2;
}
}
// Iterate over row and column
for (sudoku_size_t i = 0; i < side_len; ++i) {
const sudoku_size_t temp2 = s_data[cell_row_ind * n_stored_per_side + i * n_stored_per_cell];
if (temp2) {//Number in same row set
s_data[curr_ind + temp2] = 1;
}
const sudoku_size_t temp3 = s_data[i * n_stored_per_side + cell_col_ind * n_stored_per_cell];
if (temp3) {//Number in same col set
s_data[curr_ind + temp3] = 1;
}
}
// Iterate over squares
const sudoku_size_t square_begin = side_len * (cell_row_ind - (cell_row_ind % square_height)) + (cell_col_ind - (cell_col_ind % square_width));
const sudoku_size_t square_row_ind = cell_row_ind / square_height;
const sudoku_size_t square_col_ind = cell_col_ind / square_width;
for (sudoku_size_t k = 0; k < square_height; ++k) {
for (sudoku_size_t i = 0; i < square_width; ++i) {
const sudoku_size_t temp4 = s_data[(square_begin + i + k * side_len) * n_stored_per_cell];
if (temp4) {//Number in same col set
s_data[curr_ind + temp4] = 1;
}
}
}
}
}
if constexpr (printDebugInfo) std::cout << "Sudoku initialized with autofill.\n";
}
/// Looks for numbers that can only be placed in one cell in a given row/col.
template<bool printDebugInfo = printDebugInfodefault>
SolveStepRes find_unique_in_rcs(sudoku_data_t & s_data) {
bool found_number = false;
// Iterate over all rows / cols / squares
for (sudoku_size_t row_num = 0; row_num < side_len; ++row_num) {
const sudoku_size_t curr_row_start = row_num * side_len;
const sudoku_size_t curr_col_start = row_num;
// Iterate over numbers
for (sudoku_size_t number = 0; number < side_len; ++number) {
// Test if number not set already somewhere
sudoku_size_t num_times_set_row = 0;
sudoku_size_t num_times_set_col = 0;
for (sudoku_size_t cell_ind = 0; cell_ind < side_len; ++cell_ind) {
const sudoku_size_t curr_cell_ind_row = curr_row_start + cell_ind;
const sudoku_size_t curr_cell_ind_col = curr_col_start + cell_ind * side_len;
if (s_data[curr_cell_ind_row * n_stored_per_cell] == number + 1) {
++num_times_set_row;
}
if (s_data[curr_cell_ind_col * n_stored_per_cell] == number + 1) {
++num_times_set_col;
}
}
if (num_times_set_row > 1 || num_times_set_col > 1) {
if constexpr (printDebugInfo) {
if (num_times_set_row > 1) {
std::cout << "Number "
<< number + 1 << " in row " << row_num + 1
<< " set " << num_times_set_row << " times.\n";
}
if (num_times_set_col > 1) {
std::cout << "Number "
<< number + 1 << " in col " << row_num + 1
<< " set " << num_times_set_col << " times.\n";
}
}
return Invalid;
}
else if (num_times_set_row == 1 && num_times_set_col == 1) {
continue;
}
sudoku_size_t num_poss_places_row = 0;
sudoku_size_t cell_poss_num_row = 0;
sudoku_size_t num_poss_places_col = 0;
sudoku_size_t cell_poss_num_col = 0;
// Iterate over all cells in row / col / square
for (sudoku_size_t cell_num = 0; cell_num < side_len; ++cell_num) {
// Count possibilities of number
// Row Stuff
const sudoku_size_t curr_row_cell_ind = curr_row_start + cell_num;
if (s_data[curr_row_cell_ind * n_stored_per_cell] == 0 && s_data[curr_row_cell_ind * n_stored_per_cell + number + 1] == 2) {
++num_poss_places_row;
cell_poss_num_row = cell_num;
}
// Col Stuff
const sudoku_size_t curr_col_cell_ind = curr_col_start + cell_num * side_len;
if (s_data[curr_col_cell_ind * n_stored_per_cell] == 0 && s_data[curr_col_cell_ind * n_stored_per_cell + number + 1] == 2) {
++num_poss_places_col;
cell_poss_num_col = cell_num;
}
}
if (num_poss_places_row == 1 && num_times_set_row == 0) {
s_data[(curr_row_start + cell_poss_num_row) * n_stored_per_cell] = number + 1;
found_number = true;
if constexpr (printDebugInfo) {
std::cout << "Found a number "
<< number + 1 << " in row " << row_num + 1 << " at index "
<< cell_poss_num_row << ".\n";
}
}
if (num_poss_places_col == 1 && num_times_set_col == 0) {
s_data[(curr_col_start + cell_poss_num_col * side_len) * n_stored_per_cell] = number + 1;
found_number = true;
if constexpr (printDebugInfo) {
std::cout << "Found a number "
<< number + 1 << " in col " << row_num + 1 << " at index "
<< cell_poss_num_col << ".\n";
}
}
if ((num_poss_places_row == 0 && num_times_set_row == 0) || (num_poss_places_col == 0 && num_times_set_col == 0)) {
if constexpr (printDebugInfo) {
std::cout << "No possibility to put "
<< number + 1 << " in " << (num_poss_places_row == 0 ? "row " : "col ") << row_num + 1 << ".\n";
}
return Invalid;
}
}
}
if constexpr (printDebugInfo) std::cout << "Sudoku checked for numbers with unique place.\n";
if (found_number) {
return ValidNewFound;
}
else {
return ValidnNoChange;
}
}
/// Looks for numbers that can only be placed in one cell in a given square.
template<bool printDebugInfo = printDebugInfodefault>
SolveStepRes find_unique_in_square(sudoku_data_t & s_data) {
bool found_number = false;
// Iterate over all rows
for (sudoku_size_t square_id = 0; square_id < side_len; ++square_id) {
const sudoku_size_t square_col_ind = square_id % square_height;
const sudoku_size_t square_row_ind = square_id / square_height;
const sudoku_size_t square_beg_ind = square_col_ind * square_width + square_row_ind * side_len * square_height;
// Iterate over numbers
for (sudoku_size_t number = 0; number < side_len; ++number) {
sudoku_size_t num_times_set = 0;
// Iterate over all rows
for (sudoku_size_t cell_id = 0; cell_id < side_len; ++cell_id) {
const sudoku_size_t square_col_ind_inner = cell_id % square_height;
const sudoku_size_t square_row_ind_inner = cell_id / square_height;
const sudoku_size_t cell_ind = square_beg_ind + square_col_ind_inner + square_row_ind_inner * side_len;
if (s_data[cell_ind * n_stored_per_cell] == number + 1) {
++num_times_set;
}
}
// Check if it is set multiple times or once
if (num_times_set > 1) {
if constexpr (printDebugInfo) {
std::cout << "Number "
<< number + 1 << " in square " << square_id
<< " set " << num_times_set << " times.\n";
}
return Invalid;
}
else if (num_times_set == 1) {
continue;
}
// Count possibilities of number
sudoku_size_t num_poss_places = 0;
sudoku_size_t cell_poss_num = 0;
for (sudoku_size_t cell_id = 0; cell_id < side_len; ++cell_id) {
const sudoku_size_t square_col_ind_inner = cell_id % square_height;
const sudoku_size_t square_row_ind_inner = cell_id / square_height;
const sudoku_size_t cell_ind = square_beg_ind + square_col_ind_inner + square_row_ind_inner * side_len;
if (s_data[cell_ind * n_stored_per_cell] == 0 && s_data[cell_ind * n_stored_per_cell + number + 1] == 2) {
++num_poss_places;
cell_poss_num = cell_id;
}
}
// If it can only be in one place
if (num_poss_places == 1) {
const sudoku_size_t square_col_ind_inner = cell_poss_num % square_height;
const sudoku_size_t square_row_ind_inner = cell_poss_num / square_height;
const sudoku_size_t cell_ind = square_beg_ind + square_col_ind_inner + square_row_ind_inner * side_len;
s_data[cell_ind * n_stored_per_cell] = number + 1;
found_number = true;
if constexpr (printDebugInfo) {
std::cout << "Found a number "
<< number + 1 << " in square " << square_id << " at cell "
<< cell_poss_num << ".\n";
}
}
// If it can't be set anywhere
if (num_poss_places == 0) {
if constexpr (printDebugInfo) {
std::cout << "No possibility to put "
<< number + 1 << " in square " << square_id << ".\n";
}
return Invalid;
}
}
}
if constexpr (printDebugInfo) std::cout << "Sudoku checked for numbers with unique place in square.\n";
if (found_number) {
return ValidNewFound;
}
else {
return ValidnNoChange;
}
}
/// Looks for cells where only one number can be.
template<bool printDebugInfo = printDebugInfodefault>
SolveStepRes find_single_number_cell(sudoku_data_t & s_data) {
bool found_number = false;
// Iterate over all rows
for (sudoku_size_t row_num = 0; row_num < side_len; ++row_num) {
// Iterate over all cols
for (sudoku_size_t col_num = 0; col_num < side_len; ++col_num) {
const sudoku_size_t cell_ind = row_num + col_num * side_len;
const sudoku_size_t data_ind = cell_ind * n_stored_per_cell;
if (s_data[data_ind] > 0) continue;
// Iterate over all numbers
sudoku_size_t last_possible_num = -1;
sudoku_size_t num_possible_num = 0;
for (sudoku_size_t num = 0; num < side_len; ++num) {
const sudoku_size_t curr_state = s_data[data_ind + 1 + num];
if (curr_state == 0) {
std::cout << "ERROR: Sudoku not filled yet.\n";
return Invalid;
}
else if (curr_state == 2) {
num_possible_num++;
last_possible_num = num;
}
}
if (num_possible_num == 1) {
s_data[data_ind] = 1 + last_possible_num;
if constexpr (printDebugInfo) std::cout << "Found new number!\n";
found_number = true;
}
else if (num_possible_num == 1) {
return Invalid;
}
}
}
if constexpr (printDebugInfo) std::cout << "Sudoku checked for cells with unique numbers.\n";
if (found_number) {
return ValidNewFound;
}
else {
return ValidnNoChange;
}
}
/// Looks for possible numbers that can be eliminated in all rows.
template<bool printDebugInfo = printDebugInfodefault>
SolveStepRes eliminate_possible_numbers_row(sudoku_data_t & s_data) {
bool found_number = false;
std::array<sudoku_size_t, square_height> occurrance_arr;
// Iterate over all rows
for (sudoku_size_t row_num = 0; row_num < side_len; ++row_num) {
// Check if a number in this row can only occur in a particular square
// Iterate over all numbers
for (sudoku_size_t num = 0; num < side_len; ++num) {
// Check if number already set somewhere
bool already_set = false;
for (sudoku_size_t col_num = 0; col_num < side_len; ++col_num) {
const sudoku_size_t cell_ind = row_num * side_len + col_num;
if (s_data[cell_ind * n_stored_per_cell] == num + 1) {
already_set = true;
}
}
if (already_set == true) continue;
setZero(occurrance_arr);
// Iterate over parts of row
for (sudoku_size_t square_col_num = 0; square_col_num < square_height; ++square_col_num) {
const sudoku_size_t first_cell_index = row_num * side_len + square_col_num * square_width;
// Iterate over cells in row in square
for (sudoku_size_t square_num = 0; square_num < square_width; ++square_num) {
const sudoku_size_t cell_ind = first_cell_index + square_num;
if (s_data[cell_ind * n_stored_per_cell] == 0 && s_data[cell_ind * n_stored_per_cell + 1 + num] == 2) {
occurrance_arr[square_col_num] = 1;
break;
}
}
}
// Find how many times it occurred and where
sudoku_size_t sum_occur = 0;
sudoku_size_t pos_occur = 0;
for (sudoku_size_t i = 0; i < square_height; ++i) {
if (occurrance_arr[i] == 1) {
sum_occur++;
pos_occur = i;
}
}
if (sum_occur > 1) {
continue;
}
else if (sum_occur == 0) {
if constexpr (printDebugInfo) std::cout << "No possibility!\n";
return Invalid;
}
// Iterate over square
const sudoku_size_t square_row_ind = row_num / square_height;
const sudoku_size_t square_col_ind = pos_occur;
const sudoku_size_t first_cell_index = square_col_ind * square_width + square_row_ind * square_height * side_len;
for (sudoku_size_t square_row_num = 0; square_row_num < square_height; ++square_row_num) {
// Ignore overlap
if (square_row_num == (row_num % square_height)) continue;
const sudoku_size_t curr_row_cell_index = first_cell_index + square_row_num * side_len;
// Iterate over cells in row in square
for (sudoku_size_t square_num = 0; square_num < square_width; ++square_num) {
const sudoku_size_t cell_ind = curr_row_cell_index + square_num;
if (s_data[cell_ind * n_stored_per_cell] == 0 && s_data[cell_ind * n_stored_per_cell + 1 + num] == 2) {
s_data[cell_ind * n_stored_per_cell + 1 + num] = 1;
found_number = true;
if constexpr (printDebugInfo) std::cout << "Eliminated possible number " << 1 + num << "!\n";
}
}
}
}
}
if constexpr (printDebugInfo) std::cout << "Sudoku checked for possibility elimination.\n";
if (found_number) {
return ValidNewFound;
}
else {
return ValidnNoChange;
}
}
/// Looks for possible numbers that can be eliminated in all cols.
template<bool printDebugInfo = printDebugInfodefault>
SolveStepRes eliminate_possible_numbers_col(sudoku_data_t & s_data) {
bool found_number = false;
std::array<sudoku_size_t, square_width> occurrance_arr;
// Iterate over all rows
for (sudoku_size_t col_num = 0; col_num < side_len; ++col_num) {
// Check if a number in this row can only occur in a particular square
// Iterate over all numbers
for (sudoku_size_t num = 0; num < side_len; ++num) {
// Check if number already set somewhere
bool already_set = false;
for (sudoku_size_t row_num = 0; row_num < side_len; ++row_num) {
const sudoku_size_t cell_ind = row_num * side_len + col_num;
if (s_data[cell_ind * n_stored_per_cell] == num + 1) {
already_set = true;
}
}
if (already_set == true) continue;
setZero(occurrance_arr);
// Iterate over parts of col
for (sudoku_size_t square_row_num = 0; square_row_num < square_width; ++square_row_num) {
const sudoku_size_t first_cell_index = square_row_num * side_len * square_height + col_num;
// Iterate over cells in col in square
for (sudoku_size_t square_num = 0; square_num < square_height; ++square_num) {
const sudoku_size_t cell_ind = first_cell_index + square_num * side_len;
if (s_data[cell_ind * n_stored_per_cell] == 0 && s_data[cell_ind * n_stored_per_cell + 1 + num] == 2) {
occurrance_arr[square_row_num] = 1;
break;
}
}
}
// Find how many times it occurred and where
sudoku_size_t sum_occur = 0;
sudoku_size_t pos_occur = 0;
for (sudoku_size_t i = 0; i < square_width; ++i) {
if (occurrance_arr[i] == 1) {
sum_occur++;
pos_occur = i;
}
}
if (sum_occur > 1) {
continue;
}
else if (sum_occur == 0) {
if constexpr (printDebugInfo) std::cout << "No possibility!\n";
return Invalid;
}
// Iterate over square
const sudoku_size_t square_row_ind = pos_occur;
const sudoku_size_t square_col_ind = col_num / square_width;
const sudoku_size_t first_cell_index = square_col_ind * square_width + square_row_ind * square_height * side_len;
for (sudoku_size_t square_col_num = 0; square_col_num < square_height; ++square_col_num) {
// Ignore overlap
if (square_col_num == (col_num % square_width)) continue;
const sudoku_size_t curr_row_cell_index = first_cell_index + square_col_num;
// Iterate over cells in row in square
for (sudoku_size_t square_num = 0; square_num < square_width; ++square_num) {
const sudoku_size_t cell_ind = curr_row_cell_index + square_num * side_len;
if (s_data[cell_ind * n_stored_per_cell] == 0 && s_data[cell_ind * n_stored_per_cell + 1 + num] == 2) {
s_data[cell_ind * n_stored_per_cell + 1 + num] = 1;
found_number = true;
if constexpr (printDebugInfo) std::cout << "Eliminated possible number " << 1 + num << "!\n";
}
}
}
}
}
if constexpr (printDebugInfo) std::cout << "Sudoku checked for possibility elimination.\n";
if (found_number) {
return ValidNewFound;
}
else {
return ValidnNoChange;
}
}
/// Looks for possible numbers that can be eliminated in all squares.
template<bool printDebugInfo = printDebugInfodefault>
SolveStepRes eliminate_possible_numbers_square(sudoku_data_t & s_data) {
bool found_number = false;
std::array<sudoku_size_t, square_height> occurrance_arr_h;
std::array<sudoku_size_t, square_width> occurrance_arr_w;
// Iterate over all rows
for (sudoku_size_t square_id = 0; square_id < side_len; ++square_id) {
const sudoku_size_t square_col_ind = square_id % square_height;
const sudoku_size_t square_row_ind = square_id / square_height;
const sudoku_size_t square_beg_ind = square_col_ind * square_width + square_row_ind * side_len * square_height;
// Check if a number in this square can only occur in a particular row / col
// Iterate over all numbers
for (sudoku_size_t num = 0; num < side_len; ++num) {
// Check if number already set somewhere
bool already_set = false;
for (sudoku_size_t square_col = 0; square_col < square_width; ++square_col) {
for (sudoku_size_t square_row = 0; square_row < square_width; ++square_row) {
const sudoku_size_t cell_ind = square_beg_ind + square_col + square_row * side_len;
if (s_data[cell_ind * n_stored_per_cell] == num + 1) {
already_set = true;
}
}
}
if (already_set == true) continue;
setZero(occurrance_arr_h);
setZero(occurrance_arr_w);
// Iterate over parts of square
for (sudoku_size_t square_row = 0; square_row < square_height; ++square_row) {
const sudoku_size_t first_cell_index = square_beg_ind + square_row * side_len;
// Iterate over cells in row in square
for (sudoku_size_t square_col = 0; square_col < square_width; ++square_col) {
const sudoku_size_t cell_ind = first_cell_index + square_col;
if (s_data[cell_ind * n_stored_per_cell] == 0 && s_data[cell_ind * n_stored_per_cell + 1 + num] == 2) {
occurrance_arr_h[square_row] = 1;
break;
}
}
}
// Iterate over parts of square
for (sudoku_size_t square_row = 0; square_row < square_width; ++square_row) {
const sudoku_size_t first_cell_index = square_beg_ind + square_row;
// Iterate over cells in row in square
for (sudoku_size_t square_col = 0; square_col < square_height; ++square_col) {
const sudoku_size_t cell_ind = first_cell_index + square_col * side_len;
if (s_data[cell_ind * n_stored_per_cell] == 0 && s_data[cell_ind * n_stored_per_cell + 1 + num] == 2) {
occurrance_arr_w[square_row] = 1;
break;
}
}
}
// Find how many times it occurred and where
sudoku_size_t sum_occur_w = 0;
sudoku_size_t pos_occur_w = 0;
sudoku_size_t sum_occur_h = 0;
sudoku_size_t pos_occur_h = 0;
for (sudoku_size_t i = 0; i < square_width; ++i) {
if (occurrance_arr_w[i] == 1) {
sum_occur_w++;
pos_occur_w = i;
}
}
for (sudoku_size_t i = 0; i < square_height; ++i) {
if (occurrance_arr_h[i] == 1) {
sum_occur_h++;
pos_occur_h = i;
}
}
if (sum_occur_h > 1 && sum_occur_w > 1) {
continue;
}
else if (sum_occur_h == 0 || sum_occur_w == 0) {
if constexpr (printDebugInfo) std::cout << "No possibility!\n";
return Invalid;
}
if (sum_occur_h == 1) {
// Iterate over row
const sudoku_size_t row_ind = square_row_ind * square_height + pos_occur_h;
const sudoku_size_t first_cell_index = row_ind * side_len;
for (sudoku_size_t col_ind = 0; col_ind < side_len; ++col_ind) {
// Ignore overlap
if (col_ind / square_width == square_col_ind) continue;
const sudoku_size_t cell_ind = first_cell_index + col_ind;
if (s_data[cell_ind * n_stored_per_cell] == 0 && s_data[cell_ind * n_stored_per_cell + 1 + num] == 2) {
s_data[cell_ind * n_stored_per_cell + 1 + num] = 1;
found_number = true;
if constexpr (printDebugInfo) std::cout << "Eliminated possible number " << 1 + num << "!\n";
}
}
}
if (sum_occur_w == 1) {
// Iterate over col
const sudoku_size_t col_ind = square_col_ind * square_width + pos_occur_w;
const sudoku_size_t first_cell_index = col_ind;
for (sudoku_size_t row_ind = 0; row_ind < side_len; ++row_ind) {
// Ignore overlap
if (row_ind / square_height == square_row_ind) continue;
const sudoku_size_t cell_ind = first_cell_index + row_ind * side_len;
if (s_data[cell_ind * n_stored_per_cell] == 0 && s_data[cell_ind * n_stored_per_cell + 1 + num] == 2) {
s_data[cell_ind * n_stored_per_cell + 1 + num] = 1;
found_number = true;
if constexpr (printDebugInfo) std::cout << "Eliminated possible number " << 1 + num << "!\n";
}
}
}
}
}
if constexpr (printDebugInfo) std::cout << "Sudoku checked for possibility elimination.\n";
if (found_number) {
return ValidNewFound;
}
else {
return ValidnNoChange;
}
}
/// Updating status uf solving process
SolveStepRes update(SolveStepRes old_step, SolveStepRes new_step) const {
if (old_step == Invalid || new_step == Invalid) {
return Invalid;
}
else if (new_step == ValidNewFound) {
return ValidNewFound;
}
return old_step;
}
/// Try to solve the sudoku using the previously defined functions
template<bool printDebugInfo = printDebugInfodefault>
SolveStepRes try_solving(sudoku_data_t & s_data) {
SolveStepRes found_something = ValidNewFound;
while (found_something == ValidNewFound) {
found_something = ValidnNoChange;
found_something = update(found_something, find_unique_in_rcs<printDebugInfo>(s_data));
found_something = update(found_something, find_unique_in_square<printDebugInfo>(s_data));
found_something = update(found_something, find_single_number_cell<printDebugInfo>(s_data));
found_something = update(found_something, eliminate_possible_numbers_row<printDebugInfo>(s_data));
found_something = update(found_something, eliminate_possible_numbers_col<printDebugInfo>(s_data));
found_something = update(found_something, eliminate_possible_numbers_square<printDebugInfo>(s_data));
auto_fill<printDebugInfo>(s_data, false);
}
return found_something;
}
/// Check if sudoku is solved.
bool solved(sudoku_data_t & s_data) {
// Check if there is a number set in every cell
for (sudoku_size_t cell_ind = 0; cell_ind < tot_num_cells; ++cell_ind) {
if (s_data[cell_ind * n_stored_per_cell] == 0) {
return false;
}
}
return true;
}
/// Find the cell with the least numbers possible
template<bool printDebugInfo = printDebugInfodefault>
sudoku_size_t find_least_uncertain_cell(sudoku_data_t & s_data) {
sudoku_size_t min_poss_nums = side_len;
sudoku_size_t min_data_ind = 0;
// Iterate over all rows
for (sudoku_size_t row_num = 0; row_num < side_len; ++row_num) {
// Iterate over all cols
for (sudoku_size_t col_num = 0; col_num < side_len; ++col_num) {
const sudoku_size_t cell_ind = row_num + col_num * side_len;
const sudoku_size_t data_ind = cell_ind * n_stored_per_cell;
if (s_data[data_ind] > 0) continue;
// Iterate over all numbers
sudoku_size_t num_possible_num = 0;
for (sudoku_size_t num = 0; num < side_len; ++num) {
const sudoku_size_t curr_state = s_data[data_ind + 1 + num];
if (curr_state == 2) {
num_possible_num++;
}
}
// Found new cell with fewer possibilities
if (min_poss_nums > num_possible_num) {
min_poss_nums = num_possible_num;
min_data_ind = data_ind;
}
}
}
return min_data_ind;
}
/// Find a solution and check if it is unique
template<bool random_order = False, bool printDebugInfo = printRecDebInfo,
typename RNG>
FullSol_t solve_brute_force_multiple_random(
sudoku_data_t & s_data,
RNG & rng,
const rec_depth_t rec_dep = 0
) {
// Try solving
SolveStepRes init_stat = try_solving(s_data);
if (init_stat == Invalid) {
return std::make_pair(InvalidSolution, rec_dep);
}
else if (solved(s_data)) {
return std::make_pair(UniqueSolution, rec_dep);
}
// Solve by guessing recursively
sudoku_data_t s_data_copy = s_data;
sudoku_data_t s_data_res = s_data;
const sudoku_size_t cell_picked = find_least_uncertain_cell(s_data);
SolveResultFinal res = UnknownSolution;
sudoku_size_t num_sols = 0;
rec_depth_t curr_min_rd = -3;
rec_depth_t res_rd = -3;
// Random Order
std::array<sudoku_value_t, side_len> perm;
if constexpr (random_order) {
for (sudoku_size_t i = 0; i < side_len; ++i) {
perm[i] = i;
}
std::shuffle(perm.begin(), perm.end(), rng);
}
// Loop over all possible guesses
for (sudoku_size_t i = 0; i < side_len; ++i) {
const sudoku_size_t curr_i = random_order ? perm[i]: i;
if (s_data[cell_picked + 1 + curr_i] == 2) {
// Copy data and set guessed value
s_data_copy = s_data;
s_data_copy[cell_picked] = curr_i + 1;
// Recursion
auto[res, res_rd] = solve_brute_force_multiple_random<random_order, printDebugInfo>(
s_data_copy, rng, rec_dep + 1);
if (res == UniqueSolution) {
s_data_res = s_data_copy;
num_sols += 1;
if (curr_min_rd == -3 || res_rd < curr_min_rd) {
curr_min_rd = res;
}
}
if (num_sols > 1 || res == MultipleSolution) {
s_data = s_data_copy;
return std::make_pair(MultipleSolution, -1);
}
}
}
s_data = s_data_res;
if (num_sols == 1) {
return std::make_pair(UniqueSolution, curr_min_rd);
}
if (num_sols == 0) {
return std::make_pair(InvalidSolution, -2);
}
if (num_sols > 1) {
return std::make_pair(MultipleSolution, curr_min_rd);
}
// Should not happen
return std::make_pair(UnknownSolution, -3);
}
public:
/// Default Constructor.
SudokuHandler() {};
/// Construct from raw sudoku.
SudokuHandler(raw_sudoku_t raw_sud) {
set_sudoku(raw_sud);
};
/// Set the sudoku.
void set_sudoku(raw_sudoku_t raw_sud) {
this->raw_sud = raw_sud;
sud_data = init_sudoku_with_raw(raw_sud);
auto_fill(this->sud_data, true);
}
/// Solves the loaded sudoku.
FullSol_t solve(bool random_order = false) {
FullSol_t sol;
if (random_order) {
// Initialize rng and solve.
std::mt19937 gen = std::mt19937(seed);
sol = solve_brute_force_multiple_random<true>(sud_data, gen);
}
else {
int rng = 0; // Dummy RNG.
sol = solve_brute_force_multiple_random<false>(sud_data, rng);
}
return sol;
}
void test_init() const {
assert(this->tot_num_cells == this->side_len * this->side_len);
}
};