序言谜语解决 [英] Prolog riddle solving

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

问题描述

声明:

总共四对

参加了化妆舞会.

2

打扮成猫的那位女士

与丈夫马特(Matt)抵达.

3

已经有两对了,

一个男人穿得像熊一样.

4

第一个到达不是Vince,

但是他在王子面前到达那里.

5

女巫(不是苏)嫁给了查克,

谁打扮成唐老鸭.

6

玛丽是在娄之后进来的,

两个人都在苏之前在那儿

7

吉普赛人在安(Ann)之前到达,

与蝙蝠侠结婚也不会.

8

如果白雪公主在苔丝之后到达,

那每对夫妇穿得怎么样?

我的代码在这里,但返回false:

sol(S):-
    S=[[1,L1,M1,LD1,MD1],
        [2,L2,M2,LD2,MD2],
        [3,L3,M3,LD3,MD3],
        [4,L4,M4,LD4,MD4]],
    member([_,_,matt,cat,_],S),
    member([ALR,_,_,_,bear],S),
    (ALR =:= 1 ; ALR =:= 2),
    not(member([1,_,vince,_,_],S)),
    member([VN,_,vince,_,_],S),
    member([PS,_,_,_,prince],S),
    VN < PS ,
    member([_,_,chuck,witch,donald],S),
    not(member([_,sue,_,witch,_],S)),
    member([MRY,mary,_,_,_],S),
    member([LOU,_,lou,_,_],S),
    member([SUE,sue,_,_,_],S),
    MRY > LOU,
    MRY < SUE,
    member([GPS,_,_,gipsy,_],S),
    member([ANN,ann,_,_,_],S),
    GPS < ANN ,
    not(member([_,_,_,gipsy,batman],S)),
    not(member([_,ann,_,_,batman],S)),
    member([SW,_,_,snowwhite,_],S),
    member([TS,tess,_,_,_],S),
    SW > TS ,
    perm([sue,mary,ann,tess],[L1,L2,L3,L4]),
    perm([matt,lou,vince,chuck],[M1,M2,M3,M4]),
    perm([cat,witch,gipsy,snowwhite],[LD1,LD2,LD3,LD4]),
    perm([donald,prince,batman,bear],[MD1,MD2,MD3,MD4]).




takeout(X,[X|R],R).
takeout(X,[F|R],[F|S]) :- takeout(X,R,S).

perm([],[]).
perm([X|Y],Z) :- perm(Y,W), takeout(X,Z,W).

任何解决方案吗?

解决方案

您应将所有not(...)目标移到谓词的最后.

not(G)的意思是"G现在不可能满足 ".如果过早尝试,但列表中仍有许多未实例化的变量,实际上可能可能满足目标,而整个not(...)调用将立即失败.

或者,延迟对变量不等式的检查,直到实例化它为止,例如.在SWI Prolog中使用 freeze/2 (例如此答案).

The statement :

Four couples in all

Attended a costume ball.

2

The lady dressed as a cat

Arrived with her husband Matt.

3

Two couples were already there,

One man dressed like a bear.

4

First to arrive wasn't Vince,

But he got there before the Prince.

5

The witch (not Sue) is married to Chuck,

Who was dressed as Donald Duck.

6

Mary came in after Lou,

Both were there before Sue.

7

The Gipsy arrived before Ann,

Neither is wed to Batman.

8

If Snow White arrived after Tess,

Then how was each couple dressed?

My code is here , but it returns false :

sol(S):-
    S=[[1,L1,M1,LD1,MD1],
        [2,L2,M2,LD2,MD2],
        [3,L3,M3,LD3,MD3],
        [4,L4,M4,LD4,MD4]],
    member([_,_,matt,cat,_],S),
    member([ALR,_,_,_,bear],S),
    (ALR =:= 1 ; ALR =:= 2),
    not(member([1,_,vince,_,_],S)),
    member([VN,_,vince,_,_],S),
    member([PS,_,_,_,prince],S),
    VN < PS ,
    member([_,_,chuck,witch,donald],S),
    not(member([_,sue,_,witch,_],S)),
    member([MRY,mary,_,_,_],S),
    member([LOU,_,lou,_,_],S),
    member([SUE,sue,_,_,_],S),
    MRY > LOU,
    MRY < SUE,
    member([GPS,_,_,gipsy,_],S),
    member([ANN,ann,_,_,_],S),
    GPS < ANN ,
    not(member([_,_,_,gipsy,batman],S)),
    not(member([_,ann,_,_,batman],S)),
    member([SW,_,_,snowwhite,_],S),
    member([TS,tess,_,_,_],S),
    SW > TS ,
    perm([sue,mary,ann,tess],[L1,L2,L3,L4]),
    perm([matt,lou,vince,chuck],[M1,M2,M3,M4]),
    perm([cat,witch,gipsy,snowwhite],[LD1,LD2,LD3,LD4]),
    perm([donald,prince,batman,bear],[MD1,MD2,MD3,MD4]).




takeout(X,[X|R],R).
takeout(X,[F|R],[F|S]) :- takeout(X,R,S).

perm([],[]).
perm([X|Y],Z) :- perm(Y,W), takeout(X,Z,W).

Any solution ?

解决方案

You should move all your not(...) goals to the very end of the predicate.

not(G) means, "G is impossible to satisfy right now". When tried too early, with many still non-instantiated variables in the lists, it is in fact very often possible to satisfy a goal, and the whole not(...) call will fail right away.

Alternatively, delay the checking of the inequality on a variable until it is instantiated, e.g. in SWI Prolog with freeze/2 (as seen e.g. in this answer).

这篇关于序言谜语解决的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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