Logic Dots puzzle problems with SWI-Prolog

霸气de小男生 提交于 2019-12-23 03:39:14

问题


I am working on a logic game named "Logic Dots". The game is a 3*3 grid with numbers surrounding each row and column. The rule is easy that the sum of each row and column should equal to those numbers. Below is my code, it is changed from clpfd sokoku. But result is something wrong.

Code:

:- use_module(library(clpfd)).

kakuro(Rows) :-
    length(Rows, 3), maplist(length_list(3), Rows),
    append(Rows, Vs), Vs ins 0..1,
    Rows = [A,B,C],
    sum(A,#=,1),
    sum(B,#=,1),
    sum(C,#=,1),
    transpose(Rows, Columns),
    append(Columns, V), V ins 0..1,
    Columns = [D,E,F],
    sum(D,#=,1),
    sum(E,#=,1),
    sum(F,#=,1),
    Rows = [A,B,C].     

length_list(L,Ls) :- length(Ls,L).

problem([1,0,0,1,0,0], [[_,_,_],
                        [_,_,_],
                        [_,_,_]]).

Result:

?- problem([1,0,0,1,0,0], Rows), kakuro(Rows), maplist(writeln, Rows).
[_G670,_G673,_G676]
[_G682,_G685,_G688]
[_G694,_G697,_G700]
Rows = [[_G4520, _G4523, _G4526], [_G4532, _G4535, _G4538], [_G4544, _G4547, _G4550]],
_G4520 in 0..1,
_G4520+_G4532+_G4544#=1,
_G4520+_G4523+_G4526#=1,
_G4532 in 0..1,
_G4532+_G4535+_G4538#=1,
_G4535 in 0..1,
_G4523+_G4535+_G4547#=1,
_G4523 in 0..1,
_G4526 in 0..1,
_G4526+_G4538+_G4550#=1,
_G4538 in 0..1,
_G4550 in 0..1,
_G4544+_G4547+_G4550#=1,
_G4544 in 0..1,
_G4547 in 0..1.

But when I replace my puzzle with [[_,0,0],[0,_,0],[0,0,_]], the result is correct:

?- problem([1,0,0,1,0,0], Rows), kakuro(Rows), maplist(writeln, Rows).
[1,0,0]
[0,1,0]
[0,0,1]
Rows = [[1, 0, 0], [0, 1, 0], [0, 0, 1]].

But I need grid to be unknown (symbol "_"). Where is the mistake?

I changed Rows = [A,B,C] of last line in kakuro/1 to label(Vs). It works, but only one result shows:

[0,0,1]
[0,1,0]
[1,0,0]

There should be another:

[1,0,0]
[0,1,0]
[0,0,1]

What is wrong with it here?

来源:https://stackoverflow.com/questions/28270558/logic-dots-puzzle-problems-with-swi-prolog

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!