-
Notifications
You must be signed in to change notification settings - Fork 2
/
Source.cpp
371 lines (315 loc) · 11.7 KB
/
Source.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
#include <iomanip>
#include <cstdlib>
#include "DancingLinks.h"
using namespace std;
const int TOTAL_WIDTH = 50;
const int PADDING_CHAR = '-';
enum MENU { RANDOM = 1, BACKTRACKING, DANCINGLINKS, EXIT };
enum STATES { UNASSIGNED };
// Type alias for a 2D vector of integers and 2D array (9x9 Sudoku board)
using VectorSudokuBoard = vector<vector<int>>;
using ArraySudokuBoard = int[SIZE][SIZE];
// Prototypes
VectorSudokuBoard createDifficultSudokuBoard();
VectorSudokuBoard createRandomSudokuBoard(int arrayBoard[][SIZE]);
bool solveSudokuUsingBacktracking(VectorSudokuBoard& board);
int findEmpty(const VectorSudokuBoard& board, int& row, int& col);
bool isSafe(const VectorSudokuBoard& board, int row, int col, int num);
void printCenteredTitle(string title, int total_width, char padding_char);
void clearBoards(VectorSudokuBoard vectorBoard, ArraySudokuBoard arrayBoard);
void purgeInputErrors(string error_mess);
void printArrayGridWithBorders(int vectorBoard[][SIZE]);
void printVectorGridWithBorders(vector<vector<int>> vectorBoard);
int main() {
cout << "Welcome\n\n" <<
"The purpose of this project is to demonstrate the efficacy of different approaches to the exact cover problem.\n"
"For a standard 9x9 Sudoku Board, the exact cover problem is to find a solution that satisfies the following constraints:\n\n"
"1. Cell Constraint: Each cell must contain exactly one number\n"
"2. Row Constraint: Each number must appear exactly once in each row\n"
"3. Column Constraint: Each number must appear exactly once in each column\n"
"4. Box Constraint: Each number must appear exactly once in each 3x3 subgrid\n\n";
printCenteredTitle("Backtracking Algorithm", TOTAL_WIDTH, PADDING_CHAR);
cout << "Time Complexity: O(9^(N*N)) --> For every unassigned index, there are 9 possible options\n"
"Space Complexity: O(N*N)\n\n";
printCenteredTitle("Dancing Links Algorithm", TOTAL_WIDTH, PADDING_CHAR);
cout << "Time Complexity: O(N!) -------> At worst, the algorithm attempts every permutation of rows\n"
"Space Complexity: O(N*N)\n\n";
printCenteredTitle("HOWEVER", TOTAL_WIDTH, PADDING_CHAR);
cout << "Sudoku boards are typically sparse, meaning that most cells are empty.\n"
"This means that the time complexity of the Dancing Links algorithm is much closer to O(N^2), as we will soon see.\n\n";
int selection{ 0 };
VectorSudokuBoard vectorBoard;
ArraySudokuBoard arrayBoard = { {0} };
DancingLinks dlx;
do {
cout << "MENU\nSelect one of the following:\n"
"1. GENERATE RANDOM SUDOKU BOARD\n"
"2. SOLVE USING BACKTRACKING\n"
"3. SOLVE USING DANCING LINKS\n"
"4. EXIT\n"
"Selection: ";
cin >> selection;
switch (selection) {
case RANDOM:
// Clear boards before randomly generating values
clearBoards(vectorBoard, arrayBoard);
vectorBoard = createRandomSudokuBoard(arrayBoard);
printCenteredTitle("Randomly Generated Sudoku Board", TOTAL_WIDTH, PADDING_CHAR);
/*
cout << "Initial Vector Sudoku Board:\n";
printVectorGridWithBorders(vectorBoard);
*/
cout << "Initial Array Sudoku Board:\n";
printArrayGridWithBorders(arrayBoard);
break;
case BACKTRACKING:
if (solveSudokuUsingBacktracking(vectorBoard)) {
printCenteredTitle("Backtracking Algorithm", TOTAL_WIDTH, PADDING_CHAR);
cout << "\nSolved Vector Sudoku Board:\n";
printVectorGridWithBorders(vectorBoard);
}
else {
cout << "\nNo solution exists for the given Sudoku board." << endl;
}
break;
case DANCINGLINKS:
printCenteredTitle("Dancing Links (DLX) Algorithm", TOTAL_WIDTH, PADDING_CHAR);
/*
cout << "Array Board Before passing to dlx:\n";
printArrayGridWithBorders(arrayBoard);
*/
dlx.solveSudoku(arrayBoard);
cout << "Solved Array Sudoku Board:\n";
printArrayGridWithBorders(arrayBoard);
break;
case EXIT:
cout << "Terminating Program\n\n";
break;
default:
purgeInputErrors("Invalid selection");
}
} while (selection != EXIT);
return 0;
}
// Returns an example of a difficult-to-brute-force Sudoku board
VectorSudokuBoard createDifficultSudokuBoard() {
VectorSudokuBoard board = {
{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}
};
return board;
}
// Generates a random 9x9 Sudoku board
VectorSudokuBoard createRandomSudokuBoard(int arrayBoard[][SIZE]) {
// Seed the random number generator with the current time
srand(static_cast<unsigned>(time(nullptr)));
// Initialize an empty 9x9 Sudoku board with zeros
VectorSudokuBoard board = VectorSudokuBoard(9, vector<int>(9, 0));
// Fill a random number of cells with valid numbers (between 1 and 9)
for (int i = 0; i < 20; i++) { // Adjust the number of filled cells as needed
int row = rand() % 9;
int col = rand() % 9;
int num = rand() % 9 + 1; // Random number between 1 and 9
if (isSafe(board, row, col, num)) {
board[row][col] = num;
arrayBoard[row][col] = num;
}
}
return board;
}
// Uses backtracking to solve the Sudoku board
bool solveSudokuUsingBacktracking(VectorSudokuBoard& board) {
int row, col;
clock_t timer1;
// If there are no unassigned locations, we are done
if (!findEmpty(board, row, col))
return true; // success!
// Consider digits 1 to 9
for (int num = 1; num <= 9; num++) {
// Check if it looks promising
if (isSafe(board, row, col, num)) {
// Make tentative assignment
board[row][col] = num;
// Return, if success
if (solveSudokuUsingBacktracking(board)) {
return true;
}
// Failure, unmake & try again
board[row][col] = UNASSIGNED;
}
}
// This triggers backtracking
return false;
}
// Searches the Sudoku board to find an empty cell
int findEmpty(const VectorSudokuBoard& board, int& row, int& col) {
for (row = 0; row < 9; row++) {
for (col = 0; col < 9; col++) {
if (board[row][col] == 0) {
return 1;
}
}
}
return 0;
}
// Checks if it's safe to place a number in the given cell
bool isSafe(const VectorSudokuBoard& board, int row, int col, int num) {
// Check if the number is not already present in the current row
for (int i = 0; i < 9; i++) {
if (board[row][i] == num) {
return false;
}
}
// Check if the number is not already present in the current column
for (int i = 0; i < 9; i++) {
if (board[i][col] == num) {
return false;
}
}
// Check if the number is not already present in the current 3x3 subgrid
auto subgridStartRow = static_cast<vector<vector<int>>::size_type> (row) - (row % 3);
auto subgridStartCol = static_cast<vector<vector<int>>::size_type> (col) - (col % 3);
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (board[subgridStartRow + i][subgridStartCol + j] == num) {
return false;
}
}
}
// If none of the above conditions are violated, it's safe to place the number
return true;
}
void printCenteredTitle(string title, int total_width, char padding_char) {
int title_length = title.length();
string truncatedTitle = title;
if (title_length > total_width) {
truncatedTitle = title.substr(0, total_width);
title_length = truncatedTitle.length();
}
// Determine Padding
int num_padding_chars = (total_width - title_length) / 2;
string padding(num_padding_chars, padding_char);
// Ensure symmetry by checking for odd total width and even title length
bool needs_extra_padding = (total_width % 2 != 0) && (title_length % 2 == 0);
// Use std::string for efficient string concatenation
string output = "\n";
// Append padding and title to output
output += padding;
if (needs_extra_padding) {
output += padding_char;
}
// Check for non-empty title
if (title_length > 0) {
output += " " + truncatedTitle + " " + padding;
}
else {
output += std::string(2, padding_char) + padding;
}
cout << output << "\n\n";
}
void clearBoards(VectorSudokuBoard vectorBoard, ArraySudokuBoard arrayBoard) {
vectorBoard.clear();
for (int i = 0; i < SIZE; i++) {
for (int j = 0; j < SIZE; j++) {
arrayBoard[i][j] = 0;
}
}
}
void purgeInputErrors(string error_mess) {
cout << "ERROR: " << error_mess << "\n";
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
}
void printArrayGridWithBorders(int sudoku[][SIZE]) {
string borderEXT = "+", borderINT = "|";
int counter = 1;
int additional = 0;
if (SIZE > 9) {
additional = SIZE;
}
for (int i = 0; i < ((SIZE + SIZE_SQRT - 1) * 2 + additional + 1); i++) {
borderEXT += '-';
if (i > 0 && i % ((SIZE_SQRT * 2 + SIZE_SQRT * (SIZE > 9) + 1) * counter + counter - 1) == 0) {
borderINT += '+';
counter++;
}
else {
borderINT += '-';
}
}
borderEXT += '+';
borderINT += "|";
cout << borderEXT << endl;
for (int i = 0; i < SIZE; i++) {
cout << "| ";
for (int j = 0; j < SIZE; j++) {
if (sudoku[i][j] == 0) {
cout << ". ";
}
else {
cout << sudoku[i][j] << " ";
}
if (additional > 0 && sudoku[i][j] < 10) {
cout << " ";
}
if ((j + 1) % SIZE_SQRT == 0) {
cout << "| ";
}
}
cout << endl;
if ((i + 1) % SIZE_SQRT == 0 && (i + 1) < SIZE) {
cout << borderINT << endl;
}
}
cout << borderEXT << endl << endl;
}
void printVectorGridWithBorders(vector<vector<int>> vectorBoard) {
string borderEXT = "+", borderINT = "|";
int counter = 1;
int additional = 0;
if (SIZE > 9) {
additional = SIZE;
}
for (int i = 0; i < ((SIZE + SIZE_SQRT - 1) * 2 + additional + 1); i++) {
borderEXT += '-';
if (i > 0 && i % ((SIZE_SQRT * 2 + SIZE_SQRT * (SIZE > 9) + 1) * counter + counter - 1) == 0) {
borderINT += '+';
counter++;
}
else {
borderINT += '-';
}
}
borderEXT += '+';
borderINT += "|";
cout << borderEXT << endl;
for (int i = 0; i < SIZE; i++) {
cout << "| ";
for (int j = 0; j < SIZE; j++) {
if (vectorBoard[i][j] == 0) {
cout << ". ";
}
else {
cout << vectorBoard[i][j] << " ";
}
if (additional > 0 && vectorBoard[i][j] < 10) {
cout << " ";
}
if ((j + 1) % SIZE_SQRT == 0) {
cout << "| ";
}
}
cout << endl;
if ((i + 1) % SIZE_SQRT == 0 && (i + 1) < SIZE) {
cout << borderINT << endl;
}
}
cout << borderEXT << endl << endl;
}