1
0
This repository has been archived on 2025-03-06. You can view files and clone it, but cannot push or open issues or pull requests.
Jip J. Dekker fad1b07018 Squashed 'software/minizinc/' content from commit 4f10c8205
git-subtree-dir: software/minizinc
git-subtree-split: 4f10c82056ffcb1041d7ffef29d77a7eef92cf76
2021-06-16 14:06:46 +10:00

97 lines
2.6 KiB
Plaintext

% tenpenki.mzn
% vim: ft=zinc ts=4 sw=4 et
% Ralph Becket
% Thu Aug 14 15:23:11 EST 2008
%
% A tenpenki puzzle involves filling out the pixels in a grid to make
% a picture. Each row/column constraint specifies the distinct
% runs of "black" pixels in that row/column. For example,
%
% 1 1
% 1 2 1 1
% 1 1 5 2 1
% +-----------+
% 1 3 | # . # # # |
% 2 | . # # . . |
% 5 | # # # # # |
% 2 | . . # # . |
% 3 1 | # # # . # |
% +-----------+
% The dimensions of the board.
%
int: nrows;
int: ncols;
set of int: row = 1..nrows;
set of int: col = 1..ncols;
set of int: row_plus_one = 1..(nrows + 1);
set of int: col_plus_one = 1..(ncols + 1);
% The grid.
%
array [row, col] of var bool: a;
% A generic row constraint.
%
predicate row_constraint(row: r, array [int] of col: x) =
if index_set(x) = {} then
forall (c in col) (a[r, c] = false)
else
let {
int: n = max(index_set(x)),
array [1..n] of var col: s % The starting col of each block.
} in (
% There must be at least one white cell between each black block.
%
forall (i in 1..(n - 1)) (s[i + 1] > s[i] + x[i])
/\
% The final black block mustn't overrun the row.
%
s[n] + x[n] <= ncols + 1
/\
% Ensure that the pixels in this row are coloured in appropriately.
%
forall (c in col) (
a[r, c]
<->
exists (i in 1..n) (s[i] <= c /\ c < s[i] + x[i])
)
)
endif;
% A generic col constraint.
%
predicate col_constraint(col: c, array [int] of row: x) =
if index_set(x) = {} then
forall (r in row) (a[r, c] = false)
else
let {
int: n = max(index_set(x)),
array [1..n] of var row: s % The starting row of each block.
} in (
% There must be at least one white cell between each black block.
%
forall (i in 1..(n - 1)) (s[i + 1] > s[i] + x[i])
/\
% The final black block mustn't overrun the col.
%
s[n] + x[n] <= ncols + 1
/\
% Ensure that the pixels in this col are coloured in appropriately.
%
forall (r in row) (
a[r, c]
<->
exists (i in 1..n) (s[i] <= r /\ r < s[i] + x[i])
)
)
endif;
solve satisfy;
output [ if fix(a[r, c]) then "# " else ". " endif ++
if c = ncols then "\n" else "" endif
| r in row, c in col
];