Prolog为数独求解器返回false

时间:2013-05-06 16:15:38

标签: prolog sudoku

:-use_module(library(clpfd)).

solve(X,Board):-

    Board=X,
    Board =
    [A1,A2,A3,A4,A5,A6,A7,A8,A9,
     B1,B2,B3,B4,B5,B6,B7,B8,B9,
     C1,C2,C3,C4,C5,C6,C7,C8,C9,
     D1,D2,D3,D4,D5,D6,D7,D8,D9,
     E1,E2,E3,E4,E5,E6,E7,E8,E9,
     F1,F2,F3,F4,F5,F6,F7,F8,F9,
     G1,G2,G3,G4,G5,G6,G7,G8,G9,
     H1,H2,H3,H4,H5,H6,H7,H8,H9,
     I1,I2,I3,I4,I5,I6,I7,I8,I9],

    Board ins 1..9,
    label(Board),

    %rows on the board

    all_different([A1,A2,A3,A4,A5,A6,A7,A8,A9]),
    all_different([B1,B2,B3,B4,B5,B6,B7,B8,B9]),
    all_different([C1,C2,C3,C4,C5,C6,C7,C8,C9]),
    all_different([D1,D2,D3,D4,D5,D6,D7,D8,D9]),
    all_different([E1,E2,E3,E4,E5,E6,E7,E8,E9]),
    all_different([F1,F2,F3,F4,F5,F6,F7,F8,F9]),
    all_different([G1,G2,G3,G4,G5,G6,G7,G8,G9]),
    all_different([H1,H2,H3,H4,H5,H6,H7,H8,H9]),
    all_different([I1,I2,I3,I4,I5,I6,I7,I8,I9]),

    %columns on the board

    all_different([A1,B1,C1,D1,E1,F1,G1,H1,I1]),
    all_different([A2,B2,C2,D2,E2,F2,G2,H2,I2]),
    all_different([A3,B3,C3,D3,E3,F3,G3,H3,I3]),
    all_different([A4,B4,C4,D4,E4,F4,G4,H4,I4]),
    all_different([A5,B5,C5,D5,E5,F5,G5,H5,I5]),
    all_different([A6,B6,C6,D6,E6,F6,G6,H6,I6]),
    all_different([A7,B7,C7,D7,E7,F7,G7,H7,I7]),
    all_different([A8,B8,C8,D8,E8,F8,G8,H8,I8]),
    all_different([A9,B9,C9,D9,E9,F9,G9,H9,I9]),

    %different boxes

    all_different([A1,A2,A3,B1,B2,B3,C1,C2,C3]),
    all_different([A4,A5,A6,B4,B5,B6,C4,C5,C6]),
    all_different([A7,A8,A9,B7,B8,B9,C7,C8,C9]),
    all_different([D1,D2,D3,E1,E2,E3,F1,F2,F3]),
    all_different([D4,D5,D6,E4,E5,E6,F4,F5,F6]),
    all_different([D7,D8,D9,E7,E8,E9,F7,F8,F9]),
    all_different([G1,G2,G3,H1,H2,H3,I1,I2,I3]),
    all_different([G4,G5,G6,H4,H5,H6,I4,I5,I6]),
    all_different([G7,G8,G9,H7,H8,H9,I7,I8,I9]).

37? - 解决(解决方案,[的,3,,8,5,,1,, 2,,5,,7,,5,,7,的, 4,,1,的,如图9所示,,5,,7,3,,2,,1, ,4,,_,9])。 假的。

嗯,下面有一个代码的超链接来比较我的,我自己比较,除了我的9x9和他的4x4(我使用ins而不是fd_domain)的事实我可以看到没有区别...我的问题是,这可能是假的,因为我使用的是ins而不是fd_domain,还是我看不到的其他内容?

Some code which I compared my own to

1 个答案:

答案 0 :(得分:2)

通常,标签/ 1在所有约束发布后称为。那是因为它必须标记每个变量,即在修剪后枚举剩余的值。但是你的主要问题是,你传递了一个错误的板,有90个元素,而不是81个,应该是这样。

我可以通过这个奇怪的'查询'向您展示:

?- atomic_list_concat(L, ',', ',,,,,_,,,_,,,_,,,3,,8,5,,_,1,,2,,_,,,_,,,5,,7,,_,,,_,,5,,7,,,_,,,4,,,_,1,,,_,9,,,_,,,_,,5,,_,,,_,,7,3,,_,2,,1,,_,,,_,,,_,4,,,_,9'),findall(Y,(between(1,10,_),length(Y,9)),G), append(G,L),maplist(writeln,G).
[,,,,,_,,,_]
[,,_,,,3,,8,5]
[,_,1,,2,,_,,]
[_,,,5,,7,,_,]
[,_,,5,,7,,,_]
[,,4,,,_,1,,]
[_,9,,,_,,,_,]
[5,,_,,,_,,7,3]
[,_,2,,1,,_,,]
[_,,,_,4,,,_,9]
L = ['', '', '', '', '', '_', '', '', '_'|...],
G = [['', '', '', '', '', '_', '', ''|...], ['', '', '_', '', '', '3', ''|...], ['', '_', '1', '', '2', ''|...], ['_', '', '', '5', ''|...], ['', '_', '', '5'|...], ['', '', '4'|...], ['_', '9'|...], ['5'|...], [...|...]|...].

尝试以这种方式调用您的程序(只是一个示例,以显示正确的fomatting):

?- solve([
   /* 1,2,3,4,5,6,7,8,9 */
      _,_,2,_,_,_,_,_,_,
      1,_,_,_,8,_,_,_,_,
      ...
   /* there must be 9 rows */
], X).