gpt4 book ai didi

lisp - 用 LISP 填充水壶

转载 作者:太空宇宙 更新时间:2023-11-03 19:04:46 28 4
gpt4 key购买 nike

从一个 5 加仑的空水壶和一个 11 加仑的空水壶开始,我们如何才能在 11 加仑的水壶和 5 加仑的水壶是空的情况下恰好有 3 加仑的水?

我想用 Lisp 编写一个函数来计算这个谜题中任何状态的后继状态列表

我的解决方案

(0 0) > (5 0) > (0 5) > (5 5) > (0 10 ) > (5 10)>(4 11)>(4 0)>(0 4)>(5 4)>(0 9)>(5 9)>(3 11)>(3 0)>(0 3)

如何实现 successors 功能?

        (setq initial-state '(0 0))
(setq fill-jug1-state '(5 0))
(setq fill-jug2-state '(0 11))
(setq full '(5 11))
(defparameter *jug-1* 5)
(defparameter *jug-2* 11)
(defun successors (initial-state)

)

请帮忙!!!!

最佳答案

这是一个开始的提示:

(defun successors (state)        ; for each state
(let ((jug1 (first state)) ; gallons in jug1 for state
(jug2 (second state)) ; gallons in jug2 for state
(new-states nil)) ; successor states of state
(when (< jug1 5) ; if jug1 is not full
(push (list 5 jug2) new-states)) ; then fill jug1
; do the same for jug2
; ...
(when (> jug1 0) ; if jug1 has some water
;... empty jug1, that is, new-state = (0 jug2)
; do the same for jug2 if jug2 has some water
;...
(when (and (> jug2 0) (< jug1 5)) ; if jug2 can give water to jug1
; then pour the water of jug2 in jug1
(push (list (min 5 (+ jug1 jug2))
(max (- jug2 (- 5 jug1)) 0)) new-states))
; do the same for the opposite situation
;...
new-states)) ; finally return the set of new states

关于lisp - 用 LISP 填充水壶,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31961173/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com