/*
Talisman square in Picat.
http://mathworld.wolfram.com/TalismanSquare.html
"""
An nXn array of the integers from 1 to n^2 such that the difference between
any one integer and its neighbor (horizontally, vertically, or
diagonally, without wrapping around) is greater than or equal to
some value k is called a n,k)-talisman square.
"""
Model created by Hakan Kjellerstrand, hakank@gmail.com
See also my Picat page: http://www.hakank.org/picat/
*/
import cp.
main => go.
go ?=>
N = 5,
K = 4,
talisman_square(N, K, X),
pretty_print(X),
fail.
go => true.
talisman_square(N,K,X) =>
X = new_array(N,N),
X :: 1..N*N,
all_different(vars(X)),
foreach(I in 2..N, J in 2..N)
abs(X[I,J]-X[I-1,J]) #>= K,
abs(X[I,J]-X[I,J-1]) #>= K
end,
foreach(I in 1..N-1,J in 1..N-1)
abs(X[I,J]-X[I+1,J]) #>= K,
abs(X[I,J]-X[I,J+1]) #>= K
end,
% some symmetry breaking
X[1,1] #= 1,
solve(X).
pretty_print(X) =>
foreach(Row in X)
foreach(R in Row) printf("%2d ",R) end,
nl
end,
nl.