学习序言:解决填字游戏方案 [英] Learning Prolog: solving a crossword scheme

查看:126
本文介绍了学习序言:解决填字游戏方案的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试根据

I'm trying to learn Prolog following the tutorials on this site and I can't find a solution to an exercise (and there's no solution on the site).

这就是我要做的:

这里有六个意大利语单词:

Here are six Italian words:

阿斯塔特,阿斯托里亚,巴拉托,钴,手枪,Statale.

astante , astoria , baratto , cobalto , pistola , statale .

以填字游戏的方式,将它们排列在以下网格中:

They are to be arranged, crossword puzzle fashion, in the following grid:

以下知识库表示包含以下单词的词典:

The following knowledge base represents a lexicon containing these words:

  • word(astante,a,s,t,a,n,t,e).
  • 单词(astoria,a,s,t,o,r,i,a).
  • word(baratto,b,a,r,a,t,t,o).
  • word(钴,c,o,b,a,l,t,o).
  • word(pistola,p,i,s,t,o,l,a).
  • word(状态,s,t,a,t,a,l,e).
  • word(astante, a,s,t,a,n,t,e).
  • word(astoria, a,s,t,o,r,i,a).
  • word(baratto, b,a,r,a,t,t,o).
  • word(cobalto, c,o,b,a,l,t,o).
  • word(pistola, p,i,s,t,o,l,a).
  • word(statale, s,t,a,t,a,l,e).

写一个谓词填字游戏/6,它告诉我们如何填充网格.前三个参数应该是从左到右的垂直词,后三个参数应该是从上到下的水平词.

Write a predicate crossword/6 that tells us how to fill in the grid. The first three arguments should be the vertical words from left to right, and the last three arguments the horizontal words from top to bottom.

现在,已经提出了相同的问题,但是每个给定的解决方案都使用了我不知道(我不应该知道解决这个问题).

Now, the same question has been asked there but each given solution uses things that I don't know (and I'm not supposed to know to solve this).

为澄清起见,虽然所链接的问题确实可以正常工作,但它们使用的是我遵循的指南中尚未说明的内容,这意味着我需要解决练习没有使用那种东西,所以没有maplist之类的东西.

To clarify, while the things in the linked question are surely working, they use stuff that hasn't been explained yet in the guide that I'm following, and this means that I need to solve the exercise without using that kind stuff, so no maplist and things like that.

我的想法是用给定单词的字母填充董事会,但有一些限制:

My idea was to fill the board with the letters from the given words, with some constraints:

  • V1中的单词的第二个字符必须与H1中的单词的第二个字符一样
  • V1中的单词的第二个字符必须为H2中的第二个字符
  • V1中的单词的第六个字符必须为H3中的第二个字符

以此类推.

这是我的代码:

word(astante,  a,s,t,a,n,t,e).
word(astoria,  a,s,t,o,r,i,a).
word(baratto,  b,a,r,a,t,t,o).
word(cobalto,  c,o,b,a,l,t,o).
word(pistola,  p,i,s,t,o,l,a).
word(statale,  s,t,a,t,a,l,e). 

crossword(V1,V2,V3,H1,H2,H3):- word(V1, V11,V12,V13,V14,V15,V16,V17),
                               word(H1, H11,V12,H13,H14,H15,H16,H17),
                               word(H2, H21,V14,H23,H24,H25,H26,H27),
                               word(H3, H31,V16,H33,H34,H35,H36,H37),

                               word(V2, V21,V22,V23,V24,V25,V26,V27),
                               word(H1, H11,H12,H13,V22,H15,H16,H17),
                               word(H2, H21,H22,H23,V24,H25,H26,H27),
                               word(H3, H31,H32,H33,V26,H35,H36,H37),

                               word(V3, V31,V32,V33,V34,V35,V36,V37),
                               word(H1, H11,H12,H13,H14,H15,V32,H17),
                               word(H2, H21,H22,H23,H24,H25,V34,H27),
                               word(H3, H31,H23,H33,H34,H35,V36,H37). 

(很抱歉,如果格式不正确,但我仍然不知道Prolog的正确缩进样式是什么).

(I'm sorry if this is not formatted well but I still don't know what's the correct indentation style for Prolog).

当然,如果我的想法(至少对我而言)是正确的,则此代码返回No,我不知道为什么.

Of course, event if my idea seems correct (at least to me), this code returns No and I don't know why.

对此有任何提示吗?

在@mbratch的评论之后,我尝试使用在

Following @mbratch's comment, I've tried using the code found in this solution.

代码如下:

crossword(V1, V2, V3, H1, H2, H3) :-
   word(V1, V1a, V1bH1b, V1c, V1dH2b, V1e, V1fH3b, V1g), 
   word(V2, V2a, V2bH1d, V2c, V2dH2d, V2e, V2fH3d, V2g), 
   word(V3, V3a, V3bH1f, V3c, V3dH2f, V3e, V3fH3f, V3g), 
   word(H1, H1a, V1bH1b, H1c, V2bH1d, H1e, V3bH1f, H1g), 
   word(H2, H2a, V1dH2b, H2c, V2dH2d, H2e, V3dH2f, H2g), 
   word(H3, H3a, V1fH3b, H3c, V2fH3d, H3e, V3fH3f, H3g).

该代码有效,但是我不介意重复出现问题.

The code works, but it has a problem with duplicates which I don't mind.

我想理解的是为什么,而我的返回No时,这一方法仍然有效.

What I'd like to understand is why this one works while mine returns No.

推荐答案

经过几次尝试,并基于@ joel76的注释,我注意到我的第一个代码是错误的,因为我声明了H1H2H3多次,因此第二行中计算出的结果在第六行中被更改,这导致Prolog返回No.

After few tries, and based on @joel76's comment, I noticed that my first code was wrong because I declared H1,H2 and H3 multiple times, so the result computed in the second line was being changed in the sixth and this led to the No returned by Prolog.

因此,我没有将其合并为多个行,而是将它们与以下结果合并:

So, instead of doing things in multiple lines, I merged them with this result:

crossword(V1,V2,V3,H1,H2,H3):- word(V1, V11, V12, V13, V14, V15, V16, V17),
                               word(V2, V21, V22, V23, V24, V25, V26, V27),
                               word(V3, V31, V32, V33, V34, V35, V36, V37),
                               word(H1, H11, V12, H13, V22, H15, V32, H17),
                               word(H2, H21, V14, H23, V24, H25, V34, H27),
                               word(H3, H31, V16, H33, V26, H35, V36, H37).

现在可以正常工作了.

这篇关于学习序言:解决填字游戏方案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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