-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOrder8MOFPLS.cpp
335 lines (281 loc) · 10.6 KB
/
Order8MOFPLS.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
// To compile: g++ -static-libgcc -static-libstdc++ .\NewSPLSsolver.cpp -o 26SolutionSet.exe
// .\26SolutionSet.exe
#include <iostream>
#include <cmath>
#include <string>
#include <ctime>
using namespace std;
// Define the order of the SPLS$(a,b)$, where SIZE = $a \times b$
#define SIZE 8
#define a 2
#define b 4
// #define fp2 4
// #define fp3 4
const int SIZE_SQUARED = SIZE*SIZE;
const int ROW_NB = SIZE*SIZE*SIZE;
const int intCount = 4; // Number of Factors (Factor Pairs)
const int COL_NB = (intCount+1) * SIZE*SIZE; // +1 for one symbol in each cell
#define MAX_K 1000
#define block(r,c,i) (FParray[i][0]*((r)/FParray[i][0])+((c)/FParray[i][1]))
struct Node {
Node *left;
Node *right;
Node *up;
Node *down;
Node *head;
int size; //used for Column header
int rowID[3]; //used to identify row in order to map solutions to a sudoku grid
//ID Format: (Candidate, Row, Column)
};
struct Node Head;
struct Node* HeadNode = &Head;
struct Node* solution[MAX_K];
struct Node* orig_values[MAX_K];
bool matrix[ROW_NB][COL_NB] = { { 0 } };
bool isSolved = false;
void MapSolutionToGrid(int Sudoku[][SIZE]);
void PrintGrid(int Sudoku[][SIZE]);
clock_t timer, timer2;
int NumberOfSolutions = 0;
//===============================================================================================================//
//---------------------------------------------DLX Functions-----------------------------------------------------//
//===============================================================================================================//
void coverColumn(Node* col) {
col->left->right = col->right;
col->right->left = col->left;
for (Node* node = col->down; node != col; node = node->down) {
for (Node* temp = node->right; temp != node; temp = temp->right) {
temp->down->up = temp->up;
temp->up->down = temp->down;
temp->head->size--;
}
}
}
void uncoverColumn(Node* col) {
for (Node* node = col->up; node != col; node = node->up) {
for (Node* temp = node->left; temp != node; temp = temp->left) {
temp->head->size++;
temp->down->up = temp;
temp->up->down = temp;
}
}
col->left->right = col;
col->right->left = col;
}
void search(int k) {
if (HeadNode->right == HeadNode) {
timer2 = clock() - timer;
int Grid[SIZE][SIZE] = { {0} };
MapSolutionToGrid(Grid);
NumberOfSolutions++;
if (NumberOfSolutions%1000000 == 0) {
cout << "There are over " << NumberOfSolutions << " solutions in standard form." << endl;
}
// To see each solution, uncomment below:
// PrintGrid(Grid);
// cout << "Time Elapsed: " << (float)timer2 / CLOCKS_PER_SEC << " seconds.\n\n";
// cin.get(); //Pause console
timer = clock();
isSolved = true;
return;
}
//Choose Column Object Deterministically: Choose the column with the smallest Size
Node* Col = HeadNode->right;
for (Node* temp = Col->right; temp != HeadNode; temp = temp->right)
if (temp->size < Col->size)
Col = temp;
coverColumn(Col);
for (Node* temp = Col->down; temp != Col; temp = temp->down) {
solution[k] = temp;
for (Node* node = temp->right; node != temp; node = node->right) {
coverColumn(node->head);
}
search(k + 1);
temp = solution[k];
solution[k] = NULL;
Col = temp->head;
for (Node* node = temp->left; node != temp; node = node->left) {
uncoverColumn(node->head);
}
}
uncoverColumn(Col);
}
//===============================================================================================================//
//----------------------Functions to turn a Sudoku grid into an Exact Cover problem -----------------------------//
//===============================================================================================================//
//--------------------------BUILD THE INITIAL MATRIX CONTAINING ALL POSSIBILITIES--------------------------------//
void BuildSparseMatrix(bool matrix[ROW_NB][COL_NB]) {
// Calculates the factor Pairs.
int FParray[intCount][2];
FParray[0][0] = 1;
FParray[0][1] = SIZE;
FParray[1][0] = SIZE;
FParray[1][1] = 1;
FParray[2][0] = a;
FParray[2][1] = b;
FParray[3][0] = b;
FParray[3][1] = a;
// FParray[4][0] = fp2;
// FParray[4][1] = fp3;
// FParray[5][0] = fp3;
// FParray[5][1] = fp2;
for (int row=0,r=0; r<SIZE; r++)
for (int c=0; c<SIZE; c++)
for (int i=0; i<SIZE; i++,row++)
{
//uniqueness constraint
matrix[row][r+SIZE*c]=1;
//Factor Pair constraint (including Row & column)
for (int pair = 0; pair < intCount; pair++)
{
matrix[row][(pair+1)*(SIZE*SIZE)+i+SIZE*(block(r,c,pair))] = 1;
}
}
}
//-------------------BUILD A TOROIDAL DOUBLY LINKED LIST OUT OF THE SPARSE MATRIX-------------------------//
void BuildLinkedList(bool matrix[ROW_NB][COL_NB]) {
Node* header = new Node;
header->left = header;
header->right = header;
header->down = header;
header->up = header;
header->size = -1;
header->head = header;
Node* temp = header;
//Create all Column Nodes
for (int i = 0; i < COL_NB; i++) {
Node* newNode = new Node;
newNode->size = 0;
newNode->up = newNode;
newNode->down = newNode;
newNode->head = newNode;
newNode->right = header;
newNode->left = temp;
temp->right = newNode;
temp = newNode;
}
int ID[3] = { 0,1,1 };
//Add a Node for each 1 present in the sparse matrix and update Column Nodes accordingly
for (int i = 0; i < ROW_NB; i++) {
Node* top = header->right;
Node* prev = NULL;
if (i != 0 && i%SIZE_SQUARED == 0) {
ID[0] -= SIZE - 1;
ID[1]++;
ID[2] -= SIZE - 1;
}
else if (i!= 0 && i%SIZE == 0) {
ID[0] -= SIZE - 1;
ID[2]++;
}
else {
ID[0]++;
}
for (int j = 0; j < COL_NB; j++, top = top->right) {
if (matrix[i][j]) {
Node* newNode = new Node;
newNode->rowID[0] = ID[0];
newNode->rowID[1] = ID[1];
newNode->rowID[2] = ID[2];
if (prev == NULL) {
prev = newNode;
prev->right = newNode;
}
newNode->left = prev;
newNode->right = prev->right;
newNode->right->left = newNode;
prev->right = newNode;
newNode->head = top;
newNode->down = top;
newNode->up = top->up;
top->up->down = newNode;
top->size++;
top->up = newNode;
if (top->down == top)
top->down = newNode;
prev = newNode;
}
}
}
HeadNode = header;
}
//-------------------COVERS VALUES THAT ARE ALREADY PRESENT IN THE GRID-------------------------//
void TransformListToCurrentGrid(int Puzzle[][SIZE]) {
int index = 0;
for(int i = 0 ; i<SIZE; i++ )
for(int j = 0 ; j<SIZE; j++)
if (Puzzle[i][j] > 0) {
Node* Col = NULL;
Node* temp = NULL;
for (Col = HeadNode->right; Col != HeadNode; Col = Col->right) {
for (temp = Col->down; temp != Col; temp = temp->down)
if (temp->rowID[0] == Puzzle[i][j] && (temp->rowID[1] - 1) == i && (temp->rowID[2] - 1) == j)
goto ExitLoops;
}
ExitLoops: coverColumn(Col);
orig_values[index] = temp;
index++;
for (Node* node = temp->right; node != temp; node = node->right) {
coverColumn(node->head);
}
}
}
//===============================================================================================================//
//----------------------------------------------- Print Functions -----------------------------------------------//
//===============================================================================================================//
void MapSolutionToGrid(int Sudoku[][SIZE]) {
for (int i = 0; solution[i] != NULL; i++) {
Sudoku[solution[i]->rowID[1]-1][solution[i]->rowID[2]-1] = solution[i]->rowID[0];
}
for (int i = 0; orig_values[i] != NULL; i++) {
Sudoku[orig_values[i]->rowID[1] - 1][orig_values[i]->rowID[2] - 1] = orig_values[i]->rowID[0];
}
}
//---------------------------------PRINTS A SUDOKU GRID OF ANY SIZE---------------------------------------------//
void PrintGrid(int Sudoku[][SIZE]){
cout << "Solution " << NumberOfSolutions << endl << endl;
for (int i = 0; i < SIZE; i++) {
cout << "\\foreach\\x[count=\\i] in{";
for (int j = 0; j < SIZE; j++) {
if (Sudoku[i][j] != 0) {
cout << Sudoku[i][j];
}
if (j < SIZE-1) {
cout << " , ";
}
}
cout << "}{\\node at(\\i-0.5," << SIZE - .5 - i << "){$\\x$};};" << endl;
// cout << " }" << endl;
}
// cout << " }" << endl;
cout << endl;
}
//--------------------------------------------------------------------------------//
void SolveSudoku(int Sudoku[][SIZE]) {
timer = clock();
BuildSparseMatrix(matrix);
BuildLinkedList(matrix);
TransformListToCurrentGrid(Sudoku);
search(0);
if (!isSolved)
cout << "No Solution!" << endl;
isSolved = false;
}
int main(){
// Starting Puzzle
int Puzzle[SIZE][SIZE] = {
{1, 2, 3, 4, 5, 6, 7, 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}
};
int EmptyPuzzle[SIZE][SIZE] = { {0} };
SolveSudoku(Puzzle);
cout << "There are " << NumberOfSolutions << " solutions" << endl;
// cin.get(); // Pauses the outputs at each solution
return 0;
}