-
Notifications
You must be signed in to change notification settings - Fork 0
/
sudoku
466 lines (404 loc) · 12.8 KB
/
sudoku
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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
// GE523: Discrete Event Systems
// A Sudoku Solver in C++ written by Prof. R.S. Sreenivas
// This is written in a "C" like syntax. You can think about how to
// put this into an include file etc on your own.
//
// There is an ILP formulation for any 9 x 9 Sudoku Puzzle
// See the handout for the formulation -- there are 729 variables
// with a whole lot of constraints that is driven by the input
// (which is the incomplete board-layout). You can write this
// (moderate-size) ILP automatically in C++, which is then solved
// using the API for Lpsolve.
//#define WIN32
#include <iostream>
#include <iomanip>
#include <cmath>
#include <fstream>
#include <cstdlib>
#include "lp_lib.h"
using namespace std;
// Global variables for the problem 9*9*9 = 729
// See my handout for further explanations
lprec *lp;
double solution[729];
// This sets the generic ILP for the Sudoku Puzzle
// It does not use any of the pre-determined board-positions,
// that part is done after the input file is read.
void set_sudoku_ilp()
{
// setting the problem up: 9*9*9 = 729 variables
lp = make_lp(0, 729);
// This keeps the message reporting of lp_solve to a minimum
set_verbose(lp, 3);
// following is the first constraint in the instruction
{
for (int i = 1; i <= 9; i++)
{
for (int j = 1; j <= 9; j++)
{
// constraint that says each (i,j) entry in the 9x9 table
// should have one number in it
// creating a row with 1's at the right spot (the first
// entry has to be a zero; this is idiosynchratic of lpsolve)
double row[730];
for (int k = 0; k < 730; k++)
row[k] = 0;
for (int k = 1; k <= 9; k++)
row[(81 * (i - 1)) + (9 * (j - 1)) + k] = 1; //set all entries in (i,j) position 1 (that is 9 variables)
// adding the constraint
add_constraint(lp, row, EQ, 1);// in the (i,j) position, there should be and only be one k in that position
}
}
}
//set the 2nd constraint (for any j...)
{
// Using the above code as a guide, put the appropriate lines that
// articulate the constraint that says each number must appear once
// in each row in the 9x9 table; create a bunch of rows with 1's at
// the right spot (the first entry has to be a zero; this is
// idiosynchratic of lpsolve)
for (int i = 1; i <= 9; i++)
{
for (int k = 1; k <= 9; k++)
{
// constraint that says each (i,j) entry in the 9x9 table
// should have one number in it
// creating a row with 1's at the right spot (the first
// entry has to be a zero; this is idiosynchratic of lpsolve)
double row[730];
for (int j = 0; j < 730; j++)
row[j] = 0;
for (int j = 1; j <= 9; j++)
row[(81 * (i - 1)) + (9 * (j - 1)) + k] = 1; //set all entries in i row with k value be 1 (that is 9 variables)
// adding the constraint
add_constraint(lp, row, EQ, 1);// in the i row with k value, there should be and only be one k in that position
}
}
}
//set 3rd constraint (for any i...)
{
// Using the above code as a guide, put the appropriate lines that
// articulate the constraint that says each number must appear once
// in each column in the 9x9 table; create a bunch of rows with 1's at
// the right spot (the first entry has to be a zero; this is
// idiosynchratic of lpsolve)
for (int j = 1; j <= 9; j++)
{
for (int k = 1; k <= 9; k++)
{
// constraint that says each (i,j) entry in the 9x9 table
// should have one number in it
// creating a row with 1's at the right spot (the first
// entry has to be a zero; this is idiosynchratic of lpsolve)
double row[730];
for (int i = 0; i < 730; i++)
row[i] = 0;
for (int i = 1; i <= 9; i++)
row[(81 * (i - 1)) + (9 * (j - 1)) + k] = 1; //set all entries in j column with k value be 1 (that is 9 variables)
// adding the constraint
add_constraint(lp, row, EQ, 1);// in the j column with k value, there should be and only be one k in that position
}
}
}
// making sure each number occurs once within each block
// Block 1
{
for (int k = 1; k <= 9; k++)
{
// Using the above code as a guide, put the appropriate lines that
// articulate theconstraint that says each number must appear once
// in each 3 x 3 block; create a bunch of rows with 1's at the right
// spot (the first entry has to be a zero; this is idiosynchratic of
// lpsolve)
double row[730];
for (int i = 0; i < 730; i++)
row[i] = 0;
for (int i = 1; i <= 3; i++)
for (int j = 1; j <= 3; j++)
row[(81 * (i - 1)) + (9 * (j - 1)) + k] = 1;
// adding the constraint
add_constraint(lp, row, EQ, 1);
}
}
// Block 2
{
for (int k = 1; k <= 9; k++)
{
// Using the above code as a guide, put the appropriate lines that
// articulate theconstraint that says each number must appear once
// in each 3 x 3 block; create a bunch of rows with 1's at the right
// spot (the first entry has to be a zero; this is idiosynchratic of
// lpsolve)
double row[730];
for (int i = 0; i < 730; i++)
row[i] = 0;
for (int i = 1; i <= 3; i++)
for (int j = 4; j <= 6; j++)
row[(81 * (i - 1)) + (9 * (j - 1)) + k] = 1;
// adding the constraint
add_constraint(lp, row, EQ, 1);
}
}
// Block 3
{
for (int k = 1; k <= 9; k++)
{
// Using the above code as a guide, put the appropriate lines that
// articulate theconstraint that says each number must appear once
// in each 3 x 3 block; create a bunch of rows with 1's at the right
// spot (the first entry has to be a zero; this is idiosynchratic of
// lpsolve)
double row[730];
for (int i = 0; i < 730; i++)
row[i] = 0;
for (int i = 1; i <= 3; i++)
for (int j = 7; j <= 9; j++)
row[(81 * (i - 1)) + (9 * (j - 1)) + k] = 1;
// adding the constraint
add_constraint(lp, row, EQ, 1);
}
}
// Block 4
{
for (int k = 1; k <= 9; k++)
{
// Using the above code as a guide, put the appropriate lines that
// articulate theconstraint that says each number must appear once
// in each 3 x 3 block; create a bunch of rows with 1's at the right
// spot (the first entry has to be a zero; this is idiosynchratic of
// lpsolve)
double row[730];
for (int i = 0; i < 730; i++)
row[i] = 0;
for (int i = 4; i <= 6; i++)
for (int j = 1; j <= 3; j++)
row[(81 * (i - 1)) + (9 * (j - 1)) + k] = 1;
// adding the constraint
add_constraint(lp, row, EQ, 1);
}
}
// Block 5
{
for (int k = 1; k <= 9; k++)
{
// Using the above code as a guide, put the appropriate lines that
// articulate theconstraint that says each number must appear once
// in each 3 x 3 block; create a bunch of rows with 1's at the right
// spot (the first entry has to be a zero; this is idiosynchratic of
// lpsolve)
double row[730];
for (int i = 0; i < 730; i++)
row[i] = 0;
for (int i = 4; i <= 6; i++)
for (int j = 4; j <= 6; j++)
row[(81 * (i - 1)) + (9 * (j - 1)) + k] = 1;
// adding the constraint
add_constraint(lp, row, EQ, 1);
}
}
// Block 6
{
for (int k = 1; k <= 9; k++)
{
// Using the above code as a guide, put the appropriate lines that
// articulate theconstraint that says each number must appear once
// in each 3 x 3 block; create a bunch of rows with 1's at the right
// spot (the first entry has to be a zero; this is idiosynchratic of
// lpsolve)
double row[730];
for (int i = 0; i < 730; i++)
row[i] = 0;
for (int i = 4; i <= 6; i++)
for (int j = 7; j <= 9; j++)
row[(81 * (i - 1)) + (9 * (j - 1)) + k] = 1;
// adding the constraint
add_constraint(lp, row, EQ, 1);
}
}
// Block 7
{
for (int k = 1; k <= 9; k++)
{
// Using the above code as a guide, put the appropriate lines that
// articulate theconstraint that says each number must appear once
// in each 3 x 3 block; create a bunch of rows with 1's at the right
// spot (the first entry has to be a zero; this is idiosynchratic of
// lpsolve)
double row[730];
for (int i = 0; i < 730; i++)
row[i] = 0;
for (int i = 7; i <= 9; i++)
for (int j = 1; j <= 3; j++)
row[(81 * (i - 1)) + (9 * (j - 1)) + k] = 1;
// adding the constraint
add_constraint(lp, row, EQ, 1);
}
}
// Block 8
{
for (int k = 1; k <= 9; k++)
{
// Using the above code as a guide, put the appropriate lines that
// articulate theconstraint that says each number must appear once
// in each 3 x 3 block; create a bunch of rows with 1's at the right
// spot (the first entry has to be a zero; this is idiosynchratic of
// lpsolve)
double row[730];
for (int i = 0; i < 730; i++)
row[i] = 0;
for (int i = 7; i <= 9; i++)
for (int j = 4; j <= 6; j++)
row[(81 * (i - 1)) + (9 * (j - 1)) + k] = 1;
// adding the constraint
add_constraint(lp, row, EQ, 1);
}
}
// Block 9
{
for (int k = 1; k <= 9; k++)
{
// Using the above code as a guide, put the appropriate lines that
// articulate theconstraint that says each number must appear once
// in each 3 x 3 block; create a bunch of rows with 1's at the right
// spot (the first entry has to be a zero; this is idiosynchratic of
// lpsolve)
double row[730];
for (int i = 0; i < 730; i++)
row[i] = 0;
for (int i = 7; i <= 9; i++)
for (int j = 7; j <= 9; j++)
row[(81 * (i - 1)) + (9 * (j - 1)) + k] = 1;
// adding the constraint
add_constraint(lp, row, EQ, 1);
}
}
// upper-bound each variable by 1
for (int i = 1; i <= 729; i++)
{
double row[730];
for (int j = 0; j < 730; j++)
row[j] = 0;
row[i] = 1;
add_constraint(lp, row, LE, 1);
}
// it does not matter what the objective function (why?)
// I am minimizing the sum of all variables.
{
double row[730];
for (int i = 1; i < 730; i++)
row[i] = 1;
row[0] = 0;
set_obj_fn(lp, row);
}
// set the variables to be integers
for (int i = 1; i <= 729; i++)
set_int(lp, i, TRUE);
}
// This subroutine reads the incomplete board-information from the
// input file and sets the appropriate constraints before the ILP
// is solved.
void read_input_data(char* argv[])
{
// reading the input filename from commandline
ifstream input_filename(argv[1]);
if (input_filename.is_open()) {
cout << "Input File Name: " << argv[1] << endl;
cout << endl << "Initial Board Position" << endl;
for (int i = 1; i <= 9; i++)
{
for (int j = 1; j <= 9; j++)
{
int value_just_read;
input_filename >> value_just_read;
// check if we have a legitimate integer between 1 and 9
if ((value_just_read >= 1) && (value_just_read <= 9))
{
// printing initial value of the puzzle with some formatting
cout << value_just_read << " ";
double row[730];
for (int k = 0; k < 730; k++)
row[k] = 0;
row[(81 * (i - 1)) + (9 * (j - 1)) + value_just_read] = 1;//in the (i,j) position, the value was just read
add_constraint(lp, row, EQ, 1);
// add appropriate constraints that bind the value of the
// appropriate variables based on the incomplete information
// that was read from the input file
}
else {
// printing initial value of the puzzle with some formatting
cout << "X ";
}
}
cout << endl;
}
}
else {
cout << "Input file missing" << endl;
exit(0);
}
}
// The ILP formulation is solved using the API for Lpsolve
// Pay attention to how the solution is interpretted...
void solve_the_puzzle()
{
int count = 1;
while(TRUE)
{
int ret;
// solve the lp
ret = solve(lp);
// Check if you had a solution
// (see online guide for the codes at http://lpsolve.sourceforge.net/5.0/ )
if (ret == 0)
{
// get the optimal assignment
get_variables(lp, solution);
// print the solution
cout << endl << "Final Solution #:" << count << endl;
{
int result[9][9];
for (int i = 0; i < 9; i++)
for (int j = 0; j < 9; j++)
result[i][j] = 0;
double temp[730];
temp[0] = 0;
for (int i = 1; i < 730; i++)
temp[i] = solution[i - 1];
for (int i = 0; i <= 8; i++)
for (int j = 0; j <= 8; j++)
for (int k = 1; k <= 9; k++)
if (temp[(81 * i) + (9 * j) + k] == 1)
result[i][j] = k;
for (int i = 0; i < 9; i++)
{
for (int j = 0; j < 9; j++)
{
cout << result[i][j] << " ";
}
cout << endl;
}
// Figure out a way to look at the 729-long 0/1 vector solution
// to the ILP and print the appropriate integer value to be
// inserted into each square of the solution to the Sudoku puzzle
add_constraint(lp, temp, LE, 80);
count++;
}
}
else {
cout << "seems there is no more solutions..." << endl;
cout << "exiting the system...";
break;
}
}
delete_lp(lp);
}
int main(int argc, char* argv[])
{
// formulate the non-input related part of the puzzle
set_sudoku_ilp();
// read the incomplete input information, and set appropriate constraints
read_input_data(argv);
// solve the puzzle and print the solution
solve_the_puzzle();
return(0);
}