我对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:
www.baptiste-wicht /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.
更多推荐
爱因斯坦之谜
发布评论