解决序言中的难题 - 生成带约束的解决方案?

Solving a puzzle in prolog - generating a solution with constraints?

我正在使用 prolog 尝试解决数谜难题。我有一个这样的规则列表,其中 R 和 C 代表每个框的行和起点,L 代表长度,S 是行中数字的总和,

 % across(R, C, L, S)
 across(2,4,2,4).
 across(2,10,2,4).  
 across(3,4,4,12).
 across(3,10,2,6).
 across(4,3,2,6).

据我所知,要解决使用约束的难题,对于每个元素 L,我必须找到 1 到 9 之间的不同数字,这些数字加起来等于 S。我真的很挣扎为了解决这个问题,我目前的代码是这样的:

solveAcross(Solution) :-
    findall([R,C,L,S], across(R,C,L,S), List), 
    Solution = length(List, L),
    Solution ins 1..9,
    all_distinct(Solution),
    labeling([], Solution).

但这一切都是return错误的。

如有任何帮助,我们将不胜感激。

所以首先你需要创建你的矩阵,它最终将成为你的数谜的网格。这绝对是第一步 - 如果您已经做到了,我们深表歉意。

那么你想为交叉约束制定一些规则:

acrossConstraints(Matrix) :- getAcross(List), applyAcross(Matrix,List).
%this is the overall thing to call, containing two predicates you need to make
getAcross :- findall([R,C,L,S],across(R,C,L,S),List).
%creates a list of the across facts
applyAcross(Matrix,[[R,C,L,S]|T]) :- acrossConstraints(R,C,L,S,M), applyAcross(Matrix,T). 
%call another rule and recurse over the list.

acrossConstraints 是一个棘手的问题 - 在这里你想要抓住你想要的方块,然后从那个点开始制作一个列表。提示:使用追加和提示。

祝你好运!