Acciones

Nuricabe (Alina Kasiuk y Xinyi Wu)

De Lógica computacional y teoría de modelos (2019-20)

% according to the known conditions
numbered(1,1,1).
numbered(1,5,1).
numbered(1,7,6).
numbered(2,3,2).
numbered(3,2,1).
numbered(3,5,2).
numbered(5,1,2).
numbered(5,4,1).
numbered(7,2,3).
numbered(7,6,1).

#const n=7.
#const m=7.

col(1..n).
row(1..m).
color(b;w).

% Keep some cells white (and paint the rest black)
{cell(w,X,Y)}1 :- row(X), col(Y).
cell(b,X,Y) :- not cell(w,X,Y), row(X), col(Y).


% ensuring that the given constraints N1--N8 are satisfied:

% N1: Numbered cells remain white.
:- not cell(w,X,Y), numbered(X,Y,N).


% N5 : every white cell is orthogonally connected.
% N7 : All black cells should be connected orthogonally.
adj(C,R,C1,R1) :- row(C), row(C1), col(R), col(R1), |C-C1| + |R-R1| == 1.
connected(C,X,Y,X,Y) :- cell(C,X,Y), row(X), col(Y), color(C).
connected(C,X,Y,U,V) :- connected(C,X,Y,W,Z), adj(W,Z,U,V),
                        cell(C,U,V),row(X;U;W), col(Y;V;Z), color(C).

w_connected(X,Y) :- connected(w,X,Y,U,V),cell(w,X,Y),numbered(U,V,N),
                    row(X), col(Y).
:- not connected(b,X,Y,U,V), cell(b,X,Y), cell(b,U,V),
   row(X;U),col(Y;V).



% N2 : every white cell belongs to an island.
:- cell(w,X,Y), not w_connected(X,Y), row(X), col(Y).



% N4: Each island should contain the same number of white cells
% as the number it contains.
island(X,Y) :- N {connected(w,X,Y,U,V):row(U),col(V)} N,
               numbered(X,Y,N).
:- not island(X,Y), numbered(X,Y,N).



% N3: Each island must contain exactly exactly one number cell,
% N6: The island can not be connected.
:- numbered(X,Y,N), numbered(U,V,M), X<U, connected(w,X,Y,U,V).
:- numbered(X,Y,N), numbered(U,V,M), Y<V, connected(w,X,Y,U,V).
%:- island(X1,Y1), island(X2,Y2), X1 != X2, connected(w,X1,Y1,X2,Y2).
%:- island(X1,Y1), island(X2,Y2), Y1 != Y2, connected(w,X1,Y1,X2,Y2).



% N8: No subset of black cells forms a 2x2 square.
:- sqrBlack(X,Y), row(X), col(Y).
sqrBlack(X,Y) :- cell(b,X+1,Y), cell(b,X,Y),
                 cell(b,X, Y+1), cell(b,X+1, Y+1), row(X), col(Y).

black(X,Y) :- cell(b,X,Y), row(X), col(Y).
#show black/2.
% #show w_connected/2.