1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
| like(laitue,chou).
like(ail,carotte).
like(oignon, carotte).
like(oignon,tomate).%rajout
notlike(tomate,chou).
notlike(carotte,aneth).
% on introduit la commutativité
incompatibles(X, Y) :-
notlike(X,Y); notlike(Y, X).
solution(L, Rebut) :-
% constitution de la liste des légumes
bagof([X,Y], like(X,Y), L1),
bagof([X,Y], notlike(X,Y), L2),
flatten([L1|L2], LF),
% cree une liste sans doublons
sort(LF, Lst),
cree_liste(Lst, L, Rebut).
% on initialise la liste avec deux éléments non incompatibles
cree_liste(Lst, L, Rebut) :-
select(Elem1, Lst, Lst1),
select(Elem2, Lst1, Lst2),
\+incompatibles(Elem1,Elem2),
cree_liste(Lst2, [Elem1, Elem2], L, Rebut).
% derniers elements de la liste des légumes
cree_liste([X, Y], [H | T], L, Rebut) :-
( (\+incompatibles(X,H), \+incompatibles(Y, H), \+incompatibles(X,Y))
-> L = [X, Y, H | T], Rebut = []
; L = [H | T], Rebut = [X, Y]).
% on parcourt la liste des legumes, et on apparie
cree_liste(Lst, [H1, H2 | T], L, Rebut) :-
select(Elem, Lst, Lst1),
\+incompatibles(Elem,H1),\+incompatibles(Elem,H2),
( cree_liste(Lst1, [Elem, H1, H2 | T], L_, Rebut_)
-> L = L_, Rebut = Rebut_
; L = [H1, H2 | T], Rebut = Lst). |
Partager