35 lines
1.2 KiB
MiniZinc
35 lines
1.2 KiB
MiniZinc
int: n;
|
|
array [1..n] of var 1..n: q; % 在第i列的皇后在q[i]上
|
|
|
|
include "alldifferent.mzn";
|
|
|
|
constraint alldifferent(q); % distinct rows
|
|
constraint alldifferent([ q[i] + i | i in 1..n]); % distinct diagonals
|
|
constraint alldifferent([ q[i] - i | i in 1..n]); % upwards+downwards
|
|
|
|
include "lex_lesseq.mzn";
|
|
|
|
% 可选的布尔模型:
|
|
% 映射每一个位置 i,j到一个布尔变量上来表示在i,j上是否有一个皇后
|
|
array[1..n,1..n] of var bool: qb;
|
|
|
|
% 连通约束
|
|
constraint forall (i,j in 1..n) ( qb[i,j] <-> (q[i]=j) );
|
|
|
|
% 字典排序对称性破缺
|
|
constraint
|
|
lex_lesseq(array1d(qb), [ qb[j,i] | i,j in 1..n ])
|
|
/\ lex_lesseq(array1d(qb), [ qb[i,j] | i in reverse(1..n), j in 1..n ])
|
|
/\ lex_lesseq(array1d(qb), [ qb[j,i] | i in 1..n, j in reverse(1..n) ])
|
|
/\ lex_lesseq(array1d(qb), [ qb[i,j] | i in 1..n, j in reverse(1..n) ])
|
|
/\ lex_lesseq(array1d(qb), [ qb[j,i] | i in reverse(1..n), j in 1..n ])
|
|
/\ lex_lesseq(array1d(qb), [ qb[i,j] | i,j in reverse(1..n) ])
|
|
/\ lex_lesseq(array1d(qb), [ qb[j,i] | i,j in reverse(1..n) ])
|
|
;
|
|
|
|
% 搜索
|
|
solve :: int_search(q, first_fail, indomain_min, complete)
|
|
satisfy;
|
|
output [ if fix(q[j]) == i then "Q" else "." endif ++
|
|
if j == n then "\n" else "" endif | i,j in 1..n]
|