to be knight path

MiniZinc model

include "globals.mzn"; 
int: n = 4;
array[1..n*n] of string: s = ["to1     ",
                              "be1     ",
                              "or      ",
                              "not     ",
                              "to2     ",
                              "be2     ",
                              "that    ",
                              "is      ",
                              "the1    ",
                              "question",
                              "whether ",
                              "'tis    ",
                              "nobler  ",
                              "in      ",
                              "the2    ",
                              "mind    "
                              ];
array[1..n, 1..n] of var 1..n*n: x;
solve satisfy;
constraint
  all_different([x[i,j] | i,j in 1..n]) /\
  forall(k in 1..n*n-2) (
     exists(i, j in 1..n) (
        k = x[i, j] /\
        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
        ) 
        ( k + 1 = x[i+a, j+b] )
     ));
output[
  if i = 1 /\ j = 1 then "<li><pre>" else "" endif ++
  if j = 1 then "\n" else " " endif ++
    show(s[fix(x[i,j])]) ++
  if i = n /\ j = n then "</pre>" else "" endif
  | i, j in 1..n
] ++ ["\n"];

Back to Constrained-based constraint poetry
Created by Hakan Kjellerstrand hakank@gmail.com