Einstein puzzle in Prolog

大城市里の小女人 提交于 2019-12-11 05:02:10

问题


I'm trying to solve the Einstein riddle using Prolog. Task is

  1. The Norwegian lives in the first house .
  2. The English lives in the Red House .
  3. The Swedish HAS Dogs As pets .
  4. The Danish drinks tea .
  5. The Green House is on the left of the White House.
  6. The man who lives in the green house drinks coffee .
  7. The man who smokes Pall Mall rears birds .
  8. The man living in the Yellow House smokes Dunhill .
  9. The man who lives in the Middle house drinks milk .
  10. The man who smokes Blends lives next to the one who Has Cats .
  11. The man who keeps horses lives next to the one who smokes Dunhill .
  12. The man who smokes Blue Master drinks beer .
  13. The German smokes Prince .
  14. The Norwegian lives next to the Blue House side .
  15. The man who smokes Blends is Neighbour do of the one who drinks water .
  16. Someone has one aquarium with fish .

The program:

neighbor(Rua):-
   length(Rua, 5),
   Rua = [casa(_,noruegues,_,_,_)|_],
   member(casa(vermelha,ingles,_,_,_),Rua),
   member(casa(_,sueco,_,_,cachorros),Rua),
   member(casa(_,dinamarques,cha,_,_),Rua),
   esquerda(casa(verde,_,_,_,_), casa(branca,_,_,_,_),Rua),
   member(casa(verde,_,cafe,_,_),Rua),
   member(casa(_,_,_,pallmall,passaros),Rua),
   member(casa(amarela,_,_,dunhill,_),Rua),
   Rua = [_,_,casa(_,_,leite,_,_),_,_],
   ao_lado(casa(_,_,_,blends,_), casa(_,_,_,_,gatos),Rua),
   ao_lado(casa(_,_,_,_,cavalos), casa(_,_,_,dunhill,_),Rua),
   member(casa(_,_,cerveja,bluemaster,_),Rua),
   member(casa(_,alemao,_,prince,_),Rua),
   ao_lado(casa(_,noruegues,_,_,_), casa(azul,_,_,_,_),Rua),
   ao_lado(casa(_,_,_,blends,_), casa(_,_,agua,_,_),Rua),
   member(casa(_,_,_,_,peixes),Rua).

ao_lado([X,Y|_],X, Y).
ao_lado([X,Y|_],Y, X).
ao_lado([_|L],X, Y):-
   ao_lado(L, X, Y).

esquerda([A|As], A, E) :-
   member2(E, As).
esquerda([_|As], A, E) :-
   esquerda(As, A, E).

回答1:


Here is one reason you have to address to solve this problem. Below program fragment has quite a lot of goals removed, yet it still fails. The visible part alone is already responsible for the failure. Can you spot the reason from this fragment?

(For more on this method see this explanation.)

:- op(950, fy, *).
*_.

:- initialization(neighbor(_Rua)).

neighbor(_/*Rua*/):-
   * length(Rua, 5),
   * Rua = [casa(_,noruegues,_,_,_)|_],
   * member(casa(vermelha,ingles,_,_,_),Rua),
   * member(casa(_,sueco,_,_,cachorros),Rua),
   * member(casa(_,dinamarques,cha,_,_),Rua),
   esquerda(casa(_/*verde*/,_,_,_,_), _/*casa(branca,_,_,_,_)*/,Rua),
   * member(casa(verde,_,cafe,_,_),Rua),
   * member(casa(_,_,_,pallmall,passaros),Rua),
   * member(casa(amarela,_,_,dunhill,_),Rua),
   * Rua = [_,_,casa(_,_,leite,_,_),_,_],
   * ao_lado(casa(_,_,_,blends,_), casa(_,_,_,_,gatos),Rua),
   * ao_lado(casa(_,_,_,_,cavalos), casa(_,_,_,dunhill,_),Rua),
   * member(casa(_,_,cerveja,bluemaster,_),Rua),
   * member(casa(_,alemao,_,prince,_),Rua),
   * ao_lado(casa(_,noruegues,_,_,_), casa(azul,_,_,_,_),Rua),
   * ao_lado(casa(_,_,_,blends,_), casa(_,_,agua,_,_),Rua),
   * member(casa(_,_,_,_,peixes),Rua).

esquerda([A|As], _/*A*/, E) :-
   * member(E, As).
esquerda([_|As], A, E) :-
   * esquerda(As, A, E).


来源:https://stackoverflow.com/questions/38928611/einstein-puzzle-in-prolog

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