我想用 Prolog 程序解决最完美的魔方。
维基页面:https ://en.wikipedia.org/wiki/Most-perfect_magic_square
当我输入查询“magic_square(4, [[7, 12, 1, 14], [2, 13, 8, 11], [16, 3, 10, 5], [9, 6, 15, 4] ])。” (这是一个有效的魔方)我的程序返回 true。所以我假设我的规则基础是正确的。
不幸的是,如果超过 9 个值是未知的,则需要很长时间才能找到解决方案。
我需要帮助来限制我的搜索,以便程序在合理的时间内找到解决方案。理想情况下,它也应该适用于 12 x 12 网格(以及其他 4 的倍数)并且没有给出值:magic_square(12, Matrix)。
非常感谢!
这是我的代码:
:- use_module(library(clpfd)).
diag2_sum(0, _, _, _).
diag2_sum(I0, C1, Row1, Row3) :-
I0 > 0,
nth1(I0,Row1,A),
(I0 =:= 2 -> I2 = 4 ; I2 is mod(I0 + 2,4)),
nth1(I2,Row3,B),
C1 =:= A + B,
I1 is I0 - 1,
diag2_sum(I1, C1, Row1, Row3).
diag_sum([_,_], _, _).
diag_sum([Row1|Tail], C1, N) :-
nth1(2,Tail,Row3),
diag2_sum(N, C1, Row1,Row3),
diag_sum(Tail, C1, N).
square_sum_x(_, _, _, 0).
square_sum_x(Row1, Row2, C2, I0) :-
(I0 =:= 3 -> I2 = 4 ; I2 is mod(I0 + 1,4)),
nth1(I0,Row1,Elem1),
nth1(I2,Row1,Elem2),
nth1(I0,Row2,Elem3),
nth1(I2,Row2,Elem4),
C2 =:= Elem1 + Elem2 + Elem3 + Elem4,
I1 is I0 - 1,
square_sum_x(Row1, Row2, C2, I1).
square_sum_y(_, _, 0, _).
square_sum_y(Matrix, C2, I0, N) :-
(I0 =:= 3 -> I2 = 4 ; I2 is mod(I0 + 1,4)),
nth1(I0,Matrix,Row1),
nth1(I2,Matrix,Row2),
square_sum_x(Row1,Row2, C2, N),
I1 is I0 - 1,
square_sum_y(Matrix, C2, I1, N).
magic_square(N, Matrix) :-
Nmax is N * N,
C1 is Nmax + 1,
C2 is C1 * 2,
write(C1),nl,write(C2),nl,
length(Matrix, N),
maplist(same_length(Matrix), Matrix),
append(Matrix, Vs),
Vs ins 1..Nmax, all_different(Vs),
maplist(label, Matrix),
%write(Matrix),nl,
diag_sum(Matrix, C1, N),
square_sum_y(Matrix, C2, N, N).
% some queries i tryed out:
% magic_square(4, [[7, 12, 1, 14], [2, 13, 8, 11], [16, 3, 10, 5], [9, 6, 15, 4]]).
% magic_square(4, [[7, 12, 1, 14], [2, 13, 8, B4], [C1, C2, C3, C4], [D1, D2, D3, D4]]).
% magic_square(4, [[7, A2, A3, 14], [2, B2, 8, B4], [C1, C2, 10, C4], [D1, D2, D3, 4]]).