-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.cpp
657 lines (576 loc) · 21.4 KB
/
main.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
//
// Monza (MI), Italy
// 6 september 2005
// Copyright 2005, Isidoro Ghezzi
// isidoro.ghezzi@tiscali.it
//
// Lissone (MB), Italy
// Sat Jun 14 16:12:24 CEST 2014
// Copyright 2005-2021, Isidoro Ghezzi
// isidoro.ghezzi@icloud.com
//
// Porting to:
// mavericks, Mac OS X 10.9
// yosemite, OS X 10.10.3
// OS X El Capitan 10.11.6
// macOS High Sierra 10.13.4
// macOS Catalina 10.15.7
//
#include "common.h"
#include <math.h>
#include <algorithm>
#include <functional>
#include <stdexcept>
#include <ctime>
#include <fstream>
#include <sstream>
#include <vector>
#include <cstring>
namespace isi{
typedef unsigned short CellValue;
enum class CellStatus{
ConstantInputInitValue,
Variable
};
typedef std::pair <CellStatus, CellValue> CellStatusWithValue;
#define CAST_CELL_STATUS_TO_CELLVALUE(cellStatus) static_cast<CellValue>(cellStatus)
#if _DEBUG
namespace cellstatus{
const char * Label (const CellStatus theCellStatus){
switch (theCellStatus){
case CellStatus::ConstantInputInitValue:
return "ConstantInputInitValue";
case CellStatus::Variable:
return "Variable";
}
LogicAssert (false);
return NULL;
}
}
#endif
struct SCell{
private:
CellValue fValue;
CellStatus fStatus;
#ifdef _DEBUG
private:
bool IsGoodData (void) const;
#endif
public:
void SetInitValue (const CellValue theValue);
public:
CellValue GetValue (void) const;
void SetValue (const CellValue theValue);
public:
CellStatus GetStatus (void) const;
public:
SCell (void);
virtual ~SCell (void);
};
SCell::SCell (void):
fValue (0),
fStatus (CellStatus::Variable){
}
SCell::~SCell (void){
}
#ifdef _DEBUG
bool SCell::IsGoodData (void) const{
const bool ret = ((CellStatus::ConstantInputInitValue == fStatus) && ((fValue > 0) && (fValue <=9)))
||
((CellStatus::Variable == fStatus) && (/* (fValue >= 0) &&*/ (fValue <=9)));
if (false == ret){
ISI_DUMP (cellstatus::Label (fStatus));
ISI_DUMP (fValue);
}
return ret;
}
#endif
CellValue SCell::GetValue (void) const{
LogicAssert (true == this->IsGoodData ());
return fValue;
}
void SCell::SetValue (const CellValue theValue){
LogicAssert (CellStatus::Variable == fStatus);
fValue = theValue;
LogicAssert (true == this->IsGoodData ());
}
CellStatus SCell::GetStatus (void) const{
LogicAssert (true == this->IsGoodData ());
return fStatus;
}
void SCell::SetInitValue (const CellValue theValue){
LogicAssert ((theValue > 0) && (theValue <=9));
LogicAssert (fValue != theValue);
fValue = theValue;
fStatus = CellStatus::ConstantInputInitValue;
}
typedef SCell Cell;
const std::size_t kDim = 9;
}
namespace isi{
typedef std::vector <Cell *> CellVector; // 376616
typedef std::vector <CellVector> MatrixOfCellPtr; // 352973
static void DumpMatrix (const char * theIdentifier, const MatrixOfCellPtr &theMatrix){
LogicAssert (true == IsGoodPtr (theIdentifier));
std::cout << theIdentifier << ":" << std::endl;
for (std::size_t j = 0; j < kDim; ++j){
for (std::size_t i = 0; i < kDim; ++i){
std::cout << "\t" /*<< "[" << theMatrix [j][i]->fStatus << "] "*/<< theMatrix [j][i]->GetValue () << ", ";
// std::cout << "\t" /*<< "[" << theMatrix [j][i]->fStatus << "] "*/<< theMatrix [j][i] << ", ";
}
std::cout << std::endl;
}
}
struct SCountHelper: public std::binary_function <bool, SCell *, CellValue>{
private:
const CellValue fCellValue;
public:
bool operator () (const Cell * theIter) const{
LogicAssert (true == IsGoodPtr (theIter));
return theIter->GetValue () == fCellValue;
}
explicit SCountHelper (const CellValue theCellValue):
fCellValue (theCellValue){
LogicAssert (/*(theCellValue >= 0) &&*/ (theCellValue <= 9));
}
private:
SCountHelper &operator = (const SCountHelper &);
};
struct SFindHelper: SCountHelper{
explicit SFindHelper (const CellValue theCellValue):
SCountHelper (theCellValue){
LogicAssert (theCellValue > 0);
}
};
static bool IsAvailable (const CellVector &theCellVector, const CellValue theCellValue){
LogicAssert ((theCellValue >= 1) && (theCellValue <= 9));
const CellVector::const_iterator aIterResult = std::find_if (theCellVector.begin (), theCellVector.end () , SFindHelper (theCellValue));
return theCellVector.end () == aIterResult;
}
#ifdef _DEBUG
static bool IsGoodSchemaSequenceOriginal (const CellVector &theCellVector){
size_t aNumTotal = std::count_if (theCellVector.begin (), theCellVector.end (), SCountHelper (0));
bool rit = true;
for (CellValue aCellValue = 0; aCellValue < kDim && rit; ++aCellValue){
const size_t aNumValue = std::count_if (theCellVector.begin (), theCellVector.end (), SCountHelper (aCellValue + 1));
/*
const size_t aNumValue = std::count_if (theCellVector.begin (), theCellVector.end () , [aCellValue] (const Cell * theIter) -> bool{
LogicAssert (true == IsGoodPtr (theIter));
return theIter->GetValue () == aCellValue + 1;
});
*/
aNumTotal += aNumValue;
rit = ((1 == aNumValue) || (0 == aNumValue));
}
return (kDim == aNumTotal) && rit;
}
#endif
static bool IsGoodSchemaSequenceAdvanced (const CellVector &theCellVector){
size_t aNumTotal = 0;
bool rit = true;
for (CellValue aCellValue = 0; aCellValue < (kDim + 1) && rit; ++aCellValue){
const size_t aNumValue = std::count_if (theCellVector.begin (), theCellVector.end (), SCountHelper (aCellValue));
/*
const size_t aNumValue = std::count_if (theCellVector.begin (), theCellVector.end () , [aCellValue] (const Cell * theIter) -> bool{
LogicAssert (true == IsGoodPtr (theIter));
return theIter->GetValue () == aCellValue + 0;
});
*/
aNumTotal += aNumValue;
rit = ((1 == aNumValue) || (0 == aNumValue) || (0 == aCellValue));
}
return (kDim == aNumTotal) && rit;
}
static bool IsGoodSchemaSequence (const CellVector &theCellVector){
#ifdef _DEBUG
const bool aOriginal = IsGoodSchemaSequenceOriginal (theCellVector);
const bool aAdvanced = IsGoodSchemaSequenceAdvanced (theCellVector);
LogicAssert (aOriginal == aAdvanced);
return aAdvanced;
#else
return IsGoodSchemaSequenceAdvanced (theCellVector);
#endif
}
static bool IsGoodSolutionSequence (const CellVector &theCellVector){
LogicAssert (true == IsGoodSchemaSequence (theCellVector));
bool rit = true;
for (CellValue aCellValue = 0; aCellValue < kDim && rit; ++aCellValue){
const size_t aNumValue = std::count_if (theCellVector.begin (), theCellVector.end () , SCountHelper (aCellValue + 1));
/*
const size_t aNumValue = std::count_if (theCellVector.begin (), theCellVector.end () , [aCellValue] (const Cell * theIter){
LogicAssert (true == IsGoodPtr (theIter));
return theIter->GetValue () == aCellValue + 1;
});
*/
rit = (1 == aNumValue);
}
return rit;
}
template <class TAlgo>
bool IsGood (const MatrixOfCellPtr * theSchema, const TAlgo theAlgo){
LogicAssert(true == IsGoodPtr(theSchema));
bool rit = true;
for (std::size_t i = 0; (i < kDim) && rit; ++i){
rit = theAlgo (theSchema [0][i]) &&
theAlgo (theSchema [1][i]) &&
theAlgo (theSchema [2][i]);
}
return rit;
}
static bool IsGoodSolution (const MatrixOfCellPtr * theSchema){
LogicAssert(true == IsGoodPtr(theSchema));
return IsGood (theSchema, IsGoodSolutionSequence);
}
static bool IsGoodSchema (const MatrixOfCellPtr * theSchema){
LogicAssert(true == IsGoodPtr(theSchema));
return IsGood (theSchema, IsGoodSchemaSequence);
}
const std::size_t kIndexHelper [81][3]={
// it would be interesting automagically create this matrix at compile time, using meta programming
{1, 1, 1}, {1, 2, 1}, {1, 3, 1}, {1, 4, 2}, {1, 5, 2}, {1, 6, 2}, {1, 7, 3}, {1, 8, 3}, {1, 9, 3},
{2, 1, 1}, {2, 2, 1}, {2, 3, 1}, {2, 4, 2}, {2, 5, 2}, {2, 6, 2}, {2, 7, 3}, {2, 8, 3}, {2, 9, 3},
{3, 1, 1}, {3, 2, 1}, {3, 3, 1}, {3, 4, 2}, {3, 5, 2}, {3, 6, 2}, {3, 7, 3}, {3, 8, 3}, {3, 9, 3},
{4, 1, 4}, {4, 2, 4}, {4, 3, 4}, {4, 4, 5}, {4, 5, 5}, {4, 6, 5}, {4, 7, 6}, {4, 8, 6}, {4, 9, 6},
{5, 1, 4}, {5, 2, 4}, {5, 3, 4}, {5, 4, 5}, {5, 5, 5}, {5, 6, 5}, {5, 7, 6}, {5, 8, 6}, {5, 9, 6},
{6, 1, 4}, {6, 2, 4}, {6, 3, 4}, {6, 4, 5}, {6, 5, 5}, {6, 6, 5}, {6, 7, 6}, {6, 8, 6}, {6, 9, 6},
{7, 1, 7}, {7, 2, 7}, {7, 3, 7}, {7, 4, 8}, {7, 5, 8}, {7, 6, 8}, {7, 7, 9}, {7, 8, 9}, {7, 9, 9},
{8, 1, 7}, {8, 2, 7}, {8, 3, 7}, {8, 4, 8}, {8, 5, 8}, {8, 6, 8}, {8, 7, 9}, {8, 8, 9}, {8, 9, 9},
{9, 1, 7}, {9, 2, 7}, {9, 3, 7}, {9, 4, 8}, {9, 5, 8}, {9, 6, 8}, {9, 7, 9}, {9, 8, 9}, {9, 9, 9}
};
static unsigned long gNumOfCall = 0;
static unsigned long gNumOfSolution = 0;
static unsigned short NextIndex (const unsigned short theIndex){
// isghe: here i am simply choosing the next one,
// but in real life you can choose a smarter one:
// the exaustive result will not change but yuo'll reach the full solution in less (maybe also in time) recursive call.
LogicAssert (/*(theIndex >= 0) &&*/ (theIndex < kDim * kDim));
// isghe: to_do (but you'll need to change the caller):
// LogicAssert (/*(theIndex >= 0) &&*/ (theIndex + 1< kDim * kDim));
return theIndex + 1;
}
static int CalcSortValue (const CellStatusWithValue &thePair){
return CAST_CELL_STATUS_TO_CELLVALUE (thePair.first) * 1000 + thePair.second;
}
static bool MyCompare (const std::pair <unsigned short, CellStatusWithValue> &thePair1, const std::pair <unsigned short, CellStatusWithValue> &thePair2){
return CalcSortValue (thePair2.second) > CalcSortValue (thePair1.second);
}
#ifdef _DEBUG
static bool MyDump (const std::pair <unsigned short, CellStatusWithValue> &thePair){
ISI_DUMP (thePair.first);
ISI_DUMP (cellstatus::Label (thePair.second.first));
ISI_DUMP (thePair.second.second);
return true;
}
static bool MyCountFree (const Cell * theCell){
return theCell->GetStatus () == CellStatus::Variable;
}
static std::size_t CountFree (const CellVector &theCellVector){
return std::count_if (theCellVector.begin (), theCellVector.end (), MyCountFree);
}
#endif
static std::size_t CountFreeFree (CellVector ** aMatrix){
LogicAssert(true == IsGoodPtr(aMatrix));
bool aValueVect [kDim] = {false};
for (std::size_t i = 0; i < kDim; ++i){
for (int j = 0; j < 3; ++j){
LogicAssert(true == IsGoodPtr(aMatrix[j]));
LogicAssert(true == IsGoodPtr((*aMatrix[j])[i]));
const unsigned short aVal = (*aMatrix[j])[i]->GetValue ();
if (aVal > 0){
LogicAssert(IsInCRange <std::size_t>(0, aVal - 1, IG_DIM_OF_ARRAY(aValueVect)));
if (false == aValueVect [aVal - 1]){
aValueVect [aVal - 1] = true;
}
}
}
}
return std::count (aValueVect, aValueVect + kDim, false);
}
static unsigned short TransformIndex (const unsigned short theIndex, const Cell * theCell, MatrixOfCellPtr * theSchema){
LogicAssert (true == IsGoodPtr (theCell));
LogicAssert (true == IsGoodPtr (theSchema));
static std::vector <std::pair <unsigned short, CellStatusWithValue> > sTransformArray;
static bool sFirstTime = true;
if (sFirstTime){
sFirstTime = false;
for (unsigned short i = 0; i < kDim * kDim; ++i){
const std::size_t aRowIndex = kIndexHelper [i][0] - 1;
const std::size_t aColIndex = kIndexHelper [i][1] - 1;
const std::size_t aSqrIndex = kIndexHelper [i][2] - 1;
#ifdef _DEBUG
const std::size_t aCountFreeRow = CountFree (theSchema [0][aRowIndex]);
const std::size_t aCountFreeCol = CountFree (theSchema [1][aColIndex]);
const std::size_t aCountFreeSqr = CountFree (theSchema [2][aSqrIndex]);
ISI_DUMP(aCountFreeRow);
ISI_DUMP(aCountFreeCol);
ISI_DUMP(aCountFreeSqr);
#endif
CellVector * aMatrix []={
&theSchema [0][aRowIndex], &theSchema [1][aColIndex], &theSchema [2][aSqrIndex]
};
const std::size_t aCountFreeFree = CountFreeFree (aMatrix);
#if 1
sTransformArray.emplace_back (i, CellStatusWithValue (theCell [i].GetStatus (), aCountFreeFree));
#else
const std::pair <unsigned short, CellStatusWithValue> aPair (i, CellStatusWithValue (theCell [i].GetStatus (), aCountFreeFree));
sTransformArray.push_back (aPair);
#endif
}
std::sort (sTransformArray.begin (), sTransformArray.end (), MyCompare);
#ifdef _DEBUG
std::for_each (sTransformArray.begin (), sTransformArray.end (), MyDump);
#endif
}
// return kDim*kDim - theIndex - 1;
return sTransformArray [theIndex].first;
}
static bool IsGoodSchemaAvailable (const MatrixOfCellPtr * theSchema, const CellValue theValue, const unsigned short theTransformedIndex){
LogicAssert (true == IsGoodPtr (theSchema));
LogicAssert (true == IsGoodSchema (theSchema)); // this is the slow assert
#ifdef _DEBUG
bool aGood = true;
for (int i = 0; i < 3 && aGood; ++i){
const std::size_t aIndex = kIndexHelper [theTransformedIndex][i] - 1;
aGood = isi::IsAvailable (theSchema [i][aIndex], theValue + 1);
}
return aGood;
#else
return isi::IsAvailable (theSchema [0][kIndexHelper [theTransformedIndex][0] - 1], theValue + 1)
&& isi::IsAvailable (theSchema [1][kIndexHelper [theTransformedIndex][1] - 1], theValue + 1)
&& isi::IsAvailable (theSchema [2][kIndexHelper [theTransformedIndex][2] - 1], theValue + 1);
#endif
}
static bool HandleNextRecursive (const unsigned short theIndex, Cell * theCell, MatrixOfCellPtr * theSchema){
LogicAssert (true == IsGoodPtr (theCell));
LogicAssert (true == IsGoodPtr (theSchema));
LogicAssert (true == IsGoodSchema (theSchema));
const unsigned short aTransformedIndex = TransformIndex (theIndex, theCell, theSchema);
gNumOfCall++;
if (theIndex >= kDim * kDim){
++gNumOfSolution;
if (!(gNumOfSolution % 100000) || true){
const unsigned long aCurrentSolution = gNumOfSolution;
ISI_DUMP (aCurrentSolution);
ISI_DUMP (gNumOfCall);
ISI_DUMP (static_cast <double >(gNumOfCall)/aCurrentSolution);
DumpMatrix ("theRowVector", theSchema [0]);
ISI_DUMP (BoolToStr (IsGoodSolution (theSchema)));
}
return false;
}
LogicAssert (/*(theIndex >= 0) &&*/ (theIndex < kDim * kDim));
if (theCell [aTransformedIndex].GetStatus () == CellStatus::ConstantInputInitValue){
return HandleNextRecursive (NextIndex (theIndex), theCell, theSchema);
}
bool rit = false;
for (CellValue aValue = 0; aValue < kDim; ++aValue){
LogicAssert (theCell [aTransformedIndex].GetValue () != aValue+1);
LogicAssert (theCell [aTransformedIndex].GetValue () == 0);
const bool aGood = IsGoodSchemaAvailable (theSchema, aValue, aTransformedIndex);
if (aGood){
theCell [aTransformedIndex].SetValue (aValue + 1);
rit = HandleNextRecursive (NextIndex (theIndex), theCell, theSchema);
if (rit){
break;
}
else{
theCell [aTransformedIndex].SetValue (0);
}
}
}
return rit;
}
}
namespace isi{
static bool IsInitValue (const Cell &theCell){
return theCell.GetStatus () == CellStatus::ConstantInputInitValue;
}
static void LoadCellValuesFromFile (const char * theFileName, CellValue * theCellValue){
LogicAssert (true == IsGoodPtr (theFileName));
LogicAssert (std::strlen (theFileName) > 0);
LogicAssert (true == IsGoodPtr (theCellValue));
std::string aFileString;
{
std::ifstream aInputFileStrem (theFileName);
RuntimeAssert(!!aInputFileStrem);
std::string aLineString;
while (std::getline (aInputFileStrem, aLineString)){
aFileString += " " + aLineString;
}
}
#if _DEBUG
ISI_DUMP (aFileString);
#endif
std::istringstream incoming_stream(aFileString);
for (std::size_t i = 0; i < kDim*kDim; ++i){
CellValue aCellValue;
std::string aSeparetor;
incoming_stream >> aCellValue;
incoming_stream >> aSeparetor;
theCellValue [i] = aCellValue;
#if _DEBUG
ISI_DUMP (aCellValue);
#endif
}
}
static void my_main (const char * theFileName){
LogicAssert (true == IsGoodPtr (theFileName));
LogicAssert (std::strlen (theFileName) > 0);
ISI_DUMP (theFileName);
#if 1
CellValue kCellValue [kDim * kDim]={
0
};
LoadCellValuesFromFile (theFileName, kCellValue);
#else
const CellValue kCellValue [kDim * kDim]={
0, 0, 6, 0, 5, 0, 8, 0, 2,
0, 0, 8, 0, 0, 4, 5, 0, 9,
0, 5, 7, 0, 8, 2, 3, 0, 0,
1, 0, 0, 0, 6, 0, 0, 5, 0,
0, 8, 5, 0, 0, 3, 0, 2, 6,
6, 4, 0, 0, 0, 0, 0, 0, 7,
0, 0, 9, 1, 0, 0, 6, 0, 0,
5, 0, 1, 8, 0, 0, 2, 0, 0,
0, 0, 4, 0, 3, 0, 7, 0, 0
};
#endif
Cell aCell [kDim * kDim];
for (std::size_t aIter = 0; aIter < kDim * kDim; ++aIter){
if (0 != kCellValue [aIter]){
LogicAssert(IsInCRange <std::size_t>(0, aIter, IG_DIM_OF_ARRAY(kCellValue)));
LogicAssert(IsInCRange <std::size_t>(0, aIter, IG_DIM_OF_ARRAY(aCell)));
aCell [aIter].SetInitValue (kCellValue [aIter]);
}
}
ISI_DUMP (std::count_if (aCell, aCell + kDim * kDim, IsInitValue));
// alias of rows
MatrixOfCellPtr aRow (kDim);
std::size_t i = 0;
std::size_t j = 0;
for (j = 0; j < kDim; ++j){
for (i = 0; i < kDim; ++i){
LogicAssert(IsInCRange <std::size_t>(0, i + j*kDim, IG_DIM_OF_ARRAY(aCell)));
aRow [j].push_back(&aCell [i + j*kDim]);
}
}
isi::DumpMatrix ("aRow", aRow);
// alias of columns
MatrixOfCellPtr aCol (kDim);
for (j = 0; j < kDim; ++j){
for (i = 0; i < kDim; ++i){
LogicAssert(IsInCRange <std::size_t>(0, i + j*kDim, IG_DIM_OF_ARRAY(aCell)));
aCol [i].push_back(&aCell [i + j*kDim]);
}
}
// alias of square
MatrixOfCellPtr aSqr (kDim);
const std::size_t aTransform [kDim][3] = {
// it would be interesting automagically create this matrix at compile time, using meta programming
{0, 0, 0}, {1, 0, 3}, {2, 0, 6},
{3, 3, 0}, {4, 3, 3}, {5, 3, 6},
{6, 6, 0}, {7, 6, 3}, {8, 6, 6}
};
for (j = 0; j < kDim; ++j){
std::size_t aIndex = 0;
for (std::size_t a = aTransform [j][1]; a < aTransform [j][1] + 3; ++a){
for (std::size_t b = aTransform [j][2]; b < aTransform [j][2] + 3; ++b){
LogicAssert(IsInCRange <std::size_t>(0, a, kDim));
LogicAssert(IsInCRange <std::size_t>(0, b, kDim));
LogicAssert(IsInCRange <std::size_t>(0, j, kDim));
LogicAssert(IsInCRange <std::size_t>(0, aIndex, kDim));
aSqr [j].push_back (aRow [a] [b]);
++aIndex;
}
}
}
#ifdef _DEBUG
isi::DumpMatrix ("aCol", aCol);
isi::DumpMatrix ("aSqr", aSqr);
#endif
MatrixOfCellPtr aSchema []={
aRow, aCol, aSqr
};
RuntimeAssert (true == IsGoodSchema (aSchema));
(void) isi::HandleNextRecursive (0, aCell, aSchema);
isi::ISI_DUMP (isi::gNumOfCall);
isi::ISI_DUMP (isi::gNumOfSolution);
}
}
int main (int argc, char* argv []){
/*
compile:
$ uname -a
Darwin Isidoros-MacBook-Pro.local 14.3.0 Darwin Kernel Version 14.3.0: Mon Mar 23 11:59:05 PDT 2015; root:xnu-2782.20.48~5/RELEASE_X86_64 x86_64
$ g++ --version
Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.10.sdk/usr/include/c++/4.2.1
Apple LLVM version 6.1.0 (clang-602.0.53) (based on LLVM 3.6.0svn)
Target: x86_64-apple-darwin14.3.0
Thread model: posix
$ make
g++ -Wall -ansi -pedantic -Wextra -std=c++17 -O3 main.cpp -o sudoku.out
g++ -Wall -ansi -pedantic -Wextra -std=c++17 -g -D_DEBUG main.cpp -o sudoku_d.out
run:
$ ./sudoku.out example/diabolikus_08062008.sdk
main: isi::GetGnuVersion (): 4;
__FILE__: main.cpp;
__DATE__: May 21 2015;
__TIME__: 18:50:56;
main: isi::BoolToStr (isi::IsDebugVersion ()): false;
my_main: theFileName: example/diabolikus_08062008.sdk;
my_main: std::count_if (aCell, aCell + kDim * kDim, IsInitValue): 26;
aRow:
7, 0, 1, 0, 5, 0, 0, 0, 0,
8, 4, 0, 1, 9, 0, 0, 7, 0,
0, 0, 0, 0, 0, 2, 0, 0, 0,
6, 8, 0, 0, 0, 0, 0, 1, 0,
0, 0, 7, 0, 0, 0, 9, 0, 0,
0, 1, 0, 0, 0, 0, 0, 4, 3,
0, 0, 0, 2, 0, 0, 0, 0, 0,
0, 7, 0, 0, 8, 4, 0, 9, 5,
0, 0, 0, 0, 7, 0, 2, 0, 6,
HandleNextRecursive: aCurrentSolution: 1;
HandleNextRecursive: gNumOfCall: 17604;
HandleNextRecursive: static_cast <double >(gNumOfCall)/aCurrentSolution: 17604;
theRowVector:
7, 2, 1, 3, 5, 8, 4, 6, 9,
8, 4, 5, 1, 9, 6, 3, 7, 2,
3, 9, 6, 7, 4, 2, 8, 5, 1,
6, 8, 2, 4, 3, 9, 5, 1, 7,
4, 3, 7, 5, 6, 1, 9, 2, 8,
5, 1, 9, 8, 2, 7, 6, 4, 3,
9, 6, 8, 2, 1, 5, 7, 3, 4,
2, 7, 3, 6, 8, 4, 1, 9, 5,
1, 5, 4, 9, 7, 3, 2, 8, 6,
HandleNextRecursive: BoolToStr (IsGoodSolution (theSchema)): true;
my_main: isi::gNumOfCall: 21669;
my_main: isi::gNumOfSolution: 1;
main: rit: 0;
main: std::clock () - aStart: 7473;
*/
const std::clock_t aStart = std::clock ();
volatile int rit = 1;
try{
isi::ISI_DUMP (isi::GetGnuVersion ());
isi::DumpMacro ("__FILE__" , __FILE__);
isi::DumpMacro ("__DATE__" , __DATE__);
isi::DumpMacro ("__TIME__" , __TIME__);
isi::ISI_DUMP (isi::BoolToStr (isi::IsDebugVersion ()));
isi::RuntimeAssert (2 == argc);
isi::RuntimeAssert (true == isi::IsGoodPtr (argv [1]));
isi::RuntimeAssert (std::strlen (argv [1]) > 0);
isi::my_main (argv [1]);
rit = 0;
}
catch (const std::exception &theException){
rit = 2;
isi::HandleStdException (theException);
}
catch (...){
rit = 3;
isi::HandleUnknownException ();
}
isi::ISI_DUMP (rit);
isi::ISI_DUMP (static_cast <double> ((std::clock () - aStart))/CLOCKS_PER_SEC);
return rit;
}