/* Balanced 0-1 matrix in Picat. See http://en.wikipedia.org/wiki/Dynamic_programming A type of balanced 0-1 matrix """ Consider the problem of assigning values, either zero or one, to the positions of an n x n matrix, n even, so that each row and each column contains exactly n/2 zeros and n/2 ones. """ This Picat model was created by Hakan Kjellerstrand, hakank@gmail.com See also my Picat page: http://www.hakank.org/picat/ */ import cp,util. main => go. go ?=> N = 6, % must be even M = N div 2, X = new_array(N,N), X :: 0..1, if N mod 2 != 0 then print("N (%d) must be even!", N), halt end, XT = X.transpose, foreach(I in 1..N) count(1,X[I],M), count(1,XT[I],M) end, solve(X), foreach(Row in X) println(Row) end, nl, fail, nl. go => true.