Advertisement
Latkoski

принц/принцеза пребарување bfs

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