1

所以我需要解决那个水壶问题 - 大杯子装 5 个,小杯子装 3 个。我想在大杯子里放 4 个。

我有 BFS alogirtam 的来源,但我不知道如何创建谓词移动......

% 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).
4

0 回答 0