爱因斯坦难题的序言 [英] Einstein puzzle in Prolog

查看:113
本文介绍了爱因斯坦难题的序言的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Prolog解决爱因斯坦之谜.任务是

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

    挪威人住在第一所房子里.
  1. 英国人住在红房子里.
  2. 瑞典的HAS狗作为宠物.
  3. 丹麦人喝茶.
  4. 温室位于白宫的左侧.
  5. 住在温室里的男人喝咖啡.
  6. 抽烟颇尔购物中心的人饲养鸟类.
  7. 住在黄宫的那个人抽着Dunhill的烟.
  8. 住在中间房子的那个人喝牛奶.
  9. 抽烟的人住在有猫的人旁边.
  10. 养马的人与抽Dunhill的人住在一起.
  11. 抽蓝大师烟的男人喝啤酒.
  12. 德国人抽王子.
  13. 挪威人住在蓝屋边.
  14. 抽烟的人是喝水的人的邻居.
  15. 某人有一个带鱼的水族馆.
  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 .

程序:

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).

推荐答案

这是您必须解决该问题的原因之一.下面的程序片段删除了很多目标,但仍然失败.仅可见部分已经引起了故障.您能从这个片段中找出原因吗?

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).

这篇关于爱因斯坦难题的序言的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆