Einstein's riddle

坚强是说给别人听的谎言 提交于 2019-12-24 01:39:26

问题


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/

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.

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.


回答1:


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.



来源:https://stackoverflow.com/questions/4428311/einsteins-riddle

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!