爱因斯坦之谜 [英] Einstein's riddle

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

问题描述

我对Prolog并不陌生,我正在尝试在Prolog中建模一个像爱因斯坦之谜(也称为Zebra谜语)之类的谜语(但有10个房屋和30条提示),并且我正在使用此模型示例作为起点:

I am new to Prolog and I'm trying to model a riddle like the Einstein riddle, also known as the Zebra riddle, (but with 10 houses and 30 hints) in Prolog and I'm using this model example as a starting point:

http://www.baptiste-wicht .com/2010/09/solve-einsteins-riddle-using-prolog/

但是在我的谜语中,我必须能够说X是Y的权利.我并不是说直接对,而是列表中的权利.因此,Y可以在1号房中,而X可以在9号房中.

But in my riddle, i have to be able to say that X is right of Y. And I don't mean directly right, but right of in the list. So Y can be in house 1, while X in house 9.

如何在Prolog中做到这一点?我在考虑一个谓词,该谓词表示Y是通过将X和X之前的所有元素都从列表中取出而构成的列表的成员,但是我不确定如何做到这一点或使之适合我前面提到的示例.

How can I do this in Prolog? I was thinking about a predicate which says that Y is a member of the list constructed by taking X and all elements before X out of that list, but I'm not sure how to do that or make that so that I can fit it in the before mentioned example.

推荐答案

right(X,Y):-right(X,Z),right(Z,Y)

right(X,Y) :- right(X,Z),right(Z,Y).

因此,您只需要使用一个传递闭包来定义正确的谓词,然后定义彼此直接相邻的元素.

Hence, you just use a transitive closure to define the right predicate and then define the elements that are directly next to each other.

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

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