2014-04-21 1 views
4

이 퍼즐 프로그램에 대해 올바른 양의 솔루션을 인쇄하는 데 문제가 있습니다. 올바른 퍼즐을 인쇄하지만 필요한 솔루션의 양은 적절하지 않습니다.프롤로그 순열 색상 올바른 조합 수 오류

여기에 각 상황이하는 작업은 다음과 같습니다

퍼즐 1 - 당신은 5 개 가지 색상 유무 : 없음 같은 색이 서로 인접하지 않을 수 2 파란색, 2 녹색 1 노란색 합니다.

퍼즐 2 - 빨강, 파랑, 검정의 6 가지 색이 있습니다. 행에 검은 색이 2 개까지만 있습니다.

퍼즐 3 -

8 개 개의 색상이 3 개 그린, 2 백인, 2 개 빨간색과 한 검은 색. 흰색은 결코 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

"적절한 해결책이 필요하지 않다"는 것은 무엇을 의미합니까? 너무 많습니다 (반복)? 너무 적습니다 (일부 누락)? 반복되는 솔루션 인 경우 순열이 고유하지 않기 때문입니다 (목록에서 고유하지 않은 요소가 치환됨에 따라). – lurker

+0

너무 많은 솔루션 반복. – user3555214

답변

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 

이 문제를 해결하는 가장 저렴한 방법함으로써 중복 솔루션 제거, 각 permutationQ/1 목표 주위에 setof(t, Goal, _)을 래핑하는 것입니다 :

| ?- setof(t,permutationQ([red,red],P),_). 
P = [red,red] ? ; 
no 

은 일반적으로을에 (=)/2dif/2를 사용하는 것을 고려 (==)/2(\+)/2의 자리. 또한 조합 문제는 으로 가장 적절하게 해결됩니다.

+0

솔루션 도움에 감사드립니다. 그러나 sit2에 대한 코드를 실행했을 때 약 6 가지 솔루션 만있었습니다. 하지만 최소한 8 살이라고 생각합니다. 웬일인지, 나는이 상황을 검은 색, 검은 색, 빨간색, 파란색, 검은 색, 검은 색과 검은 색, 검은 색, 파란색, 빨간색, 검은 색, 검은 색으로 가질 수 없습니다. 이것을 가질 수있는 방법이 있습니까? – user3555214

+0

nvrmind. 고쳤다. – user3555214