Prolog 序言中的水壶宽度优先搜索
所以我需要解决水壶的问题——大的杯子能装5个,小的杯子能装3个。我想在更大的杯子里得到4分 我有BFS alogirtam的源代码,但我不知道如何创建谓词移动Prolog 序言中的水壶宽度优先搜索,prolog,water-jug-problem,Prolog,Water Jug Problem,所以我需要解决水壶的问题——大的杯子能装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 fo
% 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).
见: