model "Sudoku" uses "mmxprs" !gain access to the Xpress-Optimizer solver declarations !Declarations section for data and variables m = 3 n = 9 S: array(1..n,1..n) of integer ! S is the input puzzle x: array(1..n,1..n,1..n) of mpvar ! x is the decision variable end-declarations ! Input puzzle S := [ 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 6, 0, 0, 0, 0, 3, 0, 7, 4, 0, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 2, 0, 8, 0, 0, 4, 0, 0, 1, 0, 6, 0, 0, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 7, 8, 0, 5, 0, 0, 0, 0, 9, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 0] ! Constraints ! Each cell contains a single integer forall(i in 1..n, j in 1..n) sum(k in 1..n) x(i,j,k) = 1 ! Each integer appears once in a row forall(i in 1..n, k in 1..n) sum(j in 1..n) x(i,j,k) = 1 ! Each integer appears once in a column forall(j in 1..n, k in 1..n) sum(i in 1..n) x(i,j,k) = 1 ! Each integer appears once in a subgrid forall(p in 1..m, q in 1..m, k in 1..n) sum(j in m*q-m+1..m*q, i in m*p-m+1..m*p) x(i,j,k) = 1 ! Enter cells with given integer values */ forall(i in 1..n, j in 1..n) do if(S(i,j) > 0) then x(i,j,S(i,j)) = 1 end-if end-do ! Each variable is 0/1 forall(i in 1..n, j in 1..n, k in 1..n) x(i,j,k) is_binary minimize(0) ! Objective function ! Code to output the solution writeln(" Solution") forall(i in 1..n) do forall(j in 1..n, k in 1..n) do if(getsol(x(i,j,k)) > 0) then write(k, " ") end-if end-do writeln end-do end-model