爱因斯坦难题的序言 [英] Einstein puzzle in Prolog
本文介绍了爱因斯坦难题的序言的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使用Prolog解决爱因斯坦之谜.任务是
I'm trying to solve the Einstein riddle using Prolog. Task is
-
挪威人住在第一所房子里.
- 英国人住在红房子里.
- 瑞典的HAS狗作为宠物.
- 丹麦人喝茶.
- 温室位于白宫的左侧.
- 住在温室里的男人喝咖啡.
- 抽烟颇尔购物中心的人饲养鸟类.
- 住在黄宫的那个人抽着Dunhill的烟.
- 住在中间房子的那个人喝牛奶.
- 抽烟的人住在有猫的人旁边.
- 养马的人与抽Dunhill的人住在一起.
- 抽蓝大师烟的男人喝啤酒.
- 德国人抽王子.
- 挪威人住在蓝屋边.
- 抽烟的人是喝水的人的邻居.
- 某人有一个带鱼的水族馆.
- The Norwegian lives in the first house .
- The English lives in the Red House .
- The Swedish HAS Dogs As pets .
- The Danish drinks tea .
- The Green House is on the left of the White House.
- The man who lives in the green house drinks coffee .
- The man who smokes Pall Mall rears birds .
- The man living in the Yellow House smokes Dunhill .
- The man who lives in the Middle house drinks milk .
- The man who smokes Blends lives next to the one who Has Cats .
- The man who keeps horses lives next to the one who smokes Dunhill .
- The man who smokes Blue Master drinks beer .
- The German smokes Prince .
- The Norwegian lives next to the Blue House side .
- The man who smokes Blends is Neighbour do of the one who drinks water .
- 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屋!
查看全文