/* Filling a table with ticks problem in Picat. From Stack Overflow puzzle of filling 4*6 table with 18 ticks http://stackoverflow.com/questions/5739538/puzzle-of-filling-46-table-with-18-ticks """ I have a table of 4*6 and wants to fill with total 18 ticks so that sum of tick on each column and row is even and no row or column is empty. """ Notes: - There are 480 solutions of the original problem (18 ticks). - The minimal number of ticks that is possible is 12. This Picat model was created by Hakan Kjellerstrand, hakank@gmail.com See also my Picat page: http://www.hakank.org/picat/ */ import cp. main => go. go ?=> Rows = 4, Cols = 6, Ticks :: 0..Rows*Cols, Ticks #= 18, % decision variables X = new_array(Rows,Cols), X :: 0..1, % number of ticks sum([X[I,J] : I in 1..Rows, J in 1..Cols]) #= Ticks, % ensure the row constraints: sum >= 1 and even foreach(I in 1..Rows) S :: 1..Cols, S #= sum([X[I,J] : J in 1..Cols]), S mod 2 #= 0 end, % ensure the column constraints: sum >= 1 and even foreach(J in 1..Cols) S :: 1..Rows, S #= sum([X[I,J] : I in 1..Rows]), S mod 2 #= 0 end, % symmetry breaking X[1,1] #= 1, if var(Ticks) then solve($[min(Ticks)],X) else solve(X) end, println(ticks=Ticks), foreach(Row in X) println(Row) end, nl, fail, nl. go => true.