/* Latin squares in Picat. http://en.wikipedia.org/wiki/Latin_square: """ A Latin square is an n X n table filled with n different symbols in such a way that each symbol occurs exactly once in each row and exactly once in each column. """ Model created by Hakan Kjellerstrand, hakank@gmail.com See also my Picat page: http://www.hakank.org/picat/ */ import util. import cp. main => go. go => N = 40, latin_square(N, X), solve([ff],X), pretty_print(X), nl. go2 => N = 100, latin_square(N, X), writeln(solve), solve([ff],X), pretty_print(X), nl. % % Number of solutions for different N. Traditional form % go3 => foreach(N in 1..10) Count = count_all((latin_square(N,X),solve([ff],X))), println(N=Count) end, nl. % % Reduced form % go4 => foreach(N in 1..10) Count = count_all((latin_square_reduced(N,X),solve([ff],X))), println(N=Count) end, nl. latin_square(N, X) => X = new_array(N,N), X :: 1..N, foreach(Row in X) all_different(Row) end, foreach(Column in X.columns()) all_different(Column) end. % symmetry breaking: value in row R must not be R % foreach(R in 1..N) X[R,R] #\= R end. % % Reduced form % latin_square_reduced(N, X) => X = new_array(N,N), X :: 1..N, foreach(Row in X) all_different(Row) end, foreach(Column in X.columns()) all_different(Column) end, foreach(I in 1..N) X[1,I] #= I, X[I,1] #= I end. pretty_print(X) => foreach(Row in X) writeln(Row) end.