是的,你可以用 ASP 做到这一点。
这不是最优雅的代码,但我认为如果您使用 Clingo 系列 5,您可以这样做:
%% List the numbers to fill the matrix, we must associated each with an index value
number(1,1;2,2;3,3;1,4;3,5;5,6;1,7;2,8;4,9;2,10;3,11;7,12).
%% x limits
x(0..2).
%% y limits
y(0..3).
%% Generate the matrix
{ matrix(V, I, X, Y) } :- number(V, I), x(X), y(Y).
%% Define the order of the elements
order(V1, I1, V2, I2, Y) :- matrix(V1, I1, X1, Y), matrix(V2, I2, X2, Y), X1 < X2.
%% Do not allow two different rows to have the same order of elements
:- order(V1, I1, V2, I2, Y1), order(V1, I3, V2, I4, Y2), Y1 != Y2.
%% Each number must be in the matrix exactly once
:- not { matrix(V, I, X, Y) } = 1, number(V, I).
%% Each element can contain only one number
:- not #count { V, I : matrix(V, I, X, Y) } = 1, matrix(_, _, X, Y).
%% Show the matrix
#show matrix/4.
这给了你这个作为输出:
3 1 3
4 2 3
1 1 5
2 2 7
尽管牢记在心,但有数十万(我认为数百万)方法可以订购此矩阵并获得满足您的约束的结果。