You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

113 lines
3.4 KiB

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