我已经解决了这个水壶问题,我必须通过深度优先算法来完成。我有两个水壶,一个4加仑和一个3加仑,它们都没有标记。你怎么能在2加仑的水壶里加入2加仑的水呢?最初两个水壶都是空的。
规则是:
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).
提前谢谢!