I'm trying to use restrictions programming through Prolog CLP FD to solve a puzzle that was proposed. This puzzle consists in the next simple rules:
Now, in my code, I already cover the restrictions for the 2x2 grid AND that one piece MUST be connected to AT LEAST one of the same color.
The problem is that I cannot find a way to build the restriction that says that one piece MUST have a PATH (be connected) to all the other pieces of the same color, without passing through pieces of the opposite color, so I'm getting this kind of outputs:
0 0 0 0
0 1 0 1
0 1 0 1
0 1 0 0
0 0 0 0
0 1 0 1
0 1 0 1
0 1 0 1
where the 1s are not all connected to each other.
How can I write this kind of graph restrictions in CLP FD?
EDIT: I am using SICStus Prolog.
To reword your situation so that we can more clearly think about it:
To make your program more specific, you must find a condition that is currently violated in one of the answers you generate, but which must hold in every solution, and then express this condition with constraints.
For example, consider again you case:
Which condition is violated here? Obviously, the
1
pieces are not along a path. But describing a full path with CLP(FD) is extremely tedious, and since this is apparently taken from an exam or homework question, the idea suggests itself that there is a simple local criterion to express the desired condition.By "local", I mean that you only have to take into account a few neighbours instead of the whole board.
So, consider again the
1
pieces. Obviously, every1
piece has a neighbour that is also 1 in this answer. What else? Does every1
piece have 2 neighbours? No currently, not. Should every1
piece have 2 neighbours that are also1
? If not, how many exceptions are admissible?If you think along these conditions, you will definitely obtain a nice solution.
One hint: Sometimes reified constraints are useful in such tasks. This means that you can for example say:
B #<==> (X #= Y)
, and letB
denote whetherX #= Y
holds. Note that you may not even need this in this case.