-
Notifications
You must be signed in to change notification settings - Fork 1
/
sudoku.cpp
682 lines (574 loc) · 12.8 KB
/
sudoku.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
#include <iostream>
#include <string>
using namespace std;
#define NUM9 9
class Sudoku
{
public:
Sudoku(int sudokuArray[81])
{
for (int i = 0; i < 81; i++) {
int col = i % 9;
int row = i / 9;
mSudokuMat[row][col] = sudokuArray[i];
}
initMatSets();
};
~Sudoku()
{
};
void initMatSets()
{
memset(mMatSets, 0, sizeof(mMatSets));
for (int i = 0; i < 9; i++)
{
for (int j = 0; j < 9; j++)
{
if (mSudokuMat[i][j] == 0)
{
mMatSets[i][j] = (1 << 10) - 2;
}
else
{
mMatSets[i][j] = 0;
}
}
}
//memset(changeElement, false, sizeof(changeElement));
};
int& getCandidateSet(int row, int col)
{
return mMatSets[row][col];
};
//根据输入 求解这个数独
/*
求解算法:
一遍又一遍的遍历整个9x9格子,
1. 根据横纵行,以及3x3格子块的情况,确定当前格子可选数字 如1~9等
如果当前格子可以确定,那就用当前格子的数字,去剔除掉能影响到的格子
如果不可以确定,那么就把候选的数字集合保留起来
2. 遍历一遍后,再接着遍历,返回1
*/
//初始化 让所有为0的格子的候选集合都为1~9
//std::cout << "what the fuck" << getCandidateSet(3, 1)[0] << std::endl;
void swapMat9x9(bool isBackup, int backup[9][9], int origin[9][9])
{
for (int i = 0; i < 81; i++)
{
int row = i / 9, col = i % 9;
if (isBackup) {
backup[row][col] = origin[row][col];
}
else {
origin[row][col] = backup[row][col];
}
}
}
void backupSudokuMat(int dep)
{
if (changeElement[dep])return;
swapMat9x9(true, mBackupSudokuSets[dep], mSudokuMat );
}
void restoreSudokuMat(int dep)
{
swapMat9x9(false, mBackupSudokuSets[dep], mSudokuMat);
}
void backupAllCandidate(int dep)
{
if (changeElement[dep])return;
swapMat9x9(true, mBackupMatSets[dep], mMatSets);
}
void restoreAllCandidate(int dep)
{
swapMat9x9(false, mBackupMatSets[dep], mMatSets);
}
bool changeElement[82] = {};
bool solve(int dep)
{
for (int x = 0; x < 100; x++)
{
bool changed = false;
for (int i = 0; i < 9; i++)
{
for (int j = 0; j < 9; j++)
{
int& val = mSudokuMat[i][j];
//此时val的值是不确定的 那就根据横纵行 还有小方块,来更新当前块的候选数字
//如果更新成功 那么就把val改好
if (val == 0)
{
int& candidateSet = getCandidateSet(i, j);
if (candidateSet == 0)
{
printf("candidateSet==0这里出错了 row:%d col:%d!!!!!\n", i, j);
printSelf();
return false;
}
int res = updateCandidateSet(i, j);
if (res != -1)
{
changed = true;
val = res;
candidateSet = 0;
printf("val值更新成功! %d row:%d col:%d\n", val, i, j);
printSelf();
}
else if (candidateSet == 0)
{
return false;
}
}
}
}
printf("扫描第%d次!!!\n", x);
printPosibilities();
printSelf();
if (isSolved())
{
return true;
}
if (!changed)
{
// printPosibilities();
//尝试优化
for (int i = 0; i < 9; i++)
{
int srow = i / 3 * 3;
int scol = i % 3 * 3;
int oneCnt = 0;
int filter[10] = {};
int filterAttach[10] = {};
for (int j = 0; j < 3; j++)
{
for (int k = 0; k < 3; k++)
{
int erow = srow + j;
int ecol = scol + k;
int& cset = getCandidateSet(erow, ecol);
for (int p = 1; p <= 9; p++)
{
if ((cset >> p) & 1)
{
filter[p]++;
filterAttach[p] = erow * 9 + ecol;
}
}
}
}
for (int q = 1; q <= 9; q++)
{
if (filter[q] == 1)
{
int erow = filterAttach[q] / 9;
int ecol = filterAttach[q] % 9;
mMatSets[erow][ecol] = 0;
mSudokuMat[erow][ecol] = q;
changed = true;
}
}
}
}
/*如果集合没有变化,那么就从canSet里面的元素,进行枚举,直到能推出整个*/
if (!changed)
{
int minIndex = -1;
int minCanCnt = 1000;
for (int i = 0; i < 9; i++)
{
for (int j = 0; j < 9; j++)
{
int& canSet = getCandidateSet(i, j);
if (canSet != 0)
{
int setCnt = getSetCnt(canSet);
if (setCnt < minCanCnt)
{
minCanCnt = setCnt;
minIndex = i * 9 + j;
}
}
}
}
if (minIndex != -1)
{
int& minCanset = getCandidateSet(minIndex / 9, minIndex % 9);
for (int k = 1; k <= 9; k++) {
if ((minCanset >> k) & 1)
{
backupAllCandidate(dep);
backupSudokuMat(dep);
changeElement[dep] = true;
mSudokuMat[minIndex/9][minIndex%9] = k;
minCanset = 0;
//minCanset &= ~(1 << k);
if (solve(dep+1))
{
printf("更换元素 搜索成功\n");
return true;
}
else
{
printf("dep:%d 更换元素 搜索失败!!!!!\n",dep);
printSelf();
restoreAllCandidate(dep);
restoreSudokuMat(dep);
changeElement[dep] = false;
//printPosibilities();
mSudokuMat[minIndex / 9][minIndex % 9] = 0;
minCanset &= ~(1 << k);
}
}
}
return false;
}
}
}
return false;
}
void printPosibilities()
{
int cnt = 0;
long long posibilities = 1;
int undeterminedElements = 0;
for (int i = 0; i < 9; i++)
{
int srow = i / 3 * 3;
int scol = i % 3 * 3;
printf("i:%d\n", i + 1);
for (int j = 0; j < 3; j++)
{
for (int k = 0; k < 3; k++)
{
int erow = srow + j;
int ecol = scol + k;
int& set = getCandidateSet(erow, ecol);
int cnt = getSetCnt(set);
if (cnt != 0)
{
undeterminedElements++;
posibilities *= cnt;
printf("(%d,%d,n=%d):", erow + 1, ecol + 1, cnt);
printSet(set);
printf("\t");
}
}
printf("\n");
}
}
printf("剩余可能的组合数:%lld 未确定元素数:%d\n", posibilities, undeterminedElements);
}
void printSet(int set)
{
int count = 0;
for (int i = 1; i <= 9; i++)
{
if ((set >> i) & 1)
{
if (count == 0)
{
printf("{%d", i);
count++;
}
else
{
printf(",%d", i);
}
}
}
printf("}");
}
int getSetCnt(int set)
{
int cnt = 0;
for (int i = 1; i <= 9; i++)
{
if ((set >> i) & 1)
{
cnt++;
}
}
// printf("Cnt:%d\n",cnt);
return cnt;
}
int updateCandidateSet(int row, int col)
{
//求出横竖行还有小方格的并集 然后用val的候选集减去这个并集 便能成功更新这个候选集
int& candidateSet = getCandidateSet(row, col);
for (int i = 0; i < 9; i++)
{
if (mSudokuMat[row][i] != 0)
{
candidateSet &= ~(1 << mSudokuMat[row][i]);
}
if (mSudokuMat[i][col] != 0)
{
candidateSet &= ~(1 << mSudokuMat[i][col]);
}
}
int smallSetStartRow = row / 3 * 3;
int smallSetStartCol = col / 3 * 3;
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
int& innerVal = mSudokuMat[smallSetStartRow + i][smallSetStartCol + j];
if (innerVal != 0)
{
candidateSet &= ~(1 << innerVal);
}
}
}
if (candidateSet != 0)
{
int cnt = 0;
int candidateNum = -1;
for (int i = 1; i <= 9; i++)
{
if ((candidateSet >> i) & 1)
{
cnt++;
candidateNum = i;
}
}
if (cnt == 1)
{
return candidateNum;
}
}
return -1;
}
/*
判断一个数独是否正确
*/
bool isSolved()
{
/*
判断每一行 是否OK
*/
for (int row = 0; row < 9; row++)
{
if (!isSetOk(mSudokuMat[row])) {
return false;
}
}
/*
判断每一列 是否OK
*/
for (int col = 0; col < 9; col++)
{
int colSet[9];
for (int i = 0; i < 9; i++)
{
colSet[i] = mSudokuMat[i][col];
}
if (!isSetOk(colSet)) {
return false;
}
}
/*
判断每一个小集合是否OK
*/
for (int iset = 0; iset< 9; iset++)
{
int innerSet[9], outRow, outCol;
outRow = iset / 3 * 3;
outCol = iset % 3 * 3;
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
innerSet[i * 3 + j] = mSudokuMat[outRow + i][outCol + j];
}
}
//printf("输出InnerSet i:%d\n", iset);
//print(3, innerSet);
if (!isSetOk(innerSet)) {
return false;
}
}
printf("数独已完成\n");
return true;
}
bool isSetOk(int(&set)[9])
{
int emptySet[10] ={};
for (int i = 0; i < 9; i++) {
if (set[i] == 0)return false;
emptySet[set[i]]++;
if (emptySet[set[i]] > 1) {
printSelf();
printf("数独仍待解决\n");
return false;
}
}
return true;
}
void print(int dimension, int* mat)
{
for (int i = 0; i < dimension; i++) {
for (int j = 0; j < dimension; j++) {
printf("%d ", mat[i*dimension + j]);
if ((j + 1) % 3 == 0)printf(" ");
}
if ((i + 1) % 3 == 0)printf("\n");
printf("\n");
}
}
void printSelf()
{
printf("输出mSudokuMat:\n");
print(9, &(mSudokuMat[0][0]));
}
private:
int mSudokuMat[9][9];
int mMatSets[9][9];
int mBackupSudokuSets[100][9][9] = {};
int mBackupMatSets[100][9][9] = {};
};
int InputSudoku[] = {
//0,0,0, 0,0,0, 0,0,0,
8,0,0, 0,0,0, 0,0,0,
0,0,3, 6,0,0, 0,0,0,
0,7,0, 0,9,0, 2,0,0,
0,5,0, 0,0,7, 0,0,0,
0,0,0, 0,4,5, 7,0,0,
0,0,0, 1,0,0, 0,3,0,
0,0,1, 0,0,0, 0,6,8,
0,0,8, 5,0,0, 0,1,0,
0,9,0, 0,0,0, 4,0,0,
/*0,5,0, 0,0,1, 0,0,0,
0,0,0, 0,0,0, 0,0,0,
0,0,4, 0,7,0, 0,0,9,
0,0,0, 0,0,2, 1,5,0,
7,0,9, 0,8,0, 0,0,0,
0,0,0, 0,0,0, 0,3,0,
0,0,7, 9,0,0, 0,0,0,
0,0,0, 0,0,0, 0,1,0,
0,0,8, 0,6,0, 0,0,0*/
/*0,0,0, 0,0,0, 0,0,0,
0,9,7, 0,8,0, 0,0,0,
0,0,0, 3,0,0, 0,0,1,
0,0,0, 0,0,0, 9,7,0,
3,0,0, 1,0,0, 0,2,0,
0,0,0, 0,6,0, 8,0,0,
6,0,0, 0,0,0, 0,0,0,
1,0,0, 0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