Interpreters riddle in prolog

风格不统一 提交于 2019-12-12 01:57:30

问题


Here's a riddle's info


Six interpreters: Fran French, Geraldine German, Dudley Dutch, Spike Spanish, Polly Polish and Romanian Ron. Everyone speaks two languages, among them indicating their surnames, namely French (French), Deutsch (German), Dutch (Dutch), Spanish (Spanish), Polish (Polish) and Romanian ( Romanian). The following applies:

  1. No interpreter speak the language corresponding to his surname.
  2. No two interpreters speak the same two languages ​
  3. Each of the six languages is ​​spoken by exactly two interpreters.
  4. Spike speaks Dutch and German.
  5. There is an interpreter who speaks Dutch and Polish.
  6. Fran and Dudley speak four languages ​​(two each) that do not correspond to their surnames.
  7. Interpreters who have as surnames the languages ​​spoken by Dudley, speak French.
  8. There is no interpreter who can speak German and Polish.

What languages ​​are spoken by every interpreter?


This is what I've done until now. Rules 1,4 and 8 are returning right results.

interpreter(fran,french).
interpreter(geraldine,german).
interpreter(dudley,dutch).
interpreter(spike,spanish).
interpreter(polly,polish).
interpreter(ron,romanian).

language(french).
language(german).
language(dutch).
language(spanish).
language(polish).
language(romanian).



%rule 1                                                                     OK
rule1([X,Y,Z,W]):- interpreter(X,Y), language(Z), language(W),   
                not(Z=W;Y=Z;Y=W),
                Z\=W, Z@<W.


%rule 2    
    %rule2([X,Y,Z,W]):- interpreter(X,Y), language(Z), language(W).
                    %interpreter(I2,S2), language(Z2), language(W2).
                    %not(X1=X2),
                    %((Z1=Z2) -> not(W1=W2)).



%rule 3
rule3([X,Y,Z,W]):- interpreter(X,Y), language(Z1), language(W), 
                interpreter(X,Y), language(Z1), language(W), 
                interpreter(X,Y), language(Z2), language(W), 
                not(Z1=Z2).

%rule 4                                                                 OK
rule4([X,Y,Z,W]):-  (X=spike -> Z=dutch,W=german;Z=_,W=_).

%rule 5
rule5([X,Y,dutch,polish]).

%rule 6
rule6a([fran,french,Z,W]):- interpreter(X,Y), language(Z1), language(W), 
                not(Z=dutch).
rule6b([dudley,dutch,Z,W]):- interpreter(X,Y), language(Z1), language(W), 
                not(Z=french).
    /*      
%rule 7
rule7([dudley,dutch,Z,W]):- rule7a(X,Z,L1,french),
                        rule7b(X,Z,french,L2),
                        rule7c(X,W,L1,french),
                        rule7d(X,W,french,L2).
                        */
%rule 8                                                                 OK
rule8([X,Y,Z,W]):- interpreter(X,Y), language(Z), language(W), 
                ((Z=german)-> not(W=polish);Z=_,W=_).   


solution(X):- rule1(X), rule2(X), rule3(X), rule4(X), rule5(X), rule6(X), rule7(X), rule8(X), .

I need help mostly with rules 2 and 7. Any help appreciated.


回答1:


I tried to solve this puzzle here

I find this way of putting constraints in problems like these easier.

:- use_rendering(table,
         [header(h('Name','Surname','L1','L2'))]).

interpreters(Is) :-
    length(Is,6),
    member(h(fran,french,_,_), Is),
    member(h(geraldine,german,_,_), Is),
    member(h(dudley,dutch,_,_), Is),
    member(h(spike,spanish,_,_), Is),
    member(h(polly,polish,_,_), Is),
    member(h(ron,romanian,_,_), Is),
    member(h(_,_,french,_), Is),member(h(_,_,_,french), Is),
    member(h(_,_,german,_), Is),member(h(_,_,_,german), Is),
    member(h(_,_,dutch,_), Is),member(h(_,_,_,dutch), Is),
    member(h(_,_,spanish,_), Is),member(h(_,_,_,spanish), Is),
    member(h(_,_,polish,_), Is),member(h(_,_,_,polish), Is),
    member(h(_,_,romanian,_), Is),member(h(_,_,_,romanian), Is),
    \+member(h(_,X,X,_), Is),
    \+member(h(_,Y,_,Y), Is),
    (member(h(spike,spanish,dutch,german), Is);member(h(spike,spanish,german,dutch), Is)),
    (member(h(_,_,dutch,polish), Is);member(h(_,_,polish,dutch), Is)),
    member(h(fran,french,E,F), Is),member(h(dudley,dutch,G,H), Is),E \= G, F \= G, E \= H, F \= H,
    member(h(dudley,dutch,I,J), Is), (member(h(_,I,_,french), Is);member(h(_,I,french,_), Is)), (member(h(_,J,_,french), Is);member(h(_,J,french,_), Is)),
    \+member(h(_,_,german,polish), Is),
    \+member(h(_,_,polish,german), Is),
    \+member(h(_,_,K,K), Is).

One of the outputs is



来源:https://stackoverflow.com/questions/43500776/interpreters-riddle-in-prolog

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!