我怎样才能改变数独的大小

时间:2016-01-04 17:35:23

标签: prolog gnu-prolog

我有一个脚本来解决大小= 9 * 9的数独 我有81个变量,我为它们定义了规则,

如何更改此代码以解决任何大小的数独? 例如,对于数独16 * 16,规则将适用于4 * 4的子方格。

go(L) :- L=[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], fd_domain(L,1,9),

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

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

    fd_alldifferent([A1,A2,A3,B1,B2,B3,C1,C2,C3]),
    fd_alldifferent([A4,A5,A6,B4,B5,B6,C4,C5,C6]),
    fd_alldifferent([A7,A8,A9,B7,B8,B9,C7,C8,C9]),

    fd_alldifferent([D1,D2,D3,E1,E2,E3,F1,F2,F3]),
    fd_alldifferent([D4,D5,D6,E4,E5,E6,D4,D5,D6]),
    fd_alldifferent([D7,D8,D9,E7,E8,E9,D7,D8,D9]),
    fd_alldifferent([G1,G2,G3,H1,H2,H3,I1,I2,I3]),
    fd_alldifferent([G4,G5,G6,H4,H5,H6,I4,I5,I6]),
    fd_alldifferent([G7,G8,G9,H7,H8,H9,I7,I8,I9]),
    fd_labeling([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]).

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

我应该写另一个脚本还是我可以改变这个? 谢谢,

1 个答案:

答案 0 :(得分:2)

代码需要重写才能适用于一般情况。它目前对所有维度都是完全硬编码的,所以不能只是进行调整以概括它。

以下是一个示例,向您展示如何使用maplist作为此类问题的工具。它不是解决您问题的完整解决方案,但应该让您入门。

% Auxiliary predicates that will be maplist-friendly (the list is the last argument)
%
length_(N, L) :- length(L, N).
fd_domain_(Min, Max, L) :- fd_domain(L, Min, Max).

constrained_matrix(N, Matrix) :-
    length(Matrix, N),                   % Matrix has N elements
    maplist(length_(N), Matrix),         % Each element of Matrix has N elements
    maplist(fd_domain_(1, N), Matrix),   % The domain of each sublist is 1 to N
    maplist(fd_all_different, Matrix),   % Each sublist must have elements all different
    maplist(fd_labeling, Matrix).

运行如下查询:

| ?- constrained_matrix(3, L).

L = [[1,2,3],[1,2,3],[1,2,3]] ? ;

L = [[1,2,3],[1,2,3],[1,3,2]] ? ;

L = [[1,2,3],[1,2,3],[2,1,3]] ? ;

L = [[1,2,3],[1,2,3],[2,3,1]] ? ;
...

正如您所看到的,解决方案集是所有3x3矩阵,其中包含具有唯一元素的行,但列可以是任何内容。您可以通过编写/使用可以转置矩阵的Prolog谓词(在Matrix中交换行与列)并再次使用maplist来约束列来添加更多约束。您可以根据需要为子方块添加更多约束(例如,编写谓词以提取子矩阵,并充分利用maplist)。