列表中的唯一元素 (Prolog)

发布于 2024-10-07 04:52:28 字数 1224 浏览 10 评论 0原文

我正在实施爱因斯坦之谜的变体,但遇到了一些麻烦。

当尝试计算解决方案时,我尝试这样做:

solve(Street) :- Street = [_House1,_House2,_House3,_House4,_House5],
%hint one goes here
%hint two goes here
%etc.

然后我可以通过键入以下内容来询问解决方案:solve(Street)

但是,这作为解决方案出现:

  1. house(flower, food, pet, sport)
  2. house(flower, food, pet, sport)
  3. house(x , food, pet, sport)
  4. house(flower, food, pet, sport)
  5. 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:

  1. house(flower, food, pet, sport)
  2. house(flower, food, pet, sport)
  3. house(x , food, pet, sport)
  4. house(flower, food, pet, sport)
  5. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

GRAY°灰色天空 2024-10-14 04:52:28

为了表达没有花被报告两次,并且为了确保所有花都被绑定,可以使用 permutation/2 谓词:所有花的列表应该是指定花列表的排列。这看起来像[未经测试]

flowers([], []).
flowers([house(Flower,_,_,_)|Street], [Flower|Rest]) :- flowers(Street, Rest).

-- ...
   flowers(Street, Flowers), 
   permutation(Flowers, [kaktus, tulpe, nelke, rose, fingerhut]),

编辑:对于 10 朵花,使用排列可能太慢。另一种方法是

flower(kaktus).
flower(tulpe).
flower(nelke).
--...

       flowers(Street,[F1,F2,F3,F4,F5,F6,F7,F8,F9,F10]),
       flower(F1), flower(F2), F1\=F2,
       flower(F3), F3\=F1, F3\=F2,
       flower(F4), F4\=F1, F4\=F2, F4\=F3,
       --...

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]

flowers([], []).
flowers([house(Flower,_,_,_)|Street], [Flower|Rest]) :- flowers(Street, Rest).

-- ...
   flowers(Street, Flowers), 
   permutation(Flowers, [kaktus, tulpe, nelke, rose, fingerhut]),

Edit: for 10 flowers, using permutations is probably too slow. An alternative approach is

flower(kaktus).
flower(tulpe).
flower(nelke).
--...

       flowers(Street,[F1,F2,F3,F4,F5,F6,F7,F8,F9,F10]),
       flower(F1), flower(F2), F1\=F2,
       flower(F3), F3\=F1, F3\=F2,
       flower(F4), F4\=F1, F4\=F2, F4\=F3,
       --...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文