4

我正在尝试实现一个实现深度优先搜索和广度优先搜索的序言程序解决了以下问题

Rowena 有三个不同尺寸的无标记玻璃杯:3 盎司、5 盎司和 8 盎司。最大的玻璃杯已满。Rowena 可以做些什么来将 4 盎司的液体倒入较大的两个玻璃杯中?

我会有(大,中,小)

所以初始状态是(8,0,0),目标状态是(4,4,0)。

现在我知道我在状态空间中有 6 个可用的移动。

(1,2) 将大号倒入中号或小号 (3,4) 将中号倒入大号或小号 (5,6) 将小号倒入中号或大号

现在我只需要第一条规则的帮助,其余的我会弄清楚。所以我只能在 large > 0 和 medium 未满的情况下将 large 倒入 medium 中,新的 large 变成 old large 减去倒入介质的量,新的 medium 变成旧的 medium 加上量那倾注进去,小当然永远不会改变。

这是我尝试的。

%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 

这是第一个可用动作的正确实现吗(大到中)。我在那里得到了正确的逻辑吗?

4

1 回答 1

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 
于 2015-11-08T21:21:30.293 回答