echecDabord(Affectation) :-
variables(Variables),
echecDabord(Variables,[],Affectation).
echecDabord([],_,[]).
echecDabord(Variables,AffectationPartielle,[(X,V)|Solution]) :-
extraitMin(Variables,X:DX,VariablesSansX),
member(V,DX),
filtre(X,V,VariablesSansX,VariablesFiltres),
echecDabord(VariablesFiltres,[(X,V)|AffectationPartielle],Solution).
filtre(_,_,[],[]).
filtre(Xi,Vi,[Xj:Dj|Variables],[Xj:DjFiltre|DomainesFiltres]) :-
bagof(Vj,(member(Vj,Dj),consistants((Xi,Vi),(Xj,Vj))), DjFiltre),
DjFiltre \= [],
filtre(Xi,Vi,Variables,DomainesFiltres).
extraitMin([X:DX|Variables],Xmin:DXmin,VariablesSansXmin) :-
length(DX,N),
extraitMin(Variables,X:DX,N,Xmin:DXmin,VariablesSansXmin).
extraitMin([],X:DX,_,X:DX,[]).
extraitMin([X:DX|Variables],Y:DY,N,Z:DZ,[Y:DY|VariablesSansZ]) :-
length(DX,M),
M<N, !,
extraitMin(Variables,X:DX,M,Z:DZ,VariablesSansZ).
extraitMin([X:DX|Variables],Y:DY,N,Z:DZ,[X:DX|VariablesSansZ]) :-
extraitMin(Variables,Y:DY,N,Z:DZ,VariablesSansZ).