I\'m trying to solve a constraint processing problem in prolog.
I need to pack 4 squares of 5x5,4x4,3x3 and 2x2 in a grid of 10x10. They may not overlap.
My
Here is a solution where disjoint only takes one line:
% disjoint(+Rectangle, +Rectangle)
disjoint([XA1,XA2,YA1,YA2],[XB1,XB2,YB1,YB2]) :-
XB1 #>= XA2 #\/ XA1 #>= XB2 #\/
YB1 #>= YA2 #\/ YA1 #>= YB2.
The model setup and labeling works as follows:
% squares(-List)
squares(L) :-
maplist(square, [2,3,4,5], L),
term_variables(L, V),
place(L),
label(V).
% square(+Integer, -Rectangle)
square(S, [X1,X2,Y1,Y2]) :-
X1 in 0..8,
X2 in 1..9,
Y1 in 0..6,
Y2 in 1..7,
X2 #= X1+S,
Y2 #= Y1+S.
% place(+List)
place([]).
place([A|L]) :-
place(L, A),
place(L).
% place(+List, +Rectangle)
place([], _).
place([A|L], B) :-
disjoint(A, B),
place(L, B).
Here is an example run:
Jekejeke Prolog 3, Runtime Library 1.3.7 (May 23, 2019)
?- squares(L), show(L).
555554444
555554444
555554444
555554444
55555333.
22...333.
22...333.
L = [[0,2,5,7],[5,8,4,7],[5,9,0,4],[0,5,0,5]]