water jug in prolog breadth-first search

1.3k Views Asked by At

So i need to solve that water jug problem - The larger cup holds 5, the smaller cup holds 3. I want to get 4 in the larger cup.

I have the source of the BFS alogirtam but i don't know how to create predicate move...

% DEPTH-FIRST SEARCH

path(Start, Goal):-
    dfs1([(Start, nil)], [], Goal).

% dfs1(OPEN, CLOSED, Goal).

dfs1([], _, _):-
    write(' Graph searched, no solution is found!').

dfs1([(Goal, Parent)|_], CLOSED, Goal):-
    !,
    write('Solution path is:'), nl,
    printsolution((Goal, Parent), CLOSED).

dfs1([(X, Parent)|Rest_OPEN], CLOSED, Goal):-
    add((X, Parent), CLOSED, New_CLOSED),
    get_surv_children(X, Rest_OPEN, New_CLOSED, Surv_Children),
    append(Rest_OPEN,Surv_Children, New_OPEN),
    dfs1(New_OPEN, New_CLOSED, Goal).

get_surv_children(State, OPEN, CLOSED, Surv_Children):-
     findall(Child, move_surv(State, OPEN, CLOSED, Child),
         Surv_Children).

move_surv(State, OPEN, CLOSED, (Next, State)):-
    move(State, Next),
       \+ member_term(Next, OPEN),
       \+ member_term(Next, CLOSED).

printsolution((State, nil), _):- write(State), nl.

printsolution((State, Parent), CLOSED):-
    member((Parent, Grandparent), CLOSED),
    printsolution((Parent, Grandparent), CLOSED),
    write(State), nl.

add(E, L, [E|L]).

member_term(E,[(E,_)|T]).

member_term(E,[_|T]):-
    member_term(E, T).
0

There are 0 best solutions below