-
Notifications
You must be signed in to change notification settings - Fork 2
/
solver.js
342 lines (302 loc) · 8.66 KB
/
solver.js
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
var completion;
var solveBtn = document.getElementById("SolveButton");
var resetBtn = document.getElementById("ResetButton");
solveBtn.addEventListener('click', SolveSudoku);
resetBtn.addEventListener('click', ResetGrid);
var MAX_K = 1000;
var SIZE = 9;
//var SIZE = 4;
var SIZE_SQUARED = SIZE*SIZE;
var SIZE_SQRT = Math.sqrt(SIZE);
var ROW_NB = SIZE*SIZE*SIZE;
var COL_NB = 4*SIZE*SIZE;
//Node Object Constructor
function Node(){
this.left = this;
this.right = this;
this.up = this;
this.down = this;
this.head = this;
this.size = 0;
this.rowID = [0,0,0];
}
var HeadNode = new Node();
var solution = [];
var orig_values = [];
var Grid = [];
var matrix = [];
var row = [];
for(var i = 0; i<COL_NB; i++){
row.push(0);
}
for(var i = 0; i<ROW_NB;i++){
matrix.push(row.slice());
}
var isSolved = false;
function SolveSudoku(){
BuildSparseMatrix(matrix);
BuildLinkedList(matrix);
var Sudoku = [];
var tempRow = [];
for(var i = 0; i<SIZE; i++)
tempRow.push(0);
for(i =0;i<SIZE;i++)
Sudoku.push(tempRow.slice());
var str = "R1C1";
for(i = 1; i<10; i++){
str = str.slice(0,1) + i + str.slice(2,3);
for(var j = 1; j<10; j++){
str = str.slice(0,3)+j;
var value = Number(document.getElementById(str).value);
if(value > 0)
Sudoku[i-1][j-1] = value;
}
}
TransformListToCurrentGrid(Sudoku);
Search(0);
if(!isSolved)
document.getElementById("SolvedText").innerHTML = "No Solution";
else
document.getElementById("SolvedText").innerHTML = "Solved!";
isSolved=false;
}
//===============================================================================================================//
//---------------------------------------------DLX Functions-----------------------------------------------------//
//===============================================================================================================//
function CoverColumn(col){
col.left.right = col.right;
col.right.left = col.left;
for(var node = col.down; node!==col; node = node.down){
for(var temp = node.right; temp!==node; temp = temp.right){
temp.down.up = temp.up;
temp.up.down = temp.down;
temp.head.size--;
}
}
}
function UncoverColumn(col){
for (var node = col.up; node!== col; node = node.up){
for(var 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;
}
function Search(k){
if(HeadNode.right === HeadNode){
var tempRow = [];
for(var u = 0; u<SIZE; u++){
tempRow.push(0);
}
for(u = 0; u<SIZE; u++){
Grid.push(tempRow.slice());
}
MapSolutionToGrid(Grid);
SolvedPuzzleOutput(Grid);
isSolved = true;
return;
}
if(!isSolved){
//Choose Column Object Deterministically: Choose the column with the smallest Size
var Col = HeadNode.right;
for(var temp = Col.right; temp.size>-1; temp = temp.right){
if(temp.size < Col.size){
Col = temp;
}
}
CoverColumn(Col);
for(temp = Col.down; temp!==Col; temp = temp.down){
solution[k] = temp;
for(var 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 = 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--------------------------------//
function BuildSparseMatrix(matrix){
var j = 0, counter = 0;
//Constraint 1 : There can only be one value in any given cell
for (var i = 0; i < ROW_NB; i++){
matrix[i][j] = 1;
counter++;
if(counter >= SIZE){
j++;
counter = 0;
}
}
//Constraint 2 : There can only be one instance of a number in any given row
var x = 0;
counter = 1;
for(j = SIZE_SQUARED; j<2*SIZE_SQUARED; j++){
for (i = x; i < counter*SIZE_SQUARED; i += SIZE)
matrix[i][j] = 1;
if ((j + 1) % SIZE === 0) {
x = counter*SIZE_SQUARED;
counter++;
}
else
x++;
}
//Constraint 3: There can only be one instance of a number in any given column
j = 2 * SIZE_SQUARED;
for (i = 0; i < ROW_NB; i++){
matrix[i][j] = 1;
j++;
if (j >= 3 * SIZE_SQUARED)
j = 2 * SIZE_SQUARED;
}
//Constraint 4: There can only be one instance of a number in any given region
x = 0;
for (j = 3 * SIZE_SQUARED; j < COL_NB; j++) {
for (var l = 0; l < SIZE_SQRT; l++) {
for (var k = 0; k<SIZE_SQRT; k++)
matrix[x + l*SIZE + k*SIZE_SQUARED][j] = 1;
}
var temp = j + 1 - 3 * SIZE_SQUARED;
if (temp % (SIZE_SQRT * SIZE) === 0)
x += (SIZE_SQRT - 1)*SIZE_SQUARED + (SIZE_SQRT - 1)*SIZE + 1;
else if (temp % SIZE === 0)
x += SIZE*(SIZE_SQRT - 1) + 1;
else
x++;
}
}
//-------------------BUILD A TOROIDAL DOUBLY LINKED LIST OUT OF THE SPARSE MATRIX-------------------------//
function BuildLinkedList(matrix){
var header = new Node();
header.size = -1;
var temp = header;
//Create all Column Nodes
for (var i = 0; i<COL_NB; i++){
var newNode = new Node();
newNode.left = temp;
newNode.right = header;
temp.right = newNode;
temp = newNode;
}
var ID = [0,1,1];
//Add a Node for each 1 present in the sparse matrix and update Column Nodes accordingly
for(i = 0; i<ROW_NB; i++){
var topNode = header.right;
var prev = 5;
if(i !== 0 && i%SIZE_SQUARED === 0){
ID= new Array(ID[0]-(SIZE-1),ID[1]+1,ID[2]-(SIZE-1));
}
else if(i!==0 && i%SIZE === 0){
ID = new Array(ID[0]-(SIZE-1),ID[1],ID[2]+1);
}
else{
ID = new Array(ID[0]+1,ID[1],ID[2]);
}
for(var j = 0; j<COL_NB; j++, topNode = topNode.right){
if(matrix[i][j] === 1){
var tempNode = new Node();
tempNode.rowID = ID;
if(prev === 5){
prev = tempNode;
}
tempNode.left = prev;
tempNode.right = prev.right;
tempNode.right.left = tempNode;
prev.right = tempNode;
tempNode.head = topNode;
tempNode.down = topNode;
tempNode.up = topNode.up;
topNode.up.down = tempNode;
topNode.up = tempNode;
if(topNode.size === 0){
topNode.down = tempNode;
}
topNode.size++;
prev = tempNode;
}
}
}
HeadNode = header;
}
//-------------------COVERS VALUES THAT ARE ALREADY PRESENT IN THE GRID-------------------------//
function TransformListToCurrentGrid(Puzzle){
orig_values = [];
for(var n=0; n<SIZE; n++){
for(var m=0; m<SIZE; m++){
if(Puzzle[n][m] > 0){
loop1:
for(var Col = HeadNode.right; Col!==HeadNode; Col = Col.right){
loop2:
for(temp = Col.down; temp!==Col; temp = temp.down){
if(temp.rowID[0] === Puzzle[n][m] && (temp.rowID[1]-1) == n && (temp.rowID[2]-1) == m){
break loop1;
}
}
}
CoverColumn(Col);
orig_values.push(temp);
for(var node = temp.right; node!=temp; node = node.right){
CoverColumn(node.head);
}
}
}
}
}
//===============================================================================================================//
//----------------------------------------------- Print Functions -----------------------------------------------//
//===============================================================================================================//
function MapSolutionToGrid(grid){
for(var t=0; solution[t] != null; t++){
grid[solution[t].rowID[1]-1][solution[t].rowID[2]-1] = solution[t].rowID[0];
}
for(t=0; orig_values[t] != null; t++){
grid[orig_values[t].rowID[1]-1][orig_values[t].rowID[2]-1] = orig_values[t].rowID[0];
}
}
function SolvedPuzzleOutput(Sudoku){
str = "R1C1";
for(var i = 1; i<10; i++){
str = str.slice(0,1) + i + str.slice(2,3);
for(var j = 1; j<10; j++){
str = str.slice(0,3)+j;
var textField = document.getElementById(str);
if(textField.value == '')
textField.style.color = "#000000";
textField.value = Sudoku[i-1][j-1];
//textField.disabled = true;
textField.readOnly = true;
}
}
}
function ResetGrid(){
str = "R1C1";
for(var i = 1; i<10; i++){
str = str.slice(0,1) + i + str.slice(2,3);
for(var j = 1; j<10; j++){
str = str.slice(0,3)+j;
var textField = document.getElementById(str);
//textField.disabled = false;
textField.readOnly = false;
textField.value = "";
textField.style.color = "red";
}
}
document.getElementById("SolvedText").innerHTML = " ";
}
function limitKey(evt,num){
var key = window.evt? evt.keyCode : evt.which;
if(key<49||key>57) return false;
else if(num.length == 1) return false;
else return true;
}