Advertisement
Latkoski

Пребарување DFS - Коњски скок

Jan 11th, 2017
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lisp 1.85 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. ; run-depth-first calls depth-first-search, initializing the been-list to ().
  23. (defun run-depth (start goal moves)
  24.   (depth-first-search start goal () moves)
  25. )
  26.  
  27. (defun safe (state)
  28.     (cond
  29.         ((or(> (car state) 3)(>(cadr state) 3)(< (car state) 1)(< (cadr state) 1)) nil)
  30.         (T state)        
  31.     )
  32. )
  33. (defun make-state (x y)
  34.    (list x y)
  35. )
  36. (defun move1 (state)
  37.  (safe (make-state (+ 2 (car state))(+ 1(cadr state))))
  38. )
  39. ;x+2 y+1
  40. (defun move2 (state)
  41. (safe (make-state (+ 1 (car state))(+ 1 (cadr state))))
  42. )
  43. (defun move3(state)
  44. (safe (make-state(- (car state) 2)(+ 1(cadr state))))
  45. )
  46. (defun move4 (state)
  47. (safe (make-state(+ (car state) 1)(- (cadr state) 2)))
  48. )
  49. (defun move5(state)
  50. (safe (make-state(+ (car state) 1)(- (cadr state) 2)))
  51. )
  52. ;x+1 y+2       
  53. (print (run-depth'(1 1) '(3 3) '(move1 move2 move3 move4)))
  54. ;(run-depth '(1 1) '(3 3)  '(moves))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement