-
Notifications
You must be signed in to change notification settings - Fork 0
/
dlx.h
118 lines (94 loc) · 1.99 KB
/
dlx.h
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
#ifndef DLX_H
#define DLX_H
/*
Code modified from freely distributed Dancing Links code by Ruud/Havard on the sudoku forums
Adapted to handle bits format, and removed memory links
*/
#include <iostream>
#include "bits.h"
#include "backtrack_common.h"
class CHead;
class CCandidate;
class CCell;
class CNode {
protected:
CNode(bool head);
public:
~CNode();
CNode(CHead *head, CCandidate *candidate);
CNode *up;
CNode *down;
bool IsHead;
CNode *Peer1;
CNode *Peer2;
CNode *Peer3;
CHead *Head;
CCandidate *Candidate;
};
class CHead : public CNode {
public:
CHead();
CHead(CHead *root);
~CHead();
CHead *left;
CHead *right;
bool IsRoot;
void Cover();
void Uncover();
int size;
};
class CCandidate {
public:
CCandidate(CCell *cell, int digit);
~CCandidate();
CCell *Cell;
int Digit;
CNode *CelNode;
CNode *RowNode;
CNode *ColNode;
CNode *BoxNode;
void Disable();
void Enable();
bool Disabled;
};
class CCell {
public:
CCell(int index);
~CCell();
int Index;
int RowOffset;
int ColOffset;
int BoxOffset;
int Given; // Nonzero for a cell with a given digit
int Selected; // Currently selected digit in the DLX algorithm
int Solution; // Digit recorded in the (first) solution
CCandidate **Candidates;
void SetGiven(char digit);
void ClearGiven();
};
class CSolver {
private:
CHead *Root;
CHead *celHdr[81];
CHead *rowHdr[81];
CHead *colHdr[81];
CHead *boxHdr[81];
int solution_count;
int SolutionLimit;
bool Abort;
void Recurse();
public:
CSolver();
~CSolver();
int Solve(bool all);
CCell *Cells[81];
};
void csolver_load_bits(CSolver* solver, Bits* bits);
void csolver_unload_bits(CSolver* solver, Bits* bits);
bool csolver_unique_check(CSolver* solver, Bits* bits);
int csolver_soln_count(CSolver* solver, Bits* bits);
void csolver_load_bits(CSolver* solver, Grid& grid);
void csolver_unload_bits(CSolver* solver, Grid& grid);
bool csolver_unique_check(CSolver* solver, Grid& grid);
int csolver_soln_count(CSolver* solver, Grid& grid);
#endif