Something went wrong. Try again.
This repository has no description
Something went wrong. Try again.
2.1 kB · 72 lines
at develop
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273%------------------------------------------------------------------------------%% Placing 'Hearts' on Corners in Equilateral Triangular Grids%% The problem is to find, for an equilateral triangular grid of% side N, the maximum number of grid nodes that can have a 'heart' placed% on them without any placed heart lying on the corners of an equilateral% triangle of any size or orientation. %% (Taken from Daily Telegraph and Sunday Times)%------------------------------------------------------------------------------%% Parametersint: n; % Number of sides of the gridset of int: N = 1..n;%------------------------------------------------------------------------------%% Variablesarray [N, N] of var 0..1: heart; % Grid of equilateral triangularsvar 1..n*n: objective; % Number of 'hearts' in the gridconstraint objective = sum(i in N, j in 1..i)(heart[i, j]);%------------------------------------------------------------------------------%% Constraints % 'Hearts' constraints %constraint forall(i in 1..n, j in 1..i, k in 1..n-i, m in 0..k-1)( heart[i+m, j] + heart[i+k, j+m] + heart[i+k-m, j+k-m] <= 2 );constraint forall(i in 1..n, j in (i+1)..n)( heart[i, j] = 0 );%------------------------------------------------------------------------------%% Search and Objectivesolve :: search maximize objective;ann: search = int_search( [heart[i, j] | i in N, j in 1..i], input_order, indomain_max, complete );%------------------------------------------------------------------------------%% Outputoutput [ "objective = ", show(objective), ";\n", "heart = array2d(", show(N), ", ", show(N), ", [\n"] ++ [ show(heart[i, j]) ++ if j = n /\ i = n then "" else ", " endif ++ if j = n then "\n" else "" endif| i in N, j in 1..n] ++ [ "]);\n"] ++ [ if j = 1 then "%% " else "" endif ++ show(heart[i, j]) ++ " " ++ if j == i then "\n" else "" endif| i in N, j in 1..i ];%------------------------------------------------------------------------------%