Advertisement
Guest User

Untitled

a guest
May 27th, 2015
274
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lisp 2.18 KB | None | 0 0
  1. (defun depth-first-search (start goal been-list moves)
  2.   (cond ((equal start goal)
  3.          (reverse (cons start been-list)))
  4.         (t (try-moves start goal been-list moves moves))))
  5.  
  6. ; Try-moves scans down the list of moves in moves-to-try,
  7. ; attempting to generate a child state.  If it produces
  8. ; this state, it calls depth-first-search to complete the search.
  9.  
  10. (defun try-moves (start goal been-list moves-to-try moves)
  11.   (cond ((null moves-to-try) nil)
  12.         ((member start been-list :test #'equal) nil)
  13.         (t (let ((child (funcall (car moves-to-try) start)))
  14.              (if child
  15.                (or (depth-first-search (funcall (car moves-to-try) start)
  16.                                        goal
  17.                                        (cons start been-list)
  18.                                        moves)
  19.                    (try-moves start goal been-list (cdr moves-to-try) moves))
  20.                (try-moves start goal been-list (cdr moves-to-try) moves))))))
  21.  
  22. (defun valid-move (a b)
  23.     (if (and (> a 0) (< a 4) (> b 0) (< b 4)) T nil)
  24. )
  25.  
  26.  
  27. (defun moves (state)
  28.     (cond
  29.         ((valid-move (+ (car state) 1) (+ (car (cdr state)) 2)) (append (list (+ (car state) 1)) (list (+ (car (cdr state)) 2))))
  30.         ((valid-move (+ (car state) 2) (+ (car (cdr state)) 1)) (append (list (+ (car state) 2)) (list (+ (car (cdr state)) 1))))
  31.         ((valid-move (+ (car state) 1) (- (car (cdr state)) 2)) (append (list (+ (car state) 1)) (list (- (car (cdr state)) 2))))
  32.         ((valid-move (- (car state) 2) (+ (car (cdr state)) 1)) (append (list (- (car state) 2)) (list (+ (car (cdr state)) 1))))
  33.         )
  34. )
  35. (defun path (state goal been-list)
  36.    (cond
  37.     ((null state) nil)
  38.     ((equal state goal) (reverse (cons state been-list)))
  39.     ((not (member state been-list :test #'equal))
  40.      (or
  41.       (path (moves state) goal (cons state been-list))
  42.       )))
  43. )
  44.  
  45. (defun solve (state goal)
  46.     (path state goal nil)    
  47. )
  48.  
  49. (defun run-depth (start goal moves)
  50.   (depth-first-search start goal () moves))
  51.  
  52. ; dodajte ja listata na potezi koi treba da se probaat za sekoja sostojba i odkomentirajte ja slednata linija
  53. (run-depth '(1 1) '(3 3)  '(moves))
  54. (print (solve '(1 1) '(3 3)))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement