我有这个水壶问题要解决,我必须通过深度优先算法来解决。我有两个水壶,一个 4 加仑和一个 3 加仑,它们都没有标记。你如何才能将 2 加仑的水准确地倒入 4 加仑的水壶中?最初两个水壶都是空的。
规则是:
capacity(C,JC), for C equal j1 or j2;
jugs(C1,C2), where C1 and C2 give the current contents of the jugs
初始起始事实:jugs(0,0) 目标:jugs(2,0) 或 jugs(0,2)
initial_state(jugs,jugs(0,0)).
final_state(jugs(2,C2)).
final_state(jugs(C1,2)).
transition(jugs(C1,C2),fill(j1)).
transition(jugs(C1,C2),fill(j2)).
transition(jugs(C1,C2),empty(j1)) :-
C1 > 0.
transition(jugs(C1,C2),empty(j2)) :-
C2 > 0.
transition(jugs(C1,C2),transfer(j2,j1)).
transition(jugs(C1,C2),transfer(j1,j2)).
update(jugs(C1,C2),empty(j1),jugs(0,C2)).
update(jugs(C1,C2),empty(j2),jugs(C1,0)).
update(jugs(C1,C2),fill(j1),jugs(Capacity,C2)) :-
capacity(j1,Capacity).
update(jugs(C1,C2),fill(j2),jugs(C1,Capacity)) :-
capacity(j2,Capacity).
update(jugs(C1,C2),transfer(j2,j1),jugs(W1,W2)) :-
capacity(j1,Capacity),
Water is C1 + C2,
Overhang is Water - Capacity ,
adapt(Water,Overhang,W1,W2).
update(jugs(C1,C2),transfer(j1,j2),jugs(W1,W2)) :-
capacity(j2,Capacity),
Water is C1 + C2,
Overhang is Water - Capacity ,
adapt(Water,Overhang,W2,W1).
adapt(Water,Overhang,Water,0) :- Overhang =< 0.
adapt(Water,Overhang,C,Overhang) :-
Overhang > 0,
C is Water - Overhang.
legal(jugs(C1,C2)).
capacity(j1,4).
capacity(j2,3).
先感谢您!