-
Notifications
You must be signed in to change notification settings - Fork 1
/
solver.cpp
2425 lines (2315 loc) · 91.8 KB
/
solver.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
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
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/***************************************************************************
A Fast Simple Sudoku Solver by Mladen Dobrichev
For commercial users the actions allowed to this file are limited to erasing.
No limitations to other users, but citing somewhere my name is welcome.
There are parts of code taken from bb_sudoku by Brian Turner. Thank you, Brian!
The entry point is
int solve(char* in, char* out, int mode).
Compiler flags:
-c -O2 -Ob1 -Oi -Ot -Oy -GA -MT -GS- -GR- -Wp64 -Gr
-Qopenmp -Qftz -QxSSE2 -Qopenmp-link:static
Which means:
compile_only, optimize_for_speed, expands_only_functions_marked_as_inline,
use_intrinsic_functions, favor_fast_code, omit_frame_pointers, fast_access_to_tls_storage,
multithreaded_executable, no_buffer_security_check, disable_runtime_type_info,
detect_64-bit_portability_issues, use__fastcall_calling_convention,
generate_parallel_code, flush_denormal_fp_result_to_zero, use_SSE2_instructions,
link_statically_to_openmp
Some counterintuitive compilation hints:
- no global optimizations, only for speed
- no inlining
- no interprocedural optimizations
- no Profile Guided Optimizations
Therminology:
"Game" is the puzzle along with the whole context during processing.
Puzzle consist of 81 "Cells".
Each cell is member of three "Groups" - its "Row", "Column", and "Square".
The rest of the cells within the same row, column, and square are "Affected Cells".
Segment is a combination of first/second/third three rows or columns.
"Triplets" are first/second/third subsequent 3 cells within the rows and columns.
"Affected Triplets" for a given triplet are the rest of the triplets in the same row/col and square.
***************************************************************************/
//No global variables
//No runtime integer division or multiplication
//Runtime Library function calls are limited to memcpy (hidden, when assigning the game struct), setjmp, and longjmp.
//There is an Intel Compiler specific pragma "unroll" which causes significant improvement in the SetDigit function.
//It is critical this loop to be unrolled to obtain good performance. Splitting the loop into chunks
//may cause other compilers to unroll them. Last resort is manual unrolling.
//#define __INTEL_COMPILER
//#define USE_LOCKED_CANDIDATES //a bit slower
#ifndef __INTEL_COMPILER
#define MANUAL_UNROLL
#endif //__INTEL_COMPILER
#include <limits.h>
#include "solver.h"
#include "tables.h"
//#include "grid.h" //for unavoidables
#include "rowminlex.h" //for essentially different solutions
//Use constants and tabular functions whenever possible
//Convert mask to a number (1 to 9) if only the appropriate bit is set.
//Convert zero to 9, allowing cellDigit to char conversion using the same table.
//Convert all the rest to zero.
extern const unsigned int Bitmap2Digit[512] =
{
9,1,2,0,3,0,0,0,4,0,0,0,0,0,0,0,5,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
6,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
7,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
8,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
9,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
};
/* Zero based cell enumeration
9 10 11 12 13 14 15 16 17 Group
00 01 02 03 04 05 06 07 08 0
09 10 11 12 13 14 15 16 17 1 18 19 20
18 19 20 21 22 23 24 25 26 2
27 28 29 30 31 32 33 34 35 3
36 37 38 39 40 41 42 43 44 4 21 22 23
45 46 47 48 49 50 51 52 53 5
54 55 56 57 58 59 60 61 62 6
63 64 65 66 67 68 69 70 71 7 24 25 26
72 73 74 75 76 77 78 79 80 8
*/
//#ifdef USE_LOCKED_CANDIDATES
/*
Segment enumeration
0 0 0 3 4 5
1 1 1 3 4 5
2 2 2 3 4 5
Triplets within a segment enumeration
000 111 222
333 444 555
666 777 888
*/
static const int affectedTriplets[9][4] =
{{1,2,3,6},{0,2,4,7},{0,1,5,8},{4,5,0,6},{3,5,1,7},{3,4,2,8},{7,8,0,3},{6,8,1,4},{6,7,2,5}};
//6 segments * 9 triplets * 12 affected cells
static const int tripletAffectedCells[6][9][12] =
{
{
{ 3, 4, 5, 6, 7, 8, 9,10,11,18,19,20},{ 0, 1, 2, 6, 7, 8,12,13,14,21,22,23},{ 0, 1, 2, 3, 4, 5,15,16,17,24,25,26},
{12,13,14,15,16,17, 0, 1, 2,18,19,20},{ 9,10,11,15,16,17, 3, 4, 5,21,22,23},{ 9,10,11,12,13,14, 6, 7, 8,24,25,26},
{21,22,23,24,25,26, 0, 1, 2, 9,10,11},{18,19,20,24,25,26, 3, 4, 5,12,13,14},{18,19,20,21,22,23, 6, 7, 8,15,16,17}
},{
{30,31,32,33,34,35,36,37,38,45,46,47},{27,28,29,33,34,35,39,40,41,48,49,50},{27,28,29,30,31,32,42,43,44,51,52,53},
{39,40,41,42,43,44,27,28,29,45,46,47},{36,37,38,42,43,44,30,31,32,48,49,50},{36,37,38,39,40,41,33,34,35,51,52,53},
{48,49,50,51,52,53,27,28,29,36,37,38},{45,46,47,51,52,53,30,31,32,39,40,41},{45,46,47,48,49,50,33,34,35,42,43,44}
},{
{57,58,59,60,61,62,63,64,65,72,73,74},{54,55,56,60,61,62,66,67,68,75,76,77},{54,55,56,57,58,59,69,70,71,78,79,80},
{66,67,68,69,70,71,54,55,56,72,73,74},{63,64,65,69,70,71,57,58,59,75,76,77},{63,64,65,66,67,68,60,61,62,78,79,80},
{75,76,77,78,79,80,54,55,56,63,64,65},{72,73,74,78,79,80,57,58,59,66,67,68},{72,73,74,75,76,77,60,61,62,69,70,71}
},{
{27,36,45,54,63,72, 1,10,19, 2,11,20},{ 0, 9,18,54,63,72,28,37,46,29,38,47},{ 0, 9,18,27,36,45,55,64,73,56,65,74},
{28,37,46,55,64,73, 0, 9,18, 2,11,20},{ 1,10,19,55,64,73,27,36,45,29,38,47},{ 1,10,19,28,37,46,54,63,72,56,65,74},
{29,38,47,56,65,74, 0, 9,18, 1,10,19},{ 2,11,20,56,65,74,27,36,45,28,37,46},{ 2,11,20,29,38,47,54,63,72,55,64,73}
},{
{30,39,48,57,66,75, 4,13,22, 5,14,23},{ 3,12,21,57,66,75,31,40,49,32,41,50},{ 3,12,21,30,39,48,58,67,76,59,68,77},
{31,40,49,58,67,76, 3,12,21, 5,14,23},{ 4,13,22,58,67,76,30,39,48,32,41,50},{ 4,13,22,31,40,49,57,66,75,59,68,77},
{32,41,50,59,68,77, 3,12,21, 4,13,22},{ 5,14,23,59,68,77,30,39,48,31,40,49},{ 5,14,23,32,41,50,57,66,75,58,67,76}
},{
{33,42,51,60,69,78, 7,16,25, 8,17,26},{ 6,15,24,60,69,78,34,43,52,35,44,53},{ 6,15,24,33,42,51,61,70,79,62,71,80},
{34,43,52,61,70,79, 6,15,24, 8,17,26},{ 7,16,25,61,70,79,33,42,51,35,44,53},{ 7,16,25,34,43,52,60,69,78,62,71,80},
{35,44,53,62,71,80, 6,15,24, 7,16,25},{ 8,17,26,62,71,80,33,42,51,34,43,52},{ 8,17,26,35,44,53,60,69,78,61,70,79}
}
};
//6 segments * 9 triplets * 3 cells in triplet
static const int tripletCells[6][9][3] =
{
{{ 0, 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}},
{{ 0, 9,18},{27,36,45},{54,63,72},{ 1,10,19},{28,37,46},{55,64,73},{ 2,11,20},{29,38,47},{56,65,74}},
{{ 3,12,21},{30,39,48},{57,66,75},{ 4,13,22},{31,40,49},{58,67,76},{ 5,14,23},{32,41,50},{59,68,77}},
{{ 6,15,24},{33,42,51},{60,69,78},{ 7,16,25},{34,43,52},{61,70,79},{ 8,17,26},{35,44,53},{62,71,80}}
};
//#endif //USE_LOCKED_CANDIDATES
//game mode flags
#define MODE_SOLVING 0 //unused, keep solving
//#define MODE_STORE_SOLUTIONS 1 //initial 0
#define MODE_STOP_PROCESSING 4 //solved or errored
#define MODE_STOP_GUESSING 8 //necessary solutions found
#define MODE_EDIFFERENT 16 //stop at first essentially different solution
//The whole game context, cloned before guessing, and copied back after successful guess.
struct game
{
bitmap groupKnownDigits[27]; //initial 0
bitmap cellPossibilities[81]; //0==known, initial 511=0x01FF
int mode; //combination of the game mode flags, initial 0
int cellsLeft; //initial 81
int lastGuess; //initial somewhere in the middle
//int nSolutions;
unsigned long long nSolutions;
//int maxSolutions;
unsigned long long maxSolutions;
//game *masterGame; //sometimes we are jumping directly to g->masterGame->done
cellDigit *cellDigits;
char *results; //external buffer for solutions
uaCollector *uaColl; //callback class for processing the solutions one by one
const int *knownSolution; //pointer to a bitmapped solution to compare with or to guess from
int *pencilmarks; //pointer to a bitmapped possibilities for multisolution puzzle
//int guessDepth;
//int *maxGuessDepth;
};
//A template used for game structure initialization.
static const game defaultGame =
{
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, //groupKnownDigits
{ //cellPossibilities
511,511,511,511,511,511,511,511,511,511,511,511,511,511,511,511,511,511,511,511,511,511,511,511,511,511,511,
511,511,511,511,511,511,511,511,511,511,511,511,511,511,511,511,511,511,511,511,511,511,511,511,511,511,511,
511,511,511,511,511,511,511,511,511,511,511,511,511,511,511,511,511,511,511,511,511,511,511,511,511,511,511
},
MODE_SOLVING,
81, //cellsLeft
40, //lastGuess
0, //nSolutions
2, //maxSolutions
//NULL, //&masterGame
NULL, //&cellDigits
NULL, //&results
NULL, //&uaCollector
NULL, //&knownSolution
NULL, //&pencilmarks
//0, //guessDepth
//NULL //&maxGuessDepth
};
//extern int z0, z1;
void solutionFound(game &g) {
if(g.mode & MODE_EDIFFERENT) { //check whether solution is essentially different from knownSolution
char sol[81];
char solCan[81];
int canBM[81];
for(int j = 0; j < 81; j++) {
sol[j] = Bitmap2Digit[g.cellDigits[j]];
}
rowminlex(sol, solCan);
digit2bitmap(solCan, canBM);
if(memcmp(g.knownSolution, canBM, 81 * sizeof(int))) { //different
g.nSolutions = 1;
g.mode |= (MODE_STOP_PROCESSING | MODE_STOP_GUESSING); //force exit
return;
}
g.mode |= MODE_STOP_PROCESSING; //treat isomorphs a s no-solution
return;
}
else if(g.pencilmarks) { //compose pencilmarks
for(int j = 0; j < 81; j++) {
if(g.cellDigits[j]) {
g.pencilmarks[j] |= g.cellDigits[j];
}
else { //special case for 9
g.pencilmarks[j] |= 256;
}
}
}
else if(g.uaColl) {//collect UA sets
if (g.nSolutions) {//callback for each solution except the first
char sol[81];
for(int j = 0; j < 81; j++) {
sol[j] = Bitmap2Digit[g.cellDigits[j]];
}
if(g.uaColl->addSolution(sol)) { //limit reached
//g->nSolutions = g->maxSolutions;
g.mode |= (MODE_STOP_PROCESSING | MODE_STOP_GUESSING); //force exit
return;
}
}
}
else if(g.results) {//add the solution to the external list
for(int j = 0; j < 81; j++) {
g.results[81 * g.nSolutions + j] = Bitmap2Digit[g.cellDigits[j]];
}
}
g.nSolutions++;
//if(g.nSolutions % 1000000 == 0) printf("."); //debug
if(g.nSolutions >= g.maxSolutions) {//no more guesses, everything is done
g.mode |= (MODE_STOP_PROCESSING | MODE_STOP_GUESSING);
return;
}
g.mode |= MODE_STOP_PROCESSING;
}
//Set a known digit at a known cell.
//Update (clear) possibilities at affected cells and call recursively
//when a single possibility is found.
//Update some other context
static void setDigit(game &g, const cellIndex pi, const bitmap pbm)
{
bitmap *const gkd = g.groupKnownDigits;
bitmap *const cp = g.cellPossibilities;
cellIndex qi[82];
bitmap qbm[82];
int qTop = 0;
int n = 0;
//qi[0] = pi; qbm[0] = pbm;
int i = pi;
int bm = pbm;
//setting the same digit for second time may corrupt the rest of the data
//if(cp[pi] == 0) { //puzzle is overdetermined
// return;
//}
//process_queue:
do {
if(cp[i] == 0) continue; //silently ignore setting of already solved cell
//if((cp[i] & bm) == 0) goto ret_stop; //never happens
const int *const ag = affectedGroups[i];
//if the digit we are setting has been previously set for one of
//the 3 groups the cell is member of, we are in a wrong way
if(0 == (bm & (gkd[ag[0]] | gkd[ag[1]] | gkd[ag[2]]))) {
;
}
else {
goto ret_stop;
}
cp[i] = 0; //clear the mask to exclude this cell from further processing
//if we want the final solution, store the digit for printing
if(g.cellDigits) {
g.cellDigits[i] = (cellDigit)bm; //set the digit. Bit 8 is lost, translating 9 to 0.
}
if(--g.cellsLeft) {
;
}
else {//solved
solutionFound(g);
return;
}
//set the digit as solved for 3 groups
gkd[ag[0]] |= bm; gkd[ag[1]] |= bm; gkd[ag[2]] |= bm;
//clear the known digit from the possibilities of all related cells
const cellIndex *const ac = affectedCells[i];
//#define clp( ci ) \
// {\
// bitmap &bbm = cp[ac[ci]];\
// if(0 == (bbm & bm)) {\
// ;\
// }\
// else {\
// bbm ^= bm;\
// if(bbm) {\
// if(0 == Bitmap2Digit[bbm]) {\
// ;\
// }\
// else {\
// qi[qTop] = ac[ci], qbm[qTop++] = bbm;\
// }\
// }\
// else {\
// goto ret_stop;\
// }\
// }\
// }
#define CCELL( acci ) \
{\
bitmap &bbm = cp[acci];\
if(0 == (bbm & bm)) {\
;\
}\
else {\
bbm ^= bm;\
if(bbm) {\
if(0 == Bitmap2Digit[bbm]) {\
;\
}\
else {\
qi[qTop] = acci, qbm[qTop++] = bbm;\
}\
}\
else {\
goto ret_stop;\
}\
}\
}
#define clp( ci ) {CCELL( ac[ci] )}
clp( 0);clp( 1);clp( 2);clp( 3);clp( 4);clp( 5);clp( 6);clp( 7);clp( 8);clp( 9);
clp(10);clp(11);clp(12);clp(13);clp(14);clp(15);clp(16);clp(17);clp(18);clp(19);
//switch(i)
//{
//case 0: CCELL(1);CCELL( 2);CCELL( 3);CCELL( 4);CCELL( 5);CCELL( 6);CCELL( 7);CCELL( 8);CCELL( 9);CCELL(10);CCELL(11);CCELL(18);CCELL(19);CCELL(20);CCELL(27);CCELL(36);CCELL(45);CCELL(54);CCELL(63);CCELL(72);break;
//case 1: CCELL(0);CCELL( 2);CCELL( 3);CCELL( 4);CCELL( 5);CCELL( 6);CCELL( 7);CCELL( 8);CCELL( 9);CCELL(10);CCELL(11);CCELL(18);CCELL(19);CCELL(20);CCELL(28);CCELL(37);CCELL(46);CCELL(55);CCELL(64);CCELL(73);break;
//case 2: CCELL(0);CCELL( 1);CCELL( 3);CCELL( 4);CCELL( 5);CCELL( 6);CCELL( 7);CCELL( 8);CCELL( 9);CCELL(10);CCELL(11);CCELL(18);CCELL(19);CCELL(20);CCELL(29);CCELL(38);CCELL(47);CCELL(56);CCELL(65);CCELL(74);break;
//case 3: CCELL(0);CCELL( 1);CCELL( 2);CCELL( 4);CCELL( 5);CCELL( 6);CCELL( 7);CCELL( 8);CCELL(12);CCELL(13);CCELL(14);CCELL(21);CCELL(22);CCELL(23);CCELL(30);CCELL(39);CCELL(48);CCELL(57);CCELL(66);CCELL(75);break;
//case 4: CCELL(0);CCELL( 1);CCELL( 2);CCELL( 3);CCELL( 5);CCELL( 6);CCELL( 7);CCELL( 8);CCELL(12);CCELL(13);CCELL(14);CCELL(21);CCELL(22);CCELL(23);CCELL(31);CCELL(40);CCELL(49);CCELL(58);CCELL(67);CCELL(76);break;
//case 5: CCELL(0);CCELL( 1);CCELL( 2);CCELL( 3);CCELL( 4);CCELL( 6);CCELL( 7);CCELL( 8);CCELL(12);CCELL(13);CCELL(14);CCELL(21);CCELL(22);CCELL(23);CCELL(32);CCELL(41);CCELL(50);CCELL(59);CCELL(68);CCELL(77);break;
//case 6: CCELL(0);CCELL( 1);CCELL( 2);CCELL( 3);CCELL( 4);CCELL( 5);CCELL( 7);CCELL( 8);CCELL(15);CCELL(16);CCELL(17);CCELL(24);CCELL(25);CCELL(26);CCELL(33);CCELL(42);CCELL(51);CCELL(60);CCELL(69);CCELL(78);break;
//case 7: CCELL(0);CCELL( 1);CCELL( 2);CCELL( 3);CCELL( 4);CCELL( 5);CCELL( 6);CCELL( 8);CCELL(15);CCELL(16);CCELL(17);CCELL(24);CCELL(25);CCELL(26);CCELL(34);CCELL(43);CCELL(52);CCELL(61);CCELL(70);CCELL(79);break;
//case 8: CCELL(0);CCELL( 1);CCELL( 2);CCELL( 3);CCELL( 4);CCELL( 5);CCELL( 6);CCELL( 7);CCELL(15);CCELL(16);CCELL(17);CCELL(24);CCELL(25);CCELL(26);CCELL(35);CCELL(44);CCELL(53);CCELL(62);CCELL(71);CCELL(80);break;
//case 9: CCELL(0);CCELL( 1);CCELL( 2);CCELL(10);CCELL(11);CCELL(12);CCELL(13);CCELL(14);CCELL(15);CCELL(16);CCELL(17);CCELL(18);CCELL(19);CCELL(20);CCELL(27);CCELL(36);CCELL(45);CCELL(54);CCELL(63);CCELL(72);break;
//case 10: CCELL(0);CCELL( 1);CCELL( 2);CCELL( 9);CCELL(11);CCELL(12);CCELL(13);CCELL(14);CCELL(15);CCELL(16);CCELL(17);CCELL(18);CCELL(19);CCELL(20);CCELL(28);CCELL(37);CCELL(46);CCELL(55);CCELL(64);CCELL(73);break;
//case 11: CCELL(0);CCELL( 1);CCELL( 2);CCELL( 9);CCELL(10);CCELL(12);CCELL(13);CCELL(14);CCELL(15);CCELL(16);CCELL(17);CCELL(18);CCELL(19);CCELL(20);CCELL(29);CCELL(38);CCELL(47);CCELL(56);CCELL(65);CCELL(74);break;
//case 12: CCELL(3);CCELL( 4);CCELL( 5);CCELL( 9);CCELL(10);CCELL(11);CCELL(13);CCELL(14);CCELL(15);CCELL(16);CCELL(17);CCELL(21);CCELL(22);CCELL(23);CCELL(30);CCELL(39);CCELL(48);CCELL(57);CCELL(66);CCELL(75);break;
//case 13: CCELL(3);CCELL( 4);CCELL( 5);CCELL( 9);CCELL(10);CCELL(11);CCELL(12);CCELL(14);CCELL(15);CCELL(16);CCELL(17);CCELL(21);CCELL(22);CCELL(23);CCELL(31);CCELL(40);CCELL(49);CCELL(58);CCELL(67);CCELL(76);break;
//case 14: CCELL(3);CCELL( 4);CCELL( 5);CCELL( 9);CCELL(10);CCELL(11);CCELL(12);CCELL(13);CCELL(15);CCELL(16);CCELL(17);CCELL(21);CCELL(22);CCELL(23);CCELL(32);CCELL(41);CCELL(50);CCELL(59);CCELL(68);CCELL(77);break;
//case 15: CCELL(6);CCELL( 7);CCELL( 8);CCELL( 9);CCELL(10);CCELL(11);CCELL(12);CCELL(13);CCELL(14);CCELL(16);CCELL(17);CCELL(24);CCELL(25);CCELL(26);CCELL(33);CCELL(42);CCELL(51);CCELL(60);CCELL(69);CCELL(78);break;
//case 16: CCELL(6);CCELL( 7);CCELL( 8);CCELL( 9);CCELL(10);CCELL(11);CCELL(12);CCELL(13);CCELL(14);CCELL(15);CCELL(17);CCELL(24);CCELL(25);CCELL(26);CCELL(34);CCELL(43);CCELL(52);CCELL(61);CCELL(70);CCELL(79);break;
//case 17: CCELL(6);CCELL( 7);CCELL( 8);CCELL( 9);CCELL(10);CCELL(11);CCELL(12);CCELL(13);CCELL(14);CCELL(15);CCELL(16);CCELL(24);CCELL(25);CCELL(26);CCELL(35);CCELL(44);CCELL(53);CCELL(62);CCELL(71);CCELL(80);break;
//case 18: CCELL(0);CCELL( 1);CCELL( 2);CCELL( 9);CCELL(10);CCELL(11);CCELL(19);CCELL(20);CCELL(21);CCELL(22);CCELL(23);CCELL(24);CCELL(25);CCELL(26);CCELL(27);CCELL(36);CCELL(45);CCELL(54);CCELL(63);CCELL(72);break;
//case 19: CCELL(0);CCELL( 1);CCELL( 2);CCELL( 9);CCELL(10);CCELL(11);CCELL(18);CCELL(20);CCELL(21);CCELL(22);CCELL(23);CCELL(24);CCELL(25);CCELL(26);CCELL(28);CCELL(37);CCELL(46);CCELL(55);CCELL(64);CCELL(73);break;
//case 20: CCELL(0);CCELL( 1);CCELL( 2);CCELL( 9);CCELL(10);CCELL(11);CCELL(18);CCELL(19);CCELL(21);CCELL(22);CCELL(23);CCELL(24);CCELL(25);CCELL(26);CCELL(29);CCELL(38);CCELL(47);CCELL(56);CCELL(65);CCELL(74);break;
//case 21: CCELL(3);CCELL( 4);CCELL( 5);CCELL(12);CCELL(13);CCELL(14);CCELL(18);CCELL(19);CCELL(20);CCELL(22);CCELL(23);CCELL(24);CCELL(25);CCELL(26);CCELL(30);CCELL(39);CCELL(48);CCELL(57);CCELL(66);CCELL(75);break;
//case 22: CCELL(3);CCELL( 4);CCELL( 5);CCELL(12);CCELL(13);CCELL(14);CCELL(18);CCELL(19);CCELL(20);CCELL(21);CCELL(23);CCELL(24);CCELL(25);CCELL(26);CCELL(31);CCELL(40);CCELL(49);CCELL(58);CCELL(67);CCELL(76);break;
//case 23: CCELL(3);CCELL( 4);CCELL( 5);CCELL(12);CCELL(13);CCELL(14);CCELL(18);CCELL(19);CCELL(20);CCELL(21);CCELL(22);CCELL(24);CCELL(25);CCELL(26);CCELL(32);CCELL(41);CCELL(50);CCELL(59);CCELL(68);CCELL(77);break;
//case 24: CCELL(6);CCELL( 7);CCELL( 8);CCELL(15);CCELL(16);CCELL(17);CCELL(18);CCELL(19);CCELL(20);CCELL(21);CCELL(22);CCELL(23);CCELL(25);CCELL(26);CCELL(33);CCELL(42);CCELL(51);CCELL(60);CCELL(69);CCELL(78);break;
//case 25: CCELL(6);CCELL( 7);CCELL( 8);CCELL(15);CCELL(16);CCELL(17);CCELL(18);CCELL(19);CCELL(20);CCELL(21);CCELL(22);CCELL(23);CCELL(24);CCELL(26);CCELL(34);CCELL(43);CCELL(52);CCELL(61);CCELL(70);CCELL(79);break;
//case 26: CCELL(6);CCELL( 7);CCELL( 8);CCELL(15);CCELL(16);CCELL(17);CCELL(18);CCELL(19);CCELL(20);CCELL(21);CCELL(22);CCELL(23);CCELL(24);CCELL(25);CCELL(35);CCELL(44);CCELL(53);CCELL(62);CCELL(71);CCELL(80);break;
//case 27: CCELL(0);CCELL( 9);CCELL(18);CCELL(28);CCELL(29);CCELL(30);CCELL(31);CCELL(32);CCELL(33);CCELL(34);CCELL(35);CCELL(36);CCELL(37);CCELL(38);CCELL(45);CCELL(46);CCELL(47);CCELL(54);CCELL(63);CCELL(72);break;
//case 28: CCELL(1);CCELL(10);CCELL(19);CCELL(27);CCELL(29);CCELL(30);CCELL(31);CCELL(32);CCELL(33);CCELL(34);CCELL(35);CCELL(36);CCELL(37);CCELL(38);CCELL(45);CCELL(46);CCELL(47);CCELL(55);CCELL(64);CCELL(73);break;
//case 29: CCELL(2);CCELL(11);CCELL(20);CCELL(27);CCELL(28);CCELL(30);CCELL(31);CCELL(32);CCELL(33);CCELL(34);CCELL(35);CCELL(36);CCELL(37);CCELL(38);CCELL(45);CCELL(46);CCELL(47);CCELL(56);CCELL(65);CCELL(74);break;
//case 30: CCELL(3);CCELL(12);CCELL(21);CCELL(27);CCELL(28);CCELL(29);CCELL(31);CCELL(32);CCELL(33);CCELL(34);CCELL(35);CCELL(39);CCELL(40);CCELL(41);CCELL(48);CCELL(49);CCELL(50);CCELL(57);CCELL(66);CCELL(75);break;
//case 31: CCELL(4);CCELL(13);CCELL(22);CCELL(27);CCELL(28);CCELL(29);CCELL(30);CCELL(32);CCELL(33);CCELL(34);CCELL(35);CCELL(39);CCELL(40);CCELL(41);CCELL(48);CCELL(49);CCELL(50);CCELL(58);CCELL(67);CCELL(76);break;
//case 32: CCELL(5);CCELL(14);CCELL(23);CCELL(27);CCELL(28);CCELL(29);CCELL(30);CCELL(31);CCELL(33);CCELL(34);CCELL(35);CCELL(39);CCELL(40);CCELL(41);CCELL(48);CCELL(49);CCELL(50);CCELL(59);CCELL(68);CCELL(77);break;
//case 33: CCELL(6);CCELL(15);CCELL(24);CCELL(27);CCELL(28);CCELL(29);CCELL(30);CCELL(31);CCELL(32);CCELL(34);CCELL(35);CCELL(42);CCELL(43);CCELL(44);CCELL(51);CCELL(52);CCELL(53);CCELL(60);CCELL(69);CCELL(78);break;
//case 34: CCELL(7);CCELL(16);CCELL(25);CCELL(27);CCELL(28);CCELL(29);CCELL(30);CCELL(31);CCELL(32);CCELL(33);CCELL(35);CCELL(42);CCELL(43);CCELL(44);CCELL(51);CCELL(52);CCELL(53);CCELL(61);CCELL(70);CCELL(79);break;
//case 35: CCELL(8);CCELL(17);CCELL(26);CCELL(27);CCELL(28);CCELL(29);CCELL(30);CCELL(31);CCELL(32);CCELL(33);CCELL(34);CCELL(42);CCELL(43);CCELL(44);CCELL(51);CCELL(52);CCELL(53);CCELL(62);CCELL(71);CCELL(80);break;
//case 36: CCELL(0);CCELL( 9);CCELL(18);CCELL(27);CCELL(28);CCELL(29);CCELL(37);CCELL(38);CCELL(39);CCELL(40);CCELL(41);CCELL(42);CCELL(43);CCELL(44);CCELL(45);CCELL(46);CCELL(47);CCELL(54);CCELL(63);CCELL(72);break;
//case 37: CCELL(1);CCELL(10);CCELL(19);CCELL(27);CCELL(28);CCELL(29);CCELL(36);CCELL(38);CCELL(39);CCELL(40);CCELL(41);CCELL(42);CCELL(43);CCELL(44);CCELL(45);CCELL(46);CCELL(47);CCELL(55);CCELL(64);CCELL(73);break;
//case 38: CCELL(2);CCELL(11);CCELL(20);CCELL(27);CCELL(28);CCELL(29);CCELL(36);CCELL(37);CCELL(39);CCELL(40);CCELL(41);CCELL(42);CCELL(43);CCELL(44);CCELL(45);CCELL(46);CCELL(47);CCELL(56);CCELL(65);CCELL(74);break;
//case 39: CCELL(3);CCELL(12);CCELL(21);CCELL(30);CCELL(31);CCELL(32);CCELL(36);CCELL(37);CCELL(38);CCELL(40);CCELL(41);CCELL(42);CCELL(43);CCELL(44);CCELL(48);CCELL(49);CCELL(50);CCELL(57);CCELL(66);CCELL(75);break;
//case 40: CCELL(4);CCELL(13);CCELL(22);CCELL(30);CCELL(31);CCELL(32);CCELL(36);CCELL(37);CCELL(38);CCELL(39);CCELL(41);CCELL(42);CCELL(43);CCELL(44);CCELL(48);CCELL(49);CCELL(50);CCELL(58);CCELL(67);CCELL(76);break;
//case 41: CCELL(5);CCELL(14);CCELL(23);CCELL(30);CCELL(31);CCELL(32);CCELL(36);CCELL(37);CCELL(38);CCELL(39);CCELL(40);CCELL(42);CCELL(43);CCELL(44);CCELL(48);CCELL(49);CCELL(50);CCELL(59);CCELL(68);CCELL(77);break;
//case 42: CCELL(6);CCELL(15);CCELL(24);CCELL(33);CCELL(34);CCELL(35);CCELL(36);CCELL(37);CCELL(38);CCELL(39);CCELL(40);CCELL(41);CCELL(43);CCELL(44);CCELL(51);CCELL(52);CCELL(53);CCELL(60);CCELL(69);CCELL(78);break;
//case 43: CCELL(7);CCELL(16);CCELL(25);CCELL(33);CCELL(34);CCELL(35);CCELL(36);CCELL(37);CCELL(38);CCELL(39);CCELL(40);CCELL(41);CCELL(42);CCELL(44);CCELL(51);CCELL(52);CCELL(53);CCELL(61);CCELL(70);CCELL(79);break;
//case 44: CCELL(8);CCELL(17);CCELL(26);CCELL(33);CCELL(34);CCELL(35);CCELL(36);CCELL(37);CCELL(38);CCELL(39);CCELL(40);CCELL(41);CCELL(42);CCELL(43);CCELL(51);CCELL(52);CCELL(53);CCELL(62);CCELL(71);CCELL(80);break;
//case 45: CCELL(0);CCELL( 9);CCELL(18);CCELL(27);CCELL(28);CCELL(29);CCELL(36);CCELL(37);CCELL(38);CCELL(46);CCELL(47);CCELL(48);CCELL(49);CCELL(50);CCELL(51);CCELL(52);CCELL(53);CCELL(54);CCELL(63);CCELL(72);break;
//case 46: CCELL(1);CCELL(10);CCELL(19);CCELL(27);CCELL(28);CCELL(29);CCELL(36);CCELL(37);CCELL(38);CCELL(45);CCELL(47);CCELL(48);CCELL(49);CCELL(50);CCELL(51);CCELL(52);CCELL(53);CCELL(55);CCELL(64);CCELL(73);break;
//case 47: CCELL(2);CCELL(11);CCELL(20);CCELL(27);CCELL(28);CCELL(29);CCELL(36);CCELL(37);CCELL(38);CCELL(45);CCELL(46);CCELL(48);CCELL(49);CCELL(50);CCELL(51);CCELL(52);CCELL(53);CCELL(56);CCELL(65);CCELL(74);break;
//case 48: CCELL(3);CCELL(12);CCELL(21);CCELL(30);CCELL(31);CCELL(32);CCELL(39);CCELL(40);CCELL(41);CCELL(45);CCELL(46);CCELL(47);CCELL(49);CCELL(50);CCELL(51);CCELL(52);CCELL(53);CCELL(57);CCELL(66);CCELL(75);break;
//case 49: CCELL(4);CCELL(13);CCELL(22);CCELL(30);CCELL(31);CCELL(32);CCELL(39);CCELL(40);CCELL(41);CCELL(45);CCELL(46);CCELL(47);CCELL(48);CCELL(50);CCELL(51);CCELL(52);CCELL(53);CCELL(58);CCELL(67);CCELL(76);break;
//case 50: CCELL(5);CCELL(14);CCELL(23);CCELL(30);CCELL(31);CCELL(32);CCELL(39);CCELL(40);CCELL(41);CCELL(45);CCELL(46);CCELL(47);CCELL(48);CCELL(49);CCELL(51);CCELL(52);CCELL(53);CCELL(59);CCELL(68);CCELL(77);break;
//case 51: CCELL(6);CCELL(15);CCELL(24);CCELL(33);CCELL(34);CCELL(35);CCELL(42);CCELL(43);CCELL(44);CCELL(45);CCELL(46);CCELL(47);CCELL(48);CCELL(49);CCELL(50);CCELL(52);CCELL(53);CCELL(60);CCELL(69);CCELL(78);break;
//case 52: CCELL(7);CCELL(16);CCELL(25);CCELL(33);CCELL(34);CCELL(35);CCELL(42);CCELL(43);CCELL(44);CCELL(45);CCELL(46);CCELL(47);CCELL(48);CCELL(49);CCELL(50);CCELL(51);CCELL(53);CCELL(61);CCELL(70);CCELL(79);break;
//case 53: CCELL(8);CCELL(17);CCELL(26);CCELL(33);CCELL(34);CCELL(35);CCELL(42);CCELL(43);CCELL(44);CCELL(45);CCELL(46);CCELL(47);CCELL(48);CCELL(49);CCELL(50);CCELL(51);CCELL(52);CCELL(62);CCELL(71);CCELL(80);break;
//case 54: CCELL(0);CCELL( 9);CCELL(18);CCELL(27);CCELL(36);CCELL(45);CCELL(55);CCELL(56);CCELL(57);CCELL(58);CCELL(59);CCELL(60);CCELL(61);CCELL(62);CCELL(63);CCELL(64);CCELL(65);CCELL(72);CCELL(73);CCELL(74);break;
//case 55: CCELL(1);CCELL(10);CCELL(19);CCELL(28);CCELL(37);CCELL(46);CCELL(54);CCELL(56);CCELL(57);CCELL(58);CCELL(59);CCELL(60);CCELL(61);CCELL(62);CCELL(63);CCELL(64);CCELL(65);CCELL(72);CCELL(73);CCELL(74);break;
//case 56: CCELL(2);CCELL(11);CCELL(20);CCELL(29);CCELL(38);CCELL(47);CCELL(54);CCELL(55);CCELL(57);CCELL(58);CCELL(59);CCELL(60);CCELL(61);CCELL(62);CCELL(63);CCELL(64);CCELL(65);CCELL(72);CCELL(73);CCELL(74);break;
//case 57: CCELL(3);CCELL(12);CCELL(21);CCELL(30);CCELL(39);CCELL(48);CCELL(54);CCELL(55);CCELL(56);CCELL(58);CCELL(59);CCELL(60);CCELL(61);CCELL(62);CCELL(66);CCELL(67);CCELL(68);CCELL(75);CCELL(76);CCELL(77);break;
//case 58: CCELL(4);CCELL(13);CCELL(22);CCELL(31);CCELL(40);CCELL(49);CCELL(54);CCELL(55);CCELL(56);CCELL(57);CCELL(59);CCELL(60);CCELL(61);CCELL(62);CCELL(66);CCELL(67);CCELL(68);CCELL(75);CCELL(76);CCELL(77);break;
//case 59: CCELL(5);CCELL(14);CCELL(23);CCELL(32);CCELL(41);CCELL(50);CCELL(54);CCELL(55);CCELL(56);CCELL(57);CCELL(58);CCELL(60);CCELL(61);CCELL(62);CCELL(66);CCELL(67);CCELL(68);CCELL(75);CCELL(76);CCELL(77);break;
//case 60: CCELL(6);CCELL(15);CCELL(24);CCELL(33);CCELL(42);CCELL(51);CCELL(54);CCELL(55);CCELL(56);CCELL(57);CCELL(58);CCELL(59);CCELL(61);CCELL(62);CCELL(69);CCELL(70);CCELL(71);CCELL(78);CCELL(79);CCELL(80);break;
//case 61: CCELL(7);CCELL(16);CCELL(25);CCELL(34);CCELL(43);CCELL(52);CCELL(54);CCELL(55);CCELL(56);CCELL(57);CCELL(58);CCELL(59);CCELL(60);CCELL(62);CCELL(69);CCELL(70);CCELL(71);CCELL(78);CCELL(79);CCELL(80);break;
//case 62: CCELL(8);CCELL(17);CCELL(26);CCELL(35);CCELL(44);CCELL(53);CCELL(54);CCELL(55);CCELL(56);CCELL(57);CCELL(58);CCELL(59);CCELL(60);CCELL(61);CCELL(69);CCELL(70);CCELL(71);CCELL(78);CCELL(79);CCELL(80);break;
//case 63: CCELL(0);CCELL( 9);CCELL(18);CCELL(27);CCELL(36);CCELL(45);CCELL(54);CCELL(55);CCELL(56);CCELL(64);CCELL(65);CCELL(66);CCELL(67);CCELL(68);CCELL(69);CCELL(70);CCELL(71);CCELL(72);CCELL(73);CCELL(74);break;
//case 64: CCELL(1);CCELL(10);CCELL(19);CCELL(28);CCELL(37);CCELL(46);CCELL(54);CCELL(55);CCELL(56);CCELL(63);CCELL(65);CCELL(66);CCELL(67);CCELL(68);CCELL(69);CCELL(70);CCELL(71);CCELL(72);CCELL(73);CCELL(74);break;
//case 65: CCELL(2);CCELL(11);CCELL(20);CCELL(29);CCELL(38);CCELL(47);CCELL(54);CCELL(55);CCELL(56);CCELL(63);CCELL(64);CCELL(66);CCELL(67);CCELL(68);CCELL(69);CCELL(70);CCELL(71);CCELL(72);CCELL(73);CCELL(74);break;
//case 66: CCELL(3);CCELL(12);CCELL(21);CCELL(30);CCELL(39);CCELL(48);CCELL(57);CCELL(58);CCELL(59);CCELL(63);CCELL(64);CCELL(65);CCELL(67);CCELL(68);CCELL(69);CCELL(70);CCELL(71);CCELL(75);CCELL(76);CCELL(77);break;
//case 67: CCELL(4);CCELL(13);CCELL(22);CCELL(31);CCELL(40);CCELL(49);CCELL(57);CCELL(58);CCELL(59);CCELL(63);CCELL(64);CCELL(65);CCELL(66);CCELL(68);CCELL(69);CCELL(70);CCELL(71);CCELL(75);CCELL(76);CCELL(77);break;
//case 68: CCELL(5);CCELL(14);CCELL(23);CCELL(32);CCELL(41);CCELL(50);CCELL(57);CCELL(58);CCELL(59);CCELL(63);CCELL(64);CCELL(65);CCELL(66);CCELL(67);CCELL(69);CCELL(70);CCELL(71);CCELL(75);CCELL(76);CCELL(77);break;
//case 69: CCELL(6);CCELL(15);CCELL(24);CCELL(33);CCELL(42);CCELL(51);CCELL(60);CCELL(61);CCELL(62);CCELL(63);CCELL(64);CCELL(65);CCELL(66);CCELL(67);CCELL(68);CCELL(70);CCELL(71);CCELL(78);CCELL(79);CCELL(80);break;
//case 70: CCELL(7);CCELL(16);CCELL(25);CCELL(34);CCELL(43);CCELL(52);CCELL(60);CCELL(61);CCELL(62);CCELL(63);CCELL(64);CCELL(65);CCELL(66);CCELL(67);CCELL(68);CCELL(69);CCELL(71);CCELL(78);CCELL(79);CCELL(80);break;
//case 71: CCELL(8);CCELL(17);CCELL(26);CCELL(35);CCELL(44);CCELL(53);CCELL(60);CCELL(61);CCELL(62);CCELL(63);CCELL(64);CCELL(65);CCELL(66);CCELL(67);CCELL(68);CCELL(69);CCELL(70);CCELL(78);CCELL(79);CCELL(80);break;
//case 72: CCELL(0);CCELL( 9);CCELL(18);CCELL(27);CCELL(36);CCELL(45);CCELL(54);CCELL(55);CCELL(56);CCELL(63);CCELL(64);CCELL(65);CCELL(73);CCELL(74);CCELL(75);CCELL(76);CCELL(77);CCELL(78);CCELL(79);CCELL(80);break;
//case 73: CCELL(1);CCELL(10);CCELL(19);CCELL(28);CCELL(37);CCELL(46);CCELL(54);CCELL(55);CCELL(56);CCELL(63);CCELL(64);CCELL(65);CCELL(72);CCELL(74);CCELL(75);CCELL(76);CCELL(77);CCELL(78);CCELL(79);CCELL(80);break;
//case 74: CCELL(2);CCELL(11);CCELL(20);CCELL(29);CCELL(38);CCELL(47);CCELL(54);CCELL(55);CCELL(56);CCELL(63);CCELL(64);CCELL(65);CCELL(72);CCELL(73);CCELL(75);CCELL(76);CCELL(77);CCELL(78);CCELL(79);CCELL(80);break;
//case 75: CCELL(3);CCELL(12);CCELL(21);CCELL(30);CCELL(39);CCELL(48);CCELL(57);CCELL(58);CCELL(59);CCELL(66);CCELL(67);CCELL(68);CCELL(72);CCELL(73);CCELL(74);CCELL(76);CCELL(77);CCELL(78);CCELL(79);CCELL(80);break;
//case 76: CCELL(4);CCELL(13);CCELL(22);CCELL(31);CCELL(40);CCELL(49);CCELL(57);CCELL(58);CCELL(59);CCELL(66);CCELL(67);CCELL(68);CCELL(72);CCELL(73);CCELL(74);CCELL(75);CCELL(77);CCELL(78);CCELL(79);CCELL(80);break;
//case 77: CCELL(5);CCELL(14);CCELL(23);CCELL(32);CCELL(41);CCELL(50);CCELL(57);CCELL(58);CCELL(59);CCELL(66);CCELL(67);CCELL(68);CCELL(72);CCELL(73);CCELL(74);CCELL(75);CCELL(76);CCELL(78);CCELL(79);CCELL(80);break;
//case 78: CCELL(6);CCELL(15);CCELL(24);CCELL(33);CCELL(42);CCELL(51);CCELL(60);CCELL(61);CCELL(62);CCELL(69);CCELL(70);CCELL(71);CCELL(72);CCELL(73);CCELL(74);CCELL(75);CCELL(76);CCELL(77);CCELL(79);CCELL(80);break;
//case 79: CCELL(7);CCELL(16);CCELL(25);CCELL(34);CCELL(43);CCELL(52);CCELL(60);CCELL(61);CCELL(62);CCELL(69);CCELL(70);CCELL(71);CCELL(72);CCELL(73);CCELL(74);CCELL(75);CCELL(76);CCELL(77);CCELL(78);CCELL(80);break;
//case 80: CCELL(8);CCELL(17);CCELL(26);CCELL(35);CCELL(44);CCELL(53);CCELL(60);CCELL(61);CCELL(62);CCELL(69);CCELL(70);CCELL(71);CCELL(72);CCELL(73);CCELL(74);CCELL(75);CCELL(76);CCELL(77);CCELL(78);CCELL(79);break;
//}
#ifdef SOLVER_BY_GROUPS
while(qTop - n > 3) {
for(int x = n; x < qTop; x++) {
i = qi[x], bm = qbm[x];
if(cp[i] == 0) continue; //silently ignore setting of already solved cell
//if((cp[i] & bm) == 0) goto ret_stop; //never happens
const int *const ag = affectedGroups[i];
const cellIndex *const ac = affectedCells[i];
//if the digit we are setting has been previously set for one of
//the 3 groups the cell is member of, we are in a wrong way
if(bm & (gkd[ag[0]] | gkd[ag[1]] | gkd[ag[2]])) {
goto ret_stop;
}
cp[i] = 0; //clear the mask to exclude this cell from further processing
//if we want the final solution, store the digit for printing
if(g.cellDigits) {
g.cellDigits[i] = (cellDigit)bm; //set the digit. Bit 8 is lost, translating 9 to 0.
}
if(0 == --g.cellsLeft) {//solved
solutionFound(g);
return;
}
//set the digit as solved for 3 groups
gkd[ag[0]] |= bm; gkd[ag[1]] |= bm; gkd[ag[2]] |= bm;
}
//empty the queue
n = qTop = 0;
//now cleanup affected cells by groups
for(int i = 0; i < 81; i++) {
bitmap &bm = cp[i];
const int *ag = affectedGroups[i];
bitmap knowns = gkd[ag[0]] | gkd[ag[1]] | gkd[ag[2]];
if(bm & knowns) { //there are possibilities to be cleared
bm &= ~knowns;
if(bm == 0) { //no possibilities for this cell
g.mode |= MODE_STOP_PROCESSING;
return;
}
if(Bitmap2Digit[bm]) { //single digit?
//setDigit(g, i, bm);
//if(g.mode & MODE_STOP_PROCESSING) return;
qi[qTop] = i, qbm[qTop++] = bm;
}
}
}
}
#endif
} while (i = qi[n], bm = qbm[n++], n <= qTop);
return;
ret_stop:
g.mode |= MODE_STOP_PROCESSING;
return;
}
//#include "minimizer.h"
//static int inline checkForSubsets(game& g) {
// bitmap * cp = g.cellPossibilities;
// const bitmap *const gkd = g.groupKnownDigits;
// int found = 0;
// for(int numBits = 2; numBits < 3; numBits++) {
// //for(int numBits = 2; numBits < 8; numBits++) {
// size_t startMask = (1 << numBits) - 1;
// for(int gi = 0; gi < 27; gi++) {
// const cellIndex *const gc = cellsInGroup[gi];
// size_t unsolved = 511 ^ gkd[gi];
// if(BitCount[unsolved] < 2 * numBits) continue; //useful for naked only
// //identify pairs of interest
// for(size_t mask = startMask; mask < 511; mask = minimizer::nextPerm(mask)) {
// if((mask & unsolved) == mask) {
// int nakedCells = 0;
// int hiddenCells = 0;
// for(int ci = 0; ci < 9; ci++) {
// const int cellPoss = cp[gc[ci]];
// if(0 == cellPoss) continue;
// nakedCells |= (((int)((mask & cellPoss) == cellPoss)) << ci);
// hiddenCells |= (((int)((mask & cellPoss) != 0)) << ci);
// }
// int nNaked = BitCount[nakedCells];
// int nHidden = BitCount[hiddenCells];
// if(nNaked > numBits || nHidden < numBits) { //less cells for more values
// g.mode |= MODE_STOP_PROCESSING;
// return 0;
// }
// if(nNaked < numBits && nHidden > numBits) continue;
// if(nNaked < numBits) nakedCells = 511; //bypass
// if(nHidden > numBits) hiddenCells = 0; //bypass
// for(int ci = 0; ci < 9; ci++) {
// const int& cellPoss = cp[gc[ci]];
// if(cellPoss == 0) continue; //ignore solved cells
// if(hiddenCells & (1 << ci)) { //eliminate cell possibilities from outside the mask if any
// const int newPoss = cellPoss & mask;
// if(newPoss != cellPoss) {
// found = 1;
// //printf("H");
// if(numBits > 4) { //there is something wrong - no complementary subset of size 9-numBits ???
// printf("h");
// //g.mode |= MODE_STOP_PROCESSING;
// //return 0;
// }
// if(Bitmap2Digit[newPoss]) {
// setDigit(g, gc[ci], newPoss);
// if(g.mode & MODE_STOP_PROCESSING) return 0;
// }
// else {
// cp[gc[ci]] = newPoss; //clear some possibilities
// }
// }
// }
// if(0 == (nakedCells & (1 << ci))) { // clear the possibilities within the mask if any
// const int newPoss = cellPoss & ~mask;
// if(newPoss != cellPoss) {
// found = 1;
// //printf("N");
// if(numBits > 4) { //there is something wrong - no complementary subset of size 9-numBits ???
// printf("n");
// //g.mode |= MODE_STOP_PROCESSING;
// //return 0;
// }
// if(newPoss == 0) {
// g.mode |= MODE_STOP_PROCESSING; //no possibility for this cell
// return 0;
// }
// if(Bitmap2Digit[newPoss]) {
// setDigit(g, gc[ci], newPoss);
// if(g.mode & MODE_STOP_PROCESSING) return 0;
// }
// else {
// cp[gc[ci]] = newPoss; //clear some possibilities
// }
// }
// }
// }
// if(found) {
// return 1;
// }
// }
// }
// }
// }
// return 0;
//}
//Loop trough the groups, find the only cell containing particular digit in the group if any,
//then set the digit found.
//Since setting the digit changes the context, repeat until nothing found.
//On exit, the slower algorithms are expected to be run (i.e. guessing).
static void /*__declspec(noinline)*/ checkForLastOccurenceInGroup(game& g)
{
//checks for group having some posible digit in only one cell
const bitmap *const cp = g.cellPossibilities;
const bitmap *const gkd = g.groupKnownDigits;
restart:
for(int gi = 0; gi < 27; gi++) {
//if(gkd[gi] == 511) continue;
//if(BitCount[gkd[gi]] > 6) continue;
int groupPoss = 0;
int duplicates = 0;
const cellIndex *const gc = cellsInGroup[gi];
#ifdef __INTEL_COMPILER
#pragma unroll(9)
#endif //__INTEL_COMPILER
for(int ci = 0; ci < 9; ci++) {
const int cellPoss = cp[gc[ci]];
duplicates |= (groupPoss & cellPoss); //this tricky code is taken from bb_sudoku by Brian Turner
groupPoss |= cellPoss;
}
if((groupPoss ^ gkd[gi]) != 511) { //no place for some of the unknown digits
goto ret_err;
}
int uniques = groupPoss ^ duplicates;
if(uniques == 0) continue;
//clear the unique possibilities from the group and process the unique cells
for(int ci = 0; ci < 9; ci++) {
const int newPoss = cp[gc[ci]] & uniques;
if(newPoss == 0) continue;
//one of the cells of interest found
if(Bitmap2Digit[newPoss]) {
setDigit(g, gc[ci], newPoss);
if(g.mode & MODE_STOP_PROCESSING) return;
goto restart; //usually the rest uniques are directly eliminated in setDigit
////the benefit from the following optimization is questionable
//uniques ^= newPoss; //clear the already processed bit
//if(uniques == 0) goto restart; //no more bits
}
else {
//error: the same cell introduced > 1 unique digits in the group
goto ret_err;
}
}
//goto restart;
} //group loop
////TODO: search for pairs/triplets/quads
//if(checkForSubsets(g))
// goto restart;
return;
ret_err:
g.mode |= MODE_STOP_PROCESSING;
return;
}
//#ifdef USE_LOCKED_CANDIDATES
//Based on bb_sudoku by Brian Turner.
static void FindLockedCandidates (game &g)
{
bitmap *const gcp = g.cellPossibilities;
bitmap tripletPossibilities[9];
restart:
//int found = 0;
for(int i = 0; i < 6; i++) {
for(int j = 0; j < 9; j++)
tripletPossibilities[j] = gcp[tripletCells[i][j][0]] | gcp[tripletCells[i][j][1]] | gcp[tripletCells[i][j][2]];
for(int j = 0; j < 9; j++) {
const int b = (//found in the current triplet, and found in exactly one of the affected triplet pairs
((tripletPossibilities[affectedTriplets[j][0]] | tripletPossibilities[affectedTriplets[j][1]]) ^ //row or column pair
(tripletPossibilities[affectedTriplets[j][2]] | tripletPossibilities[affectedTriplets[j][3]])) //square pair
& tripletPossibilities[j]);
if(b == 0) continue;
//don't care which bit where came from
for(int k = 0; k < 12; k++) { //6 from the row/col + 6 from the square
const int ci = tripletAffectedCells[i][j][k];
bitmap &cp = gcp[ci];
if(0 == (cp & b)) continue;
//there is something to clear
cp = (bitmap)(cp & ~b);
if(cp) {
if(0 == Bitmap2Digit[cp]) {
//found = 1;
continue;
}
//single possibility remains
setDigit(g, ci, cp);
if(g.mode & MODE_STOP_PROCESSING) return;
checkForLastOccurenceInGroup(g);
if(g.mode & MODE_STOP_PROCESSING) return;
goto restart;
}
else {
//no any possibility for this cell
g.mode |= MODE_STOP_PROCESSING;
return;
}
}
} //for j
} //for i
//if(found) {
// checkForLastOccurenceInGroup(g);
// //goto restart;
//}
////experimental: search for pairs/triplets/quads
//checkForLastOccurenceInGroup(g);
//if(checkForSubsets(g)) {
// if(g.mode & MODE_STOP_PROCESSING) return;
// checkForLastOccurenceInGroup(g);
// if(g.mode & MODE_STOP_PROCESSING) return;
// goto restart;
//}
return;
}
//#endif // USE_LOCKED_CANDIDATES
//apply techniques prior to guessing
//useful for reducing the pencilmarks for pseudopuzzles with < 16 givens
static void fastEliminations(game &g) {
do {
checkForLastOccurenceInGroup(g); //the first algorithm performs internal repeating
if(g.mode & MODE_STOP_PROCESSING) return;
}
#ifdef USE_LOCKED_CANDIDATES
while(FindLockedCandidates(g), (g.mode & MODE_STOP_PROCESSING));
#else
while(0);
#endif // USE_LOCKED_CANDIDATES
}
struct pmWeight {
int cell;
int digit;
};
struct pmWeights {
pmWeight w[81 * 9]; //max possible cell/values
int size; //the actual size with solved removed
void init(const game &g) {
size = 0;
//compose a cache digit distribution within the houses
int boxCount[9][9];
int rowCount[9][9]; //row, digit
int colCount[9][9];
//clear
for(int c = 0; c < 9; c++) {
for(int d = 0; d < 9; d++) {
rowCount[c][d] = colCount[c][d] = boxCount[c][d] = 0;
}
}
//count
for(int c = 0; c < 81; c++) {
if(g.cellPossibilities[c]) { //skip solved cells
for(int d = 0; d < 9; d++) {
int bm = 1 << d;
if(g.cellPossibilities[c] & bm) { //unsolved digit in the cell
boxCount[boxByCellIndex[c]][d]++;
rowCount[rowByCellIndex[c]][d]++;
colCount[colByCellIndex[c]][d]++;
}
}
}
}
//populate the non-zero weight reciprocals
int minWeightReciprocal = 9*9*9*9*100; //worst case in empty grid
for(int c = 0; c < 81; c++) {
if(g.cellPossibilities[c]) { //unsolved cell
for(int d = 0; d < 9; d++) {
int bm = 1 << d;
if(g.cellPossibilities[c] & bm) { //unsolved digit in the cell
pmWeight ww;
//pmWeight &ww = w[size];
ww.cell = c;
ww.digit = d;
int weightReciprocal = BitCount[g.cellPossibilities[c]] * boxCount[boxByCellIndex[c]][d] * rowCount[rowByCellIndex[c]][d] * colCount[colByCellIndex[c]][d];
//int weightReciprocal = 100 * (BitCount[g.cellPossibilities[c]] * boxCount[boxByCellIndex[c]][d] * rowCount[rowByCellIndex[c]][d] * colCount[colByCellIndex[c]][d])
// + 10 * boxCount[boxByCellIndex[c]][d]
// + BitCount[g.cellPossibilities[c]];
if(weightReciprocal == 0 || weightReciprocal > minWeightReciprocal) {
//ignore
continue;
}
if(weightReciprocal < minWeightReciprocal) { //reset potential candidates to this
minWeightReciprocal = weightReciprocal;
w[0] = ww; //structure copy
size = 1;
}
else { //add this to potential candidates
w[size++] = ww; //structure copy
}
}
}
}
}
//at this stage we know the minWeightReciprocal and the list of pencilmarks for that weight
if(size > 1) { //further reduce
//reduce to those candidates with more visible pencilmarks
int maxAffected = 0;
for(int i = 0; i < size; i++) {
int nAffected = 0;
int c = w[i].cell;
const cellIndex * const affected = affectedCells[c];
for(int a = 0; a < 20; a++) {
if(g.cellPossibilities[affected[a]]) { //unsolved
nAffected += BitCount[g.cellPossibilities[affected[a]]];
}
}
if(nAffected > maxAffected) {
maxAffected = nAffected;
for(int j = 0; j < i; j++) { //mark all above as invalid
w[j].cell = 99;
}
}
else if(nAffected < maxAffected) {
w[i].cell = 99;
}
}
int j = 0;
for(int i = 0; i < size; i++) {
if(w[i].cell != 99) {
if(i != j) {
w[j] = w[i]; //structure copy
}
j++;
}
}
size = j;
}
}
};
//Perform any solving algorithms.
//Finally make a guess and call recursively until success (solved) or failure (no solution).
static void attempt(game &g)
{
game gg;
cellIndex chosenCell = 0;
int ci, nGuesses, chosenValue, cp, bc;
restart:
checkForLastOccurenceInGroup(g); //the first algorithm performs internal repeating
if(g.mode & MODE_STOP_PROCESSING) return;
#ifdef USE_LOCKED_CANDIDATES
//if(g.cellsLeft < 25)
if(g.maxSolutions != 1)
{
FindLockedCandidates(g); //bb_sudoku by Brian Turner
if(g.mode & MODE_STOP_PROCESSING) return;
}
#endif // USE_LOCKED_CANDIDATES
//Prepare a guess
//Findout an unsolved cell with less possibilities
nGuesses = 10;
//Randomizing the guess sequence causes 9.3% false improvement.
bool fastGuess = g.knownSolution && (g.nSolutions == 0);
ci = g.lastGuess;
do {
ci++;
if(ci > 80)
ci = 0;
cp = g.cellPossibilities[ci];
if(0 == cp) continue; //skip solved cells
if((bc = BitCount[cp]) < nGuesses) {
chosenCell = ci;
if(2 == (nGuesses = bc) || fastGuess) //guessing a cell with 2 possibilities is OK
break;
}
} while(ci != g.lastGuess);
g.lastGuess = chosenCell;
int restValues = g.cellPossibilities[chosenCell];
if(fastGuess) {
//start from the right guess
chosenValue = g.knownSolution[chosenCell];
if(0 == (chosenValue & restValues)) { //wrong way
//chosenValue = restValues & -restValues;
g.mode |= MODE_STOP_PROCESSING;
return;
}
}
else {
//start from the rightmost bit
chosenValue = restValues & -restValues;
}
for(; --nGuesses; chosenValue = restValues & -restValues) {
restValues ^= chosenValue; //rest of the possibilities
gg = g; //copy the solution context
//gg.guessDepth++;
//if(gg.maxGuessDepth && gg.guessDepth > *gg.maxGuessDepth)
// *gg.maxGuessDepth = gg.guessDepth;
setDigit(gg, chosenCell, (bitmap)chosenValue);
if(0 == (gg.mode & MODE_STOP_PROCESSING)) {
attempt(gg);
}
if(gg.mode & MODE_STOP_GUESSING) {
g.mode = gg.mode;
g.nSolutions = gg.nSolutions;
return;
}
g.nSolutions = gg.nSolutions;
if(nGuesses == 1) { //stop working with copies of the context
setDigit(g, chosenCell, restValues);
if(g.mode & MODE_STOP_PROCESSING) return;
goto restart;
}
//Note that the next guess will destroy the content of g->cellDigits.
}
}
////Perform any solving algorithms.
////Finally make a guess and call recursively until success (solved) or failure (no solution).
//static void attempt1(game &g)
//{
// game gg;
//restart:
// checkForLastOccurenceInGroup(g); //the first algorithm performs internal repeating
// if(g.mode & MODE_STOP_PROCESSING) return;
//#ifdef USE_LOCKED_CANDIDATES
// if(g.maxSolutions != 1)
// {
// FindLockedCandidates(g); //bb_sudoku by Brian Turner
// if(g.mode & MODE_STOP_PROCESSING) return;
// }
//#endif // USE_LOCKED_CANDIDATES
//
// //Prepare a guess
// //Findout unsolved cells with less possibilities
// pmWeights w;
// w.init(g);
// pmWeight &ww = w.w[0];
// //guess the most probablistic pencilmark
// gg = g; //clone the context
// setDigit(gg, ww.cell, 1 << ww.digit);
// if(0 == (gg.mode & MODE_STOP_PROCESSING)) {
// attempt1(gg);
// }
// if(gg.mode & MODE_STOP_GUESSING) {
// g.mode = gg.mode;
// g.nSolutions = gg.nSolutions;
// return;
// }
// g.nSolutions = gg.nSolutions;
// //now try the alternative by removal of the guess from the possibilities
// //do it on the original
// g.cellPossibilities[ww.cell] ^= (1 << ww.digit); //clear the guessed pencilmark
// unsigned int rest = Bitmap2Digit[g.cellPossibilities[ww.cell]];
// if(rest) { //it has been a bivalue cell so now it is solved
// setDigit(g, ww.cell, g.cellPossibilities[ww.cell]);
// if(g.mode & MODE_STOP_PROCESSING) return;
// }
// goto restart;
//}
//Set the initially known digits.
//The context is updated on 2 passes.
static inline void init(game &g, const char* in)
{
bitmap *gkd = g.groupKnownDigits;
bitmap *cp = g.cellPossibilities;
//First pass: set the digits w/o updating the affected cells.
for(int i = 0; i < 81; i++) {
if(in[i] == 0) continue;
//...additional input checking here...
//setDigit(g, i, Digit2Bitmap[in[i]]);
//...check for error here if this is the goal...
bitmap bm = Digit2Bitmap[(unsigned int)in[i]];
const int *ag = affectedGroups[i];
//if the digit we are setting has been previously set for one of
//the 3 groups the cell is member of, we are in wrong way
if(bm & (gkd[ag[0]] | gkd[ag[1]] | gkd[ag[2]])) {
g.mode |= MODE_STOP_PROCESSING;
return;
}
cp[i] = 0; //clear the mask to exclude this cell from further processing
if(g.cellDigits) {
g.cellDigits[i] = (cellDigit)bm; //set the digit. Bit 8 is lost, translating 9 to 0.
}
if(0 == --g.cellsLeft) {//lucky, 81 givens
//g.mode |= MODE_STOP_PROCESSING;
solutionFound(g);
//g.nSolutions = 1;
return;
}
//set the digit as solved for all 3 groups
gkd[ag[0]] |= bm; gkd[ag[1]] |= bm; gkd[ag[2]] |= bm;
}
//Second pass: update the affected cells.
//Overall performance improvement is about 9% compared to setDigit calls.