我遇到了为这个拼图程序打印适量解决方案的问题。它打印出正确的拼图,但不是所需的正确解决方案。
以下是每种情况的作用:
益智1 - 你有五种颜色:2蓝色,2绿色和1黄色 没有相同的颜色可以彼此相邻。
拼图2 - 你有六种颜色:1个红色,1个蓝色和4个黑色 连续不超过2个黑色。
拼图3 -
你有八种颜色:3种绿色,2种白色,2种红色和1种黑色。 白人从不在A和H. 位置D和H的颜色相同。 A和G中的颜色必须是不同的颜色。 红色永远不会出现在F和G. 果岭从不在B和C。 在每个红色的左边,有一个绿色。
% a program that find solutions for each of the following colored ball problems with different sets of constraints.
% to run, type either
% sit1, sit2 or sit3.
% select an element for use in permutation test
%
% If the element is the head of the list, then it is in the list, and the tail is left
selectE(Element, [Element|Tail], Tail).
% If the two lists have the same head, check for more elements in the rest of the lists
selectE(Element, [Head|Tail1], [Head|Tail2]) :-
selectE(Element, Tail1, Tail2).
% generate permutations
%
% The empty list is a permutation of itself
permutationQ([],[]).
% List1 is a permutation of List2 if each element occurs in both lists
% the same number of times
permutationQ(List, [Head|Tail]) :- selectE(Head, List, Rest),
permutationQ(Rest, Tail).
%
% There are 5 colors - 2 blues, 2 greens, 1 yellow
%
sit1 :- permutationQ([green,green,blue,blue,yellow],[A,B,C,D,E]),
\+ A=B, \+ B=C, \+ C=D, \+ D=E,
printout([A,B,C,D,E]). % print any solution you find
% print solutions of sit1
printout([A,B,C,D,E]) :-
nl,
write('The order of colors from top to bottom is: '), nl,
write(A),nl,
write(B),nl,
write(C),nl,
write(D),nl,
write(E),nl.
% There are 6 colors - 1 red, 1 blue, 4 blacks,
%
sit2 :- permutationQ([black,black,black,black,red,blue],[A,B,C,D,E,F]),
((A==red -> D==blue);
(A==blue -> D==red);
(B==red -> E==blue);
(B==blue -> E==red);
(C==red -> F==blue);
(C==blue -> F==red);
(D==red -> C==blue);
(D==blue -> C==red)),
printout2([A,B,C,D,E,F]). % print any solution you find
% print solutions of sit2
printout2([A,B,C,D,E,F]) :-
nl,
write('The order of colors from top to bottom is: '), nl,
write(A),nl,
write(B),nl,
write(C),nl,
write(D),nl,
write(E),nl,
write(F),nl.
% There are 8 colors - 3 greens, 2 whites, 2 reds, 1 black
sit3 :- permutationQ([black,white,white,red,red,green,green,green],[A,B,C,D,E,F,G,H]),
% The colors in B and C are not green.
\+ B=green,
\+ C=green,
% The colors in E and F are not green because the colors in F and G are not red.
\+ E=green,
\+ F=green,
% Since red can't be in H, green can't be in G.
\+ G=green,
% The colors in D and H are the same color.
D=H,
% The colors in A and G are of different colors.
\+ A=G,
% The color in F and G are not red.
\+ F=red,
\+ G=red,
% Red can't be in A because there isn't any other position on the left for the green.
\+ A=red,
% The colors in C and D are not red because the colors in B and C are not green.
\+ C=red,
\+ D=red,
% Whites are neither A nor H.
\+ A=white,
\+ H=white,
% White is not on D because white can't be on H.
\+ D=white,
printout3([A,B,C,D,E,F,G,H]). % print any solution you find
% print solutions of sit3
printout3([A,B,C,D,E,F,G,H]) :-
nl,
write('The order of colors from top to bottom is: '), nl,
write(A),nl,
write(B),nl,
write(C),nl,
write(D),nl,
write(E),nl,
write(F),nl,
write(G),nl,
write(H),nl.
答案 0 :(得分:5)
您的裁员来源取决于您使用permutationQ/2
的方式。要看到这一点,请考虑目标
| ?- permutationQ([red,red],P).
P = [red,red] ? ;
P = [red,red] ? ;
no
您期待一个答案/解决方案,但您将获得一个解决方案和一个冗余解决方案。背后的原因是permutationQ/2
只是描述了所有可能的排列,无论它们的实际内容如何。要看到这个:
| ?- permutationQ([X,Y],P).
P = [X,Y] ? ;
P = [Y,X] ? ;
no
解决此问题的最便宜方法是围绕每个setof(t, Goal, _)
目标包裹permutationQ/1
,从而消除多余的解决方案:
| ?- setof(t,permutationQ([red,red],P),_).
P = [red,red] ? ;
no
一般来说,请考虑使用(=)/2
和dif/2
代替(==)/2
和(\+)/2
。此外,clpfd最恰当地解决了组合问题。