我正在尝试实现一个实现深度优先搜索和广度优先搜索的prolog progarm解决了以下问题
Rowena有三种不同尺寸的无标记眼镜:3盎司,5 盎司,8盎司。最大的玻璃杯已满。什么可以Rowena 每个较大的两个眼镜能够得到4盎司的液体吗?
我会(大,中,小)
所以初始状态是(8,0,0),目标状态是(4,4,0)。
现在我知道我在状态空间中有6个可用的移动。
(1,2)将大块倒入中等或小块 (3,4)将介质倒入大或小 (5,6)倒入中等或大的
现在我只需要第一条规则的帮助,其余的我会弄清楚。因此,如果大的>我只能将大量倒入介质中。 0并且介质不满,新的大号变成旧的大号减去倒入介质的量,新的介质变成旧的介质加上倒入的量,而小的当然不会改变。 / p>
这是我的尝试。
%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
这是第一次可用移动(大到中等)的正确实现。我在那里得到了正确的逻辑吗?
答案 0 :(得分:2)
我认为逻辑应该是
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