在状态之间移动(Prolog 实现)
Moving between states (A Prolog implementation)
我正在尝试实现一个实现深度优先搜索和广度优先搜索的 prolog 程序,它解决了以下问题
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)。
现在我知道我在 space 状态下有 6 个可用的移动。
(1,2) 将大号倒入中号或小号
(3,4) 将 Medium 倒入 large or small
(5,6) 将小号倒入中号或大号
现在我只需要第一条规则的帮助,其余的我会弄清楚。所以我只能在 large > 0 且 medium 未满时将 large 倒入 medium,新的 large 变成旧的 large 减去倒入 medium 的量,新的 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
这是第一个可用移动(从大到中)的正确实施吗?我那里的逻辑正确吗?
我觉得逻辑应该是这样
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 程序,它解决了以下问题
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)。
现在我知道我在 space 状态下有 6 个可用的移动。
(1,2) 将大号倒入中号或小号 (3,4) 将 Medium 倒入 large or small (5,6) 将小号倒入中号或大号
现在我只需要第一条规则的帮助,其余的我会弄清楚。所以我只能在 large > 0 且 medium 未满时将 large 倒入 medium,新的 large 变成旧的 large 减去倒入 medium 的量,新的 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
这是第一个可用移动(从大到中)的正确实施吗?我那里的逻辑正确吗?
我觉得逻辑应该是这样
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