1 /* SUDOKU, Number Placement Puzzle */
3 /* Written in GNU MathProg by Andrew Makhorin <mao@mai2.rcnet.ru> */
5 /* This example shows how to use the table statement.
6 The sudoku to be solves is read from file sudoku_in.csv.
7 The solution is written to sudoku_out.csv.
8 The file format is CSV as defined in
9 RFC 4180 - Common Format and MIME Type for
10 Comma-Separated Values (CSV) Files */
12 /* Sudoku, also known as Number Place, is a logic-based placement
13 puzzle. The aim of the canonical puzzle is to enter a numerical
14 digit from 1 through 9 in each cell of a 9x9 grid made up of 3x3
15 subgrids (called "regions"), starting with various digits given in
16 some cells (the "givens"). Each row, column, and region must contain
17 only one instance of each numeral.
21 +-------+-------+-------+
22 | 5 3 . | . 7 . | . . . |
23 | 6 . . | 1 9 5 | . . . |
24 | . 9 8 | . . . | . 6 . |
25 +-------+-------+-------+
26 | 8 . . | . 6 . | . . 3 |
27 | 4 . . | 8 . 3 | . . 1 |
28 | 7 . . | . 2 . | . . 6 |
29 +-------+-------+-------+
30 | . 6 . | . . . | 2 8 . |
31 | . . . | 4 1 9 | . . 5 |
32 | . . . | . 8 . | . 7 9 |
33 +-------+-------+-------+
35 (From Wikipedia, the free encyclopedia.) */
40 param givens{1..9, 1..9}, integer, >= 0, <= 9, default 0;
44 table ti IN 'MySQL' 'Database=glpk;UID=glpk;PWD=gnu'
46 fields <- [COL, LIN], givens ~ VAL;
48 table ti IN 'MySQL' 'Database=glpk;UID=glpk;PWD=gnu'
49 'SELECT * FROM sudoku WHERE ID = ' & id :
50 fields <- [COL, LIN], givens ~ VAL;
52 var x{i in 1..9, j in 1..9, k in 1..9}, binary;
53 /* x[i,j,k] = 1 means cell [i,j] is assigned number k */
55 s.t. fa{i in 1..9, j in 1..9, k in 1..9: givens[i,j] != 0}:
56 x[i,j,k] = (if givens[i,j] = k then 1 else 0);
57 /* assign pre-defined numbers using the "givens" */
59 s.t. fb{i in 1..9, j in 1..9}: sum{k in 1..9} x[i,j,k] = 1;
60 /* each cell must be assigned exactly one number */
62 s.t. fc{i in 1..9, k in 1..9}: sum{j in 1..9} x[i,j,k] = 1;
63 /* cells in the same row must be assigned distinct numbers */
65 s.t. fd{j in 1..9, k in 1..9}: sum{i in 1..9} x[i,j,k] = 1;
66 /* cells in the same column must be assigned distinct numbers */
68 s.t. fe{I in 1..9 by 3, J in 1..9 by 3, k in 1..9}:
69 sum{i in I..I+2, j in J..J+2} x[i,j,k] = 1;
70 /* cells in the same region must be assigned distinct numbers */
72 /* there is no need for an objective function here */
76 table ta{(i,j) in fields} OUT
77 'MySQL' 'Database=glpk;UID=glpk;PWD=gnu'
78 'DELETE FROM sudoku_solution'
79 'WHERE ID = ' & id & ';'
80 'INSERT INTO sudoku_solution'
82 'VALUES(?, ?, ?, ?);' :
83 id ~ ID, i ~ COL, j ~ LIN, (sum{k in 1..9} x[i,j,k] * k) ~ VAL;
85 printf "\nSudoku to be solved\n";
87 { for {0..0: i = 1 or i = 4 or i = 7}
88 printf " +-------+-------+-------+\n";
90 { for {0..0: j = 1 or j = 4 or j = 7} printf(" |");
91 printf " %d", givens[i,j];
92 for {0..0: j = 9} printf(" |\n");
95 printf " +-------+-------+-------+\n";
97 printf "\nSolution\n";
99 { for {0..0: i = 1 or i = 4 or i = 7}
100 printf " +-------+-------+-------+\n";
102 { for {0..0: j = 1 or j = 4 or j = 7} printf(" |");
103 printf " %d", sum{k in 1..9} x[i,j,k] * k;
104 for {0..0: j = 9} printf(" |\n");
107 printf " +-------+-------+-------+\n";