%
% Knights path problem in MiniZinc.
%
% Create a knights path in a n x n matrix for all integers from 1..n*n-1.
% The integer n*n is placed whatever it may fit...
%
%
% This MiniZinc model was created by Hakan Kjellerstrand, hakank@bonetmail.com
% See also my MiniZinc page: http://www.hakank.org/minizinc
%
include "globals.mzn";
int: n = 6;
array[1..n, 1..n] of var 1..n*n: x;
% solve satisfy;
solve :: int_search([x[i,j] | i,j in 1..n], occurrence, indomain_min, complete) satisfy;
constraint
all_different([x[i,j] | i,j in 1..n])
/\ % note: place all integers 1..n-1
forall(k in 1..n*n-2) (
exists(i, j in 1..n) (
k = x[i, j] % fix this k
/\ % the knight moves
exists(a, b in {-2, -1, 1, 2}
where
i+a >= 1 /\ j+b >= 1
/\
i+a <= n /\ j+b <= n
/\
abs(a)+abs(b) = 3
)
(
% find the next k
k + 1 = x[i+a, j+b]
)
)
)
;
output[
if j = 1 then "\n" else " " endif ++
show(x[i,j])
| i, j in 1..n
] ++ ["\n"]
;