-3

Starting with an empty 5-gallon jug and an empty 11-gallon jug, how can we end up with exactly 3 gallons of water in the 11-gallon jug and with the 5-gallon jug empty?

I want to write a function in Lisp that computes a list of successor states for any state in this puzzle

my solution

(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)

How can I implement successors function?

        (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)

)

please help !!!!

nesreen
  • 33
  • 1
  • 6
  • 5
    We're not going to do your homework for you. You need to make an attempt yourself. If you can't get it working, post what you tried and we'll help you understand what you did wrong and how to fix it. – Barmar Aug 12 '15 at 09:44
  • @Barmar i don't want you to do my homework , all what i've asked for is a hint to help me start implementing the function – nesreen Aug 12 '15 at 13:26

1 Answers1

1

Here is a hint to start:

(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
Renzo
  • 26,848
  • 5
  • 49
  • 61