爱因斯坦谜语与术语列表 [英] Einstein Riddle with List of terms
问题描述
我在 Prolog 中实现了 Einstein Riddle,并试图找出谁在家里养了一条鱼.
我在此代码中找不到错误,并且跟踪选项无助于解决此问题;)
规则:
- 挪威人住在第一所房子里
- 英国人住在红房子里.
- 温室就位于白宫的左侧.
- 丹麦人喝茶.
- 轻度吸烟者生活在猫的饲养者旁边.
- 黄房子的居民抽着雪茄.
- 德国人抽水烟.
- 中心住宅的一位居民喝牛奶.
- 轻度吸烟者有一个喝水的邻居.
- 吸烟不带过滤嘴繁殖鸟类.
- 瑞典犬.
- 挪威人住在蓝房子旁边.
- 养马的人住在黄房子旁边.
- 抽薄荷醇喝啤酒.
- 他们在温室里喝咖啡.
这是我的代码:
on_the_left(X, Y, N) :-Y 是 X - 1,+ Y <1、+ X >N.next_to(X, Y, N) :-( Y 是 X + 1;Y 是 X - 1),+ X >N,+ Y >N,+ X <1、+ Y <1.鱼(谁):-房子= [房子(1,_Color1,_From1,_Animal1,_Drink1,_Smoke1),房子(2,_Color2,_From2,_Animal2,_Drink2,_Smoke2),房子(3,_Color3,_From3,_Animal3,_Drink3,_Smoke3),房子(4,_Color4,_From4,_Animal4,_Drink4,_Smoke4),房子(5,_Color5,_From5,_Animal5,_Drink5,_Smoke5)],N 为 5,%-- 提示 1成员(房子(1,_,挪威,_,_,_),房子),%-- 提示 2成员(房子(_,红色,英格兰,_,_,_),房子),%-- 提示 3 - on_the_left成员(房子(绿色,绿色,_,_,_,_),房子),成员(房子(白色,白色,_,_,_,_),房子),on_the_left(绿色,白色,N),%-- 提示 4成员(房子(_,_,丹麦,_,茶,_),房子),%-- 提示 5 - next_to成员(房子(灯光,_,_,_,_,灯光),房子),成员(房子(猫,_,_,猫,_,灯),房子),next_to(灯光,猫,N),%-- 提示 6成员(房子(_,黄色,_,_,_,雪茄),房子),%-- 提示 7成员(房子(_,_,德国,_,_,水管),房子),%-- 提示 8成员(房子(3,_,_,_,牛奶,_),房子),%-- 提示 9 - next_to成员(房子(水,_,_,_,水,_),房子),next_to(光,水,N),%-- 提示 10成员(房子(_,_,_,鸟,_,无过滤器),房子),%-- 提示 11成员(房子(_,_,瑞典,狗,_,_),房子),%-- 提示 12 - next_to成员(房屋(挪威,_,挪威,_,_,_),房屋),成员(房子(蓝色,蓝色,_,_,_,_),房子),next_to(挪威,蓝色,N),%-- 提示 13 - next_to成员(房子(马,_,_,马,_,_),房子),next_to(马,绿色,N),%-- 提示 14成员(房子(_,_,_,_,啤酒,薄荷醇),房子),%-- 提示 15成员(房子(_,绿色,_,_,咖啡,_),房子),%-- 最后一个问题 - 谁放了鱼?成员(房子(_,_,_,鱼,_,_),房子),成员(房子(_,_,谁,鱼,_,_),房子).
我尝试了很多组合但是:
<块引用>?- 鱼(谁).
假的.
代码现在可以工作了,我改变了什么:
1* 来自:
%-- 提示 5 - next_to成员(房子(灯光,_,_,_,_,灯光),房子),成员(房子(猫,_,_,猫,_,灯),房子),
收件人:
%-- 提示 5 - next_to成员(房子(灯光,_,_,_,_,灯光),房子),成员(房子(猫,_,_,猫,_,_),房子),
2* 来自:
%-- 提示 13 - next_to成员(房子(马,_,_,马,_,_),房子),next_to(马,绿色,N),
收件人:
%-- 提示 13 - next_to成员(房子(黄色,黄色,_,_,_,_),房子),成员(房子(马,_,_,马,_,_),房子),next_to(马,黄色,N),
如果您正在阅读这篇文章,请查看关于辅助谓词中结构的 @Enigmativity 评论.
你的线索中有两个错误 - 第一个你已经用 light
吸烟者修复了.二是horse
的主人住在yellow
房子旁边,而不是green
.
现在,我的序言卡在 +
运算符上,所以我重新编码了您的辅助谓词.这就是我所做的:
现在这个谜题很好地利用了这些线索:
<上一页>鱼(谁):-房子= [房子(_Color1,_From1,_Animal1,_Drink1,_Smoke1),房子(_Color2,_From2,_Animal2,_Drink2,_Smoke2),房子(_Color3,_From3,_Animal3,_Drink3,_Smoke3),房子(_Color4,_From4,_Animal4,_Drink4,_Smoke4),房子(_Color5,_From5,_Animal5,_Drink5,_Smoke5)],first(house(_, norway, _, _, _), Houses), %-- 提示 1member(house(red, england, _, _, _), Houses), %-- 提示 2on_the_left(房子(绿色,_,_,_,_),房子(白色,_,_,_,_),房子),%--提示 3 - on_the_leftmember(house(_, denmark, _, tea, _), Houses), %-- 提示 4next_to(house(_, _, _, _, light), house(_, _, cat, _, _), Houses), %-- 提示 5 - next_tomember(house(yellow, _, _, _, cigar), Houses), %-- 提示 6成员(房屋(_,德国,_,_,水管),房屋),%--提示 7中间(房子(_,_,_,牛奶,_),房子),%--提示8next_to(house(_, _, _, _, light), house(_, _, _, water, _), Houses), %-- 提示 9 - next_tomember(house(_, _, bird, _, nofilter), Houses), %-- 提示 10member(house(_, sweden, dog, _, _), Houses), %-- 提示 11next_to(house(_, norway, _, _, _), house(blue, _, _, _, _), Houses), %-- 提示 12 - next_tonext_to(house(_, _, horse, _, _), house(yellow, _, _, _, _), Houses), %-- 提示 13 - next_tomember(house(_, _, _, beer, menthol), Houses), %-- 提示 14member(house(green, _, _, coffee, _), Houses), %-- 提示 15成员(房子(_,谁,鱼,_,_),房子),写(房屋),NL.我明白了:
<上一页>[房子(黄色,挪威,猫,水,雪茄),房子(蓝色,丹麦,马,茶,灯),房子(红色,英国,鸟,牛奶,无过滤器),房子(绿色,德国,鱼,咖啡,水烟), 房子(白, 瑞典, 狗, 啤酒, 薄荷)]德国I implemented Einstein Riddle in Prolog and I'm trying to find out who had a fish at home.
I can't find fault in this code and trace option is not helping with this problem ;)
Rules:
- Norwegian lives in first house
- The Englishman lives in a red house.
- The green house is located directly on the left side of the white house.
- Dane drink tea.
- Light smoker lives next to the breeders of cats.
- A resident of the yellow house smokes a cigar.
- German smokes a water-pipe.
- A resident of the center house drinks milk.
- Light smoker has a neighbor who drink the water.
- Smoke cigarettes without filter breeding birds.
- Swede bred dogs.
- The Norwegian lives next to the blue house.
- Breeder of horses lives next to the yellow house.
- Smoke menthol drink beer.
- In the green house they drink coffee.
Here is my code:
on_the_left(X, Y, N) :-
Y is X - 1,
+ Y < 1,
+ X > N.
next_to(X, Y, N) :-
( Y is X + 1;
Y is X - 1),
+ X > N,
+ Y > N,
+ X < 1,
+ Y < 1.
fish(Who) :-
Houses = [
house(1, _Color1, _From1, _Animal1, _Drink1, _Smoke1),
house(2, _Color2, _From2, _Animal2, _Drink2, _Smoke2),
house(3, _Color3, _From3, _Animal3, _Drink3, _Smoke3),
house(4, _Color4, _From4, _Animal4, _Drink4, _Smoke4),
house(5, _Color5, _From5, _Animal5, _Drink5, _Smoke5) ],
N is 5,
%-- hint 1
member(house(1, _, norway, _, _, _), Houses),
%-- hint 2
member(house(_, red, england, _, _, _), Houses),
%-- hint 3 - on_the_left
member(house(GREEN, green, _, _, _, _), Houses),
member(house(WHITE, white, _, _, _, _), Houses),
on_the_left(GREEN, WHITE, N),
%-- hint 4
member(house(_, _, denmark, _, tea, _), Houses),
%-- hint 5 - next_to
member(house(LIGHT, _, _, _, _, light), Houses),
member(house(CAT, _, _, cat, _, light), Houses),
next_to(LIGHT, CAT, N),
%-- hint 6
member(house(_, yellow, _, _, _, cigar), Houses),
%-- hint 7
member(house(_, _, germany, _, _, waterpipe), Houses),
%-- hint 8
member(house(3, _, _, _, milk, _), Houses),
%-- hint 9 - next_to
member(house(WATER, _, _, _, water, _), Houses),
next_to(LIGHT, WATER, N),
%-- hint 10
member(house(_, _, _, bird, _, nofilter), Houses),
%-- hint 11
member(house(_, _, sweden, dog, _, _), Houses),
%-- hint 12 - next_to
member(house(NORWAY, _, norway, _, _, _), Houses),
member(house(BLUE, blue, _, _, _, _), Houses),
next_to(NORWAY, BLUE, N),
%-- hint 13 - next_to
member(house(HORSE, _, _, horse, _, _), Houses),
next_to(HORSE, GREEN, N),
%-- hint 14
member(house(_, _, _, _, beer, menthol), Houses),
%-- hint 15
member(house(_, green, _, _, coffee, _), Houses),
%-- FINAL QUESTION - WHO LET THE FISH OUT?
member(house(_, _, _, fish, _, _), Houses),
member(house(_, _, Who, fish, _, _), Houses).
I tried a lot of combination but:
?- fish(Who).
false.
Edit:
Code is working now, what i changed:
1* From:
%-- hint 5 - next_to
member(house(LIGHT, _, _, _, _, light), Houses),
member(house(CAT, _, _, cat, _, light), Houses),
To:
%-- hint 5 - next_to
member(house(LIGHT, _, _, _, _, light), Houses),
member(house(CAT, _, _, cat, _, _), Houses),
2* From:
%-- hint 13 - next_to
member(house(HORSE, _, _, horse, _, _), Houses),
next_to(HORSE, GREEN, N),
To:
%-- hint 13 - next_to
member(house(YELLOW, yellow, _, _, _, _), Houses),
member(house(HORSE, _, _, horse, _, _), Houses),
next_to(HORSE, YELLOW, N),
If you are reading this look at @Enigmativity comment about structures in helper predicates aswell.
You had two errors in your clues - the first you fixed already with the light
smoker. The second is that the horse
owner lives next to the yellow
house, not the green
.
Now, my prolog choked on the +
operator so I recoded your helper predicates. This is what I did:
first(H,[H|_]). on_the_left(X,Y,[X,Y|_]). on_the_left(X,Y,[_|Hs]) :- on_the_left(X,Y,Hs). next_to(X,Y,[X,Y|_]). next_to(X,Y,[Y,X|_]). next_to(X,Y,[_|Hs]) :- next_to(X,Y,Hs). middle(X,[_,_,X,_,_]).
Now the puzzle worked nicely with these clues:
fish(Who) :- Houses = [ house(_Color1, _From1, _Animal1, _Drink1, _Smoke1), house(_Color2, _From2, _Animal2, _Drink2, _Smoke2), house(_Color3, _From3, _Animal3, _Drink3, _Smoke3), house(_Color4, _From4, _Animal4, _Drink4, _Smoke4), house(_Color5, _From5, _Animal5, _Drink5, _Smoke5) ], first(house(_, norway, _, _, _), Houses), %-- hint 1 member(house(red, england, _, _, _), Houses), %-- hint 2 on_the_left(house(green, _, _, _, _), house(white, _, _, _, _), Houses), %-- hint 3 - on_the_left member(house(_, denmark, _, tea, _), Houses), %-- hint 4 next_to(house(_, _, _, _, light), house( _, _, cat, _, _), Houses), %-- hint 5 - next_to member(house(yellow, _, _, _, cigar), Houses), %-- hint 6 member(house(_, germany, _, _, waterpipe), Houses), %-- hint 7 middle(house(_, _, _, milk, _), Houses), %-- hint 8 next_to(house(_, _, _, _, light), house(_, _, _, water, _), Houses), %-- hint 9 - next_to member(house(_, _, bird, _, nofilter), Houses), %-- hint 10 member(house(_, sweden, dog, _, _), Houses), %-- hint 11 next_to(house(_, norway, _, _, _), house(blue, _, _, _, _), Houses), %-- hint 12 - next_to next_to(house(_, _, horse, _, _), house(yellow, _, _, _, _), Houses), %-- hint 13 - next_to member(house(_, _, _, beer, menthol), Houses), %-- hint 14 member(house(green, _, _, coffee, _), Houses), %-- hint 15 member(house(_, Who, fish, _, _), Houses), write(Houses), nl.
I got:
[house(yellow, norway, cat, water, cigar), house(blue, denmark, horse, tea, light), house(red, england, bird, milk, nofilter), house(green, germany, fish, coffee, waterpipe), house(white, sweden, dog, beer, menthol)] germany
这篇关于爱因斯坦谜语与术语列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!