Robot Mouse in Maze

Depth-first search of an implicit tree can simulate a robot mouse in a maze. The goal is to return a sequence of steps to guide the mouse to the cheese.


(defun mouse (maze x y prev)
  (let (path here)
    (if (or (eq (aref maze y x) '*)
            (member (setq here (list x y))
                    prev :test 'equal))
        nil                         ; fail
        (if (eq (aref maze y x) 'c)
            '(cheese)               ; success
            (if (setq path
                  (mouse maze (- x 1) y
                         (cons here prev)))
                (cons 'w path)
            (if (setq path
                  (mouse maze x (- y 1) (cons here prev)))
                (cons 'n path)
            (if (setq path
                  (mouse maze (+ x 1) y (cons here prev)))
                (cons 'e path)
            (if (setq path
                  (mouse maze x (+ y 1) (cons here prev)))
                (cons 's path)
                nil)))))) ))        ; fail

Contents    Page-10    Prev    Next    Page+10    Index