我正在尝试实现一个prolog progarm,实现深度优先搜索和广度优先搜索解决了以下问题
Rowena has three unmarked glasses of different sizes: 3 ounces, 5
ounces, and 8 ounces. The largest glass is full. What can Rowena
do to get 4 ounces of liquid into each of the larger two glasses?
我会(大,中,小)
所以初始状态是(8,0,0),目标状态是(4,4,0).
现在我知道我在状态空间中有6个可用的移动.
(1,2)将大块倒入中等或小块
(3,4)将介质倒入大或小
(5,6)小到中等或大
现在我只需要第一条规则的帮助,其余的我会弄清楚.因此,如果大的>我只能将大量倒入介质中. 0并且介质不是满的,新的大号变成旧的大号减去倒入介质的量,新的介质变成旧的介质加上倒入的量,而小的当然不会改变.
这是我的尝试.
%move rule #1: Pour Large into Medium (L--> M)
%move(oldstate,newstate)
move([L, M, S], [NewLarge,NewMedium,S]) :-
L > 0, %We can't move large into medium if Large has nothing
M < 5, %We can't pour into the medium if medium is full
Diff = 5 - M,
NewLarge is L - Diff, %calculate the new Large
NewMedium is M + (L - NewLarge). %calculate the new Medium
这是第一次可用移动(大到中等)的正确实现.我在那里得到了正确的逻辑吗?
最佳答案 我认为逻辑应该是
move([L, M, S], [NewLarge, NewMedium, S]) :-
Diff is min(5 - M, L),
Diff > 0,
NewLarge is L - Diff, %calculate the new Large
NewMedium is M + Diff. %calculate the new Medium