toplevel:未定义的程序Prolog

时间:2013-05-05 23:15:39

标签: prolog sudoku

独(X,板): -

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],

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

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

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

以上是解决/ 2 5? - 求解(X,Y)。 错误:顶级:未定义的程序:solve / 2(DWIM无法纠正目标)

当我尝试使用2个普通变量

时发生了这种情况

2? - 解决(X,[,7,2,4,,_,,1,,8,, 7,,3,2,,6,3,1,,_,,7,,_,,< / em>的,_,5,2,,1,4-,,5,9,,4,6-,,_ ,8,4,,3,7-,,_,的,如图9所示,,_ ,2,5,3,,6,8 ,_,5,,7,,2,,_,,9,4,6,])。 错误:顶级:未定义的程序:solve / 2(DWIM无法纠正目标)

当我输入的第二个变量是一个带有一些固定数字的列表

时,也发生了同样的情况

老实说,我不知道我做错了什么,当没有任何工作时,想到可能的答案真的很令人沮丧 有人能告诉我我做错了吗?

2 个答案:

答案 0 :(得分:3)

你可能意味着

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,
     ...

但我认为你的代码工作太慢......

答案 1 :(得分:2)

错误表示solve/2未定义。

我在您提供的代码中看不到solve/2的定义。这是你的所有代码吗?

solve/2不是特殊或内置谓词,如果你调用它,你必须定义它。