/*
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.
latin_square(N, X) =>
X = new_array(N,N),
X :: 1..N,
foreach(Row in X) all_distinct(Row) end,
foreach(Column in X.columns()) all_distinct(Column) end.
% symmetry breaking: value in row R must not be R
% foreach(R in 1..N) X[R,R] #\= R end.
pretty_print(X) =>
foreach(Row in X) writeln(Row) end.