prolog - 在状态之间移动(Prolog 实现)

标签 prolog state-space

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

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 

关于prolog - 在状态之间移动(Prolog 实现),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33598922/

相关文章:

prolog 深度优先迭代深化

java - Prolog 嵌入 java 帮助中

prolog - Prolog中如何选择bagof、setof和findall

Prolog 时间重叠问题

Prolog - 在特殊情况下消除重复答案的更好方法?

prolog - Prolog中的 ' and "有什么区别?

a-star - 产生负值的启发式函数是否 Not Acceptable ?

matlab - 使用matlab卡尔曼进行非线性系统估计