Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (defun depth-first-search (start goal been-list moves)
- (cond ((equal start goal)
- (reverse (cons start been-list)))
- (t (try-moves start goal been-list moves moves))))
- ; Try-moves scans down the list of moves in moves-to-try,
- ; attempting to generate a child state. If it produces
- ; this state, it calls depth-first-search to complete the search.
- (defun try-moves (start goal been-list moves-to-try moves)
- (cond ((null moves-to-try) nil)
- ((member start been-list :test #'equal) nil)
- (t (let ((child (funcall (car moves-to-try) start)))
- (if child
- (or (depth-first-search (funcall (car moves-to-try) start)
- goal
- (cons start been-list)
- moves)
- (try-moves start goal been-list (cdr moves-to-try) moves))
- (try-moves start goal been-list (cdr moves-to-try) moves))))))
- ; run-depth-first calls depth-first-search, initializing the been-list to ().
- (defun run-depth (start goal moves)
- (depth-first-search start goal () moves)
- )
- (defun safe (state)
- (cond
- ((or(> (car state) 3)(>(cadr state) 3)(< (car state) 1)(< (cadr state) 1)) nil)
- (T state)
- )
- )
- (defun make-state (x y)
- (list x y)
- )
- (defun move1 (state)
- (safe (make-state (+ 2 (car state))(+ 1(cadr state))))
- )
- ;x+2 y+1
- (defun move2 (state)
- (safe (make-state (+ 1 (car state))(+ 1 (cadr state))))
- )
- (defun move3(state)
- (safe (make-state(- (car state) 2)(+ 1(cadr state))))
- )
- (defun move4 (state)
- (safe (make-state(+ (car state) 1)(- (cadr state) 2)))
- )
- (defun move5(state)
- (safe (make-state(+ (car state) 1)(- (cadr state) 2)))
- )
- ;x+1 y+2
- (print (run-depth'(1 1) '(3 3) '(move1 move2 move3 move4)))
- ;(run-depth '(1 1) '(3 3) '(moves))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement