带术语表的爱因斯坦谜语

  时间:2022-05-11 06:48:37  阅读量:16  评论数:0  作者:CryptoNewbie

我在Prolog中实现了爱因斯坦谜语,我试图找出谁家里有一条鱼。 我无法在此代码中找到错误,并且跟踪选项对解决此问题没有帮助;)

规则:

    挪威人住在第一所房子里 那个英国人住在一所红色的房子里。 绿色的房子直接位于白宫的左边。 戴恩喝茶。 轻吸烟者住在猫的饲养者旁边。 黄色房子的一位居民抽着雪茄。 德国人抽水管。 中心住宅的一位居民喝牛奶。 一个吸烟者有个邻居喝水。 吸烟没有过滤嘴的香烟繁殖鸟类。 瑞典人养狗。 挪威人住在蓝房子旁边。 马的饲养员住在黄色房子旁边。 抽薄荷脑,喝啤酒。 他们在温室里喝咖啡。

以下是我的代码:

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

我尝试了很多组合,但是:

?-鱼(谁)。 false.

编辑: 代码现在正在工作,我更改的内容:

1*来自:

%-- hint 5 - next_to
member(house(LIGHT, _, _, _, _, light), Houses),
member(house(CAT, _, _, cat, _, light), Houses),

到:

%-- hint 5 - next_to
    member(house(LIGHT, _, _, _, _, light), Houses),
    member(house(CAT, _, _, cat, _, _), Houses),

2*发件人:

%-- hint 13 - next_to   
member(house(HORSE, _, _, horse, _, _), Houses),
next_to(HORSE, GREEN, N),

到:

%-- hint 13 - next_to
member(house(YELLOW, yellow, _, _, _, _), Houses),
member(house(HORSE, _, _, horse, _, _), Houses),
next_to(HORSE, YELLOW, N),

如果您正在阅读这篇文章,请查看@enigmativity关于帮助谓词中结构的注释。

网友解决方案:

你的线索中有两个错误--第一个你已经用淡烟机解决了。第二种是马主人住在黄房子旁边,而不是绿房子旁边。

现在,我的prolog在\\运算符上阻塞了,所以我重新编码了helper谓词。这是我所做的:

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,_,_]).

现在这个谜题很好地利用了以下线索:

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.

我得到:

[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

原文地址(source):原文链接