列表中的唯一元素 (Prolog)
我正在实施爱因斯坦之谜的变体,但遇到了一些麻烦。
当尝试计算解决方案时,我尝试这样做:
solve(Street) :- Street = [_House1,_House2,_House3,_House4,_House5],
%hint one goes here
%hint two goes here
%etc.
然后我可以通过键入以下内容来询问解决方案:solve(Street)
但是,这作为解决方案出现:
- house(flower, food, pet, sport)
- house(flower, food, pet, sport)
- house(x , food, pet, sport)
- house(flower, food, pet, sport)
- house(x,flower, pet, sport)
正如你所看到的,有 2 个次x,剩下的就是各类食物、鲜花、宠物和运动。 但每种类型都是独一无二的:如果一个人喜欢花 X,那么其他人就不会喜欢 X。
现在,我的解决方案给出 2 x 的原因很容易看出:我们给出了大量提示,但在所有提示中只提到了4朵花。所以 Prolog 不知道还有另一朵花,只是使用 x 两次,只是因为它是可能的并且满足所有其他提示。
我想说的是,街上所有的食物和鲜花等类型都是独一无二的,所以当他已经使用了所有类型时,应该留一些空白。 3 看起来像:house(x , food, pet ,sport)
,5 看起来像:house(_,flower, pet, sport)
。
我还尝试将其添加到提示中:(假设“仙人掌”是提示中未提及的花朵之一) member(house(cactus,_,_,_), Street)
但是我的程序并没有结束......
提示可能如下所示: is_neighbour(房子(_,_,_,足球),房子(_,_,鱼,_),街道),
其中:当 A 和 B 在 List
中彼此相邻时,is_neighbour(A,B,List)
给出 true
。 这个暗示可以翻译为:热爱足球的人住在有鱼的人旁边。
如果需要提供更多信息,我愿意详细说明。 :)
I'm implementing a variation on Einstein's Riddle and i'm having some trouble.
When trying to calculate the solution i try this:
solve(Street) :- Street = [_House1,_House2,_House3,_House4,_House5],
%hint one goes here
%hint two goes here
%etc.
I can then ask the solution by typing: solve(Street)
However this comes up as solution:
- house(flower, food, pet, sport)
- house(flower, food, pet, sport)
- house(x , food, pet, sport)
- house(flower, food, pet, sport)
- house(x, flower, pet, sport)
As you can see there's 2 times x, the rest are all types of foods, flowers, pets and sports.
But every type is unique: if one person likes flower X, noone else can like X.
Now, the reason why my solution gives 2 x's is easy to see: we are given an amount of hints but in all the hints there are only mentioned 4 flowers. So Prolog doesn't know there is another flower, and just uses x twice, just because it's possible and fulfills all the other hints.
What i want to say is that all the types of foods and flowers etc. in Street are unique so he should leave some blank when he used all types already. 3 would look like: house(x , food, pet ,sport)
and 5 would look like: house(_, flower, pet, sport)
.
I also tried adding this to the hints: (let's say "cactus" is one of the flowers not mentioned in the hints)member(house(cactus,_,_,_), Street)
However then my program doesn't end...
A hint may look like this:is_neighbour(house(_,_,_,football),house(_,_,fish,_), Street),
with : is_neighbour(A,B,List)
giving true
when A and B are next to each other in List
.
The hint can be translated to: the person who loves football lives next to the person who has fish.
If any more info need to be provided i'm willing to elaborate. :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为了表达没有花被报告两次,并且为了确保所有花都被绑定,可以使用 permutation/2 谓词:所有花的列表应该是指定花列表的排列。这看起来像[未经测试]
编辑:对于 10 朵花,使用排列可能太慢。另一种方法是
To express that no flower is reported twice, and also to make sure that all flowers are bound, you can use the permutation/2 predicate: the list of all flowers should be a permutation of the list of specified flowers. This would read like [untested]
Edit: for 10 flowers, using permutations is probably too slow. An alternative approach is