Advertisement
Latkoski

Пребарување

Aug 25th, 2016
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lisp 4.55 KB | None | 0 0
  1.  
  2. (defun run-breadth (start goal moves)
  3.   (declare (special *open*))
  4.   (declare (special *closed*))
  5.   (declare (special *goal*))
  6.   (setq *open* (list (build-record start nil)))
  7.   (setq *closed* nil)
  8.   (setq *goal* goal)
  9.   (breadth-first moves))
  10.  
  11. ;;; These functions handle the creation and access of (state parent)
  12. ;;; pairs.
  13.  
  14. (defun build-record (state parent) (list state parent))
  15.  
  16. (defun get-state (state-tuple) (nth 0 state-tuple))
  17.  
  18. (defun get-parent (state-tuple) (nth 1 state-tuple))
  19.  
  20. (defun retrieve-by-state (state list)
  21.   (cond ((null list) nil)
  22.         ((equal state (get-state (car list))) (car list))
  23.         (t (retrieve-by-state state (cdr list)))))
  24.  
  25.  
  26.  
  27. (defun breadth-first (moves)
  28.   (declare (special *open*))
  29.   (declare (special *closed*))
  30.   (declare (special *goal*))
  31.   (cond ((null *open*) nil)
  32.         (t (let ((state (car *open*)))
  33.              (setq *closed* (cons state *closed*))
  34.  
  35.              (cond
  36.     ;;; found solution: print path to it
  37.         ((equal (get-state state) *goal*) (reverse (build-solution *goal*)))
  38.              
  39.             ;;; try next child state
  40.                 (t (setq *open*
  41.                             (append (cdr *open*)
  42.                                     (generate-descendants (get-state state)
  43.                                                           moves)))
  44.                       (breadth-first moves)))))))
  45.  
  46. (defun generate-descendants (state moves)
  47.   (declare (special *open*))
  48.   (declare (special *closed*))
  49.   (cond ((null moves) nil)
  50.         (t (let ((child (funcall (car moves) state))
  51.                  (rest (generate-descendants state (cdr moves))))
  52.              (cond ((null child) rest)
  53.                    ((retrieve-by-state child rest) rest)
  54.                    ((retrieve-by-state child *open*) rest)
  55.                    ((retrieve-by-state child *closed*) rest)
  56.                    (t (cons (build-record child state) rest)))))))
  57.  
  58.  
  59. (defun build-solution (state)
  60.   (declare (special *closed*))
  61.   (cond ((null state) nil)
  62.         (t (cons state (build-solution
  63.                         (get-parent
  64.                          (retrieve-by-state state *closed*)))))))
  65.  
  66. (defvar *initial* '((0 2) (4 0) ((2 1) (3 1) (2 3)) ((0 0) (3 3)))) ;; ((Px Py) (Kx Ky) ((D1x D1y) (D2x D2y)(D3x D3y)) ((Z1x Z1y) (Z2x Z2y)(Z3x Z3y)))
  67. ;(defvar *initial* '((0 2) (4 0) ((2 1) (3 1) (3 3)) ((2 2) (3 2) (2 3)))) ;; plus test primer
  68.  
  69. (setq king (car *initial*))
  70. (setq queen (car(cdr *initial*)))
  71. (setq ghostlist (car(cdr(cdr *initial*))))
  72. (setq dragonlist (car(cdr(cdr(cdr *initial*)))))
  73.  
  74. (defun make-state (x y)
  75.     (list x y)
  76.     )
  77. (defun safe (state)
  78.     (cond
  79.         ((eq (safeFromDragons dragonlist state) nil)  nil)
  80.         ((or (< (car state) 0)(> (car state) 4)(< (car(cdr state)) 0)(> (car (cdr state)) 4)) nil)
  81.         (t state)
  82.         )
  83. )
  84.  
  85. (defun safeFromDragons (dragonlist state)
  86.     (cond
  87.         ((null dragonlist) t)
  88.         ((or (equal state (make-state (+ (car(car dragonlist)) 1)(car(cdr(car dragonlist)))))
  89.          (equal state  (make-state (- (car(car dragonlist)) 1)(car(cdr(car dragonlist)))))
  90.          (equal state  (make-state (car(car dragonlist))(+ (car(cdr(car dragonlist))) 1)))
  91.          (equal state  (make-state (car(car dragonlist))(- (car(cdr(car dragonlist))) 1)))
  92.          (equal state  (make-state (+ (car(car dragonlist)) 1)(+ (car(cdr(car dragonlist)))1)))
  93.          (equal state  (make-state (+ (car(car dragonlist)) 1)(- (car(cdr(car dragonlist)))1)))
  94.          (equal state  (make-state (- (car(car dragonlist)) 1)(+ (car(cdr(car dragonlist)))1)))
  95.          (equal state  (make-state (- (car(car dragonlist)) 1)(- (car(cdr(car dragonlist)))1))))
  96.              nil)
  97.         (t (safeFromDragons (cdr dragonlist) state))
  98.     ))
  99. (defun gore (state)
  100.     (safe (make-state (+ (car state) 1) (cadr state)))    
  101. )
  102.  
  103. (defun dolu (state)
  104.     (safe (make-state (- (car state) 1) (cadr state)))    
  105. )
  106.  
  107. (defun levo (state)
  108.     (safe (make-state (car state) (- (cadr state) 1)))  
  109. )
  110.  
  111. (defun desno (state)
  112.     (safe (make-state (car state) (+ (cadr state) 1)))  
  113. )
  114.  
  115. (defun gore-desno (state)
  116.     (safe (make-state (+ (car state) 1) (+ (cadr state) 1)))    
  117. )
  118.  
  119. (defun gore-levo (state)
  120.     (safe (make-state (+ (car state) 1) (- (cadr state) 1)))    
  121. )
  122.  
  123. (defun dolu-desno (state)
  124.     (safe (make-state (- (car state) 1) (+ (cadr state) 1)))    
  125. )
  126.  
  127. (defun dolu-levo (state)
  128.     (safe (make-state (- (car state) 1) (- (cadr state) 1)))    
  129. )
  130.  
  131.  
  132. (print(run-breadth king queen '(gore dolu levo desno gore-levo gore-desno dolu-levo dolu-desno)))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement