Advertisement
Guest User

Untitled

a guest
Mar 12th, 2016
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lisp 0.85 KB | None | 0 0
  1. (defun robot (x y k)
  2.   (let ((n 100) (m 100) (x1 60) (y1 60))
  3.     (if (= k 0)
  4.       (if (and (= x x1) (= y y1)) 1 0)
  5.       (let
  6.         (
  7.           (up (if (< (+ y 1) m) (robot x (+ y 1) (- k 1)) 0))
  8.           (down (if (>= (- y 1) 0) (robot x (- y 1) (- k 1)) 0))
  9.           (right (if (< (+ x 1) n) (robot (+ x 1) y (- k 1)) 0))
  10.           (left (if (>= (- x 1) 0) (robot (- x 1) y (- k 1)) 0))
  11.         )
  12.         (+ up down right left)
  13.       )
  14.     )
  15.   )
  16. )
  17.  
  18. (defun memoize (fn)
  19.   (let ((cache (make-hash-table :test #'equal)))
  20.     #'(lambda (&rest args)
  21.         (multiple-value-bind
  22.               (result exists)
  23.             (gethash args cache)
  24.           (if exists
  25.               result
  26.               (setf (gethash args cache)
  27.                     (apply fn args)))))))
  28.  
  29. (setf (fdefinition 'robot) (memoize #'robot))
  30.  
  31. (print (robot 50 50 200))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement