Can anyone find why I can\'t have any true answers with my \'go\' at this code? For example, I write go(7,3,l)
and I suppose that it should move 3 litres of water t
Expanding on @Mog's answer, I suggest to use iterative deepening if you want to find a shortest solution. The following is based on the code posted by @Mog (and thus solves the same problem that is slightly different from the one posted by OP). As we want to describe a list (of moves), DCG notation is convenient:
solution(Path) :- length(Path, _), phrase(path([0-3, 0-5, 8-8]), Path).
path(State) --> { equivalent(State, [0-3, 4-5, 4-8]) }.
path(State0) --> [From-To],
{ move(State0, State), State = [_-From, _-To, _] },
path(State).
equivalent(State1, State2) :- forall(member(X, State1), member(X, State2)).
move(State, [NewX-From, NewY-To|NewRest]) :-
select(X-From, State, Rest),
X \== 0,
select(Y-To, Rest, NewRest),
Fillable is To - Y,
ToFill is min(X, Fillable),
NewY is Y + ToFill,
NewX is X - ToFill.
Sample query:
?- solution(Ps).
Ps = [8-5, 5-3, 3-8, 5-3, 8-5, 5-3, 3-8] .