【发布时间】:2013-12-09 00:24:48
【问题描述】:
作为 prolog 新手,我想尝试实现一个二进制数独求解器。(代码如下,swi-prolog)。二进制数独在这里解释:https://cstheory.stackexchange.com/questions/16982/how-hard-is-binary-sudoku-puzzle
但是,在执行以下查询时:
binarySudoku([[1,0],[0,1]]). I get "true."
binarySudoku([[1,_],[_,_]]). I get "false."
现在显然它不应该返回 false,因为有解决方案......为什么会发生这种情况/我该如何解决这个问题?
:-use_module(library(clpfd)).
validRow(Row) :-
Row ins 0..1,
length(Row,L),
sum(Row,#=,L/2).
matrixNth(Matr,X,Y,El) :-
nth1(Y,Matr,CurRow),
nth1(X,CurRow,El).
allDifferent([]).
allDifferent([X|Y]) :-
not(member(X,Y)),
allDifferent(Y).
invalid(Rows,X,Y) :-
AboveY is Y-1,
BelowY is Y+1,
matrixNth(Rows,X,Y,1),
matrixNth(Rows,X,AboveY,1),
matrixNth(Rows,X,BelowY,1).
invalid(Rows,X,Y) :-
LeftX is X-1,
RightX is X+1,
matrixNth(Rows,X,Y,1),
matrixNth(Rows,LeftX,Y,1),
matrixNth(Rows,RightX,Y,1).
binarySudoku(Rows) :-
length(Rows,Height),
transpose(Rows,Cols),
length(Cols,Height),
maplist(validRow,Rows),
foreach(between(1,Height,X),foreach(between(1,Height,Y),not(invalid(Rows,X,Y)))),
allDifferent(Rows).
【问题讨论】:
-
label(Row) 似乎可以解决它,但为什么会这样呢?在 clpfd 手册中的示例数独求解器中,从未调用过标签(可能隐含在 all_distinct 中?)
-
我不会称它为拉丁方格,因为它不仅有不同的约束,当你去更大的宽度时,它甚至不像一个......另外,输入“二进制数独”进入谷歌发现我正在描述的同样的谜题。
标签: prolog swi-prolog sudoku clpfd