我正在使用prolog来解决一个kakuro难题。我有一个这样的规则列表,其中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).
但这一切都是假的。
如有任何帮助,我们将不胜感激。
因此,首先需要创建矩阵,它最终将成为kakuro谜题的网格。这绝对是第一步-如果你已经做到了,请道歉。
然后,您需要为交叉约束制定一些规则:
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是一个棘手的问题——在这里,你想抓住你想要的正方形,然后从这一点开始列出一个列表。提示:使用append和Hint。
祝你好运!