-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolver.cpp
More file actions
141 lines (120 loc) · 3.29 KB
/
Copy pathsolver.cpp
File metadata and controls
141 lines (120 loc) · 3.29 KB
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
#include"convert.h"
#include<chrono>
#define pvec(v) for(auto elem: v) cout << elem << " ";cout << endl;
using namespace std;
using namespace std::chrono;
vector<int> *tally;
string puzzle = "puzzles/grid5.txt";
int perfCounter = 0;
Node* chooseColumn(Node* root) {
Node *lowest = nullptr, *temp = root->right;
int mn = 1000000000;
while (temp!=root) {
if (temp->ones < mn) {
mn = temp->ones;
lowest = temp;
}
temp = temp->right;
}
return lowest;
}
void cover(Node *col) {
col->right->left = col->left;
col->left->right = col->right;
Node *currRow = col->down, *temp;
while (currRow!=col) {
temp = currRow->right;
while (temp!=currRow) {
temp->colHeader->ones-=1;
temp->up->down = temp->down;
temp->down->up = temp->up;
temp = temp->right;
}
currRow = currRow->down;
}
}
void uncover(Node *col) {
Node* currRow = col->up, *temp;
while (currRow!=col) {
temp = currRow->left;
while (temp!=currRow) {
temp->colHeader->ones+=1;
temp->up->down = temp;
temp->down->up = temp;
temp = temp->left;
}
currRow = currRow->up;
}
col->left->right = col;
col->right->left = col;
}
void printSolution(vector<int> &ps) {
auto sudoku = getSudoku(puzzle);
int **grid = sudoku.first, n = sudoku.second;
// int grid[4][4], n = 4;
for (auto elem: ps) {
grid[tally[elem][0]][tally[elem][1]] = tally[elem][2];
}
fstream fout;
fout.open("answers.txt", ios::out);
for (int i = 0; i<n; ++i) {
for (int j = 0; j<n; ++j)
fout << grid[i][j] << " ";
fout << "\n";
}
fout.close();
}
bool search(Node *root, vector<int> &partialSolution) {
// No column left to cover
perfCounter+=1;
if ((root->left == root) && (root->right == root)) {
printSolution(partialSolution);
return true;
}
Node *col = chooseColumn(root), *curr = col->down, *currRow;
cover(col);
while (curr!=col) {
partialSolution.push_back(curr->row);
currRow = curr->right;
while (currRow!=curr) {
cover(currRow->colHeader);
currRow = currRow->right;
}
if (search(root, partialSolution))
return true;
currRow = curr->left;
while (currRow!=curr) {
uncover(currRow->colHeader);
currRow = currRow->left;
}
partialSolution.pop_back();
curr = curr->down;
}
uncover(col);
return false;
}
void printColWise(Node* root) {
Node *temp = root->right;
int cnt = 0;
while (temp!=root) {
Node *goDown = temp->down;
while (goDown!=temp) {
++cnt;
goDown = goDown->down;
}
temp = temp->right;
}
}
int main() {
auto start = high_resolution_clock::now();
auto res = get_dlx(puzzle);
Node *root = res.first;
tally = res.second;
vector<int> ps;
if (!search(root, ps))
cout << "No Solution Possible!!!";
auto stop = high_resolution_clock::now();
auto duration = duration_cast<microseconds>(stop - start);
cout << "Time: " << duration.count()/1000.0 << " ms" << endl;
return 0;
}