Advertisement
Latkoski

Флип

Aug 25th, 2016
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lisp 4.97 KB | None | 0 0
  1. ; read input
  2. (defun read-input(p)
  3. (cond
  4.     ((null p) nil)
  5.     (T (cons p (read-input (read nil nil))))
  6. )
  7. )
  8.  
  9. (defun readinput(i)
  10.     (cdr (read-input 1))
  11. )  
  12.  
  13. (defun run-breadth (start goal moves)
  14.   (declare (special *open*))
  15.   (declare (special *closed*))
  16.   (declare (special *goal*))
  17.   (setq *open* (list (build-record start nil)))
  18.   (setq *closed* nil)
  19.   (setq *goal* goal)
  20.   (breadth-first moves))
  21.  
  22. ;;; These functions handle the creation and access of (state parent)
  23. ;;; pairs.
  24.  
  25. (defun build-record (state parent) (list state parent))
  26.  
  27. (defun get-state (state-tuple) (nth 0 state-tuple))
  28.  
  29. (defun get-parent (state-tuple) (nth 1 state-tuple))
  30.  
  31. (defun retrieve-by-state (state list)
  32.   (cond ((null list) nil)
  33.         ((equal state (get-state (car list))) (car list))
  34.         (t (retrieve-by-state state (cdr list)))))
  35.  
  36.  
  37.  
  38. (defun breadth-first (moves)
  39.   (declare (special *open*))
  40.   (declare (special *closed*))
  41.   (declare (special *goal*))
  42.   (cond ((null *open*) nil)
  43.         (t (let ((state (car *open*)))
  44.              (setq *closed* (cons state *closed*))
  45.  
  46.              (cond
  47.     ;;; found solution: print path to it
  48.         ((equal (get-state state) *goal*) (reverse (build-solution *goal*)))
  49.              
  50.             ;;; try next child state
  51.                 (t (setq *open*
  52.                             (append (cdr *open*)
  53.                                     (generate-descendants (get-state state)
  54.                                                           moves)))
  55.                       (breadth-first moves)))))))
  56.  
  57. (defun generate-descendants (state moves)
  58.   (declare (special *open*))
  59.   (declare (special *closed*))
  60.   (cond ((null moves) nil)
  61.         (t (let ((child (funcall (car moves) state))
  62.                  (rest (generate-descendants state (cdr moves))))
  63.              (cond ((null child) rest)
  64.                    ((retrieve-by-state child rest) rest)
  65.                    ((retrieve-by-state child *open*) rest)
  66.                    ((retrieve-by-state child *closed*) rest)
  67.                    (t (cons (build-record child state) rest)))))))
  68.  
  69.  
  70. (defun build-solution (state)
  71.   (declare (special *closed*))
  72.   (cond ((null state) nil)
  73.         (t (cons state (build-solution
  74.                         (get-parent
  75.                          (retrieve-by-state state *closed*)))))))
  76.  
  77.  
  78. (defvar *initial* (car (readinput nil)))
  79. ;--don't change above this line
  80.  
  81.  
  82. ; write your function(s) here
  83. (defun make-state (a b c d e f g h i)
  84. (list a b c d e f g h i)
  85. )
  86.  
  87.  
  88. (defun safeState (state)
  89. (cond
  90. ((null state) t)
  91. ((or (< (nth 0 (car state)) 0)(> (nth 0 (car state)) 2)(> (nth 1 (car state)) 2)(< (nth 1 (car state)) 0)) nil)
  92. (t (safeState (cdr state)))
  93.  
  94. )
  95. )
  96.  
  97. (defun safe (state)
  98. (cond
  99. ((safeState state) state)
  100. (t nil)
  101. )
  102. )
  103.  
  104.  
  105. (defun promeni (pole)
  106. (cond
  107. ((eq (nth 2 pole) 0)(list (nth 0 pole)(nth 1 pole) 1))
  108. ((eq (nth 2 pole) 1)(list (nth 0 pole)(nth 1 pole) 0))
  109. )
  110. )
  111.  
  112. (defun goreLevo (state)
  113. (safe (make-state (promeni (nth 0 state))(promeni (nth 1 state))(nth 2 state)(promeni (nth 3 state))(nth 4 state)(nth 5 state)(nth 6 state)(nth 7 state)(nth 8 state))
  114. ))
  115. (defun goreGore (state)
  116. (safe (make-state (promeni (nth 0 state))(promeni (nth 1 state))(promeni (nth 2 state))(nth 3 state)(promeni(nth 4 state))(nth 5 state)(nth 6 state)(nth 7 state)(nth 8 state))
  117. ))
  118. (defun goreDesno (state)
  119. (safe (make-state (nth 0 state) (promeni (nth 1 state)) (promeni (nth 2 state)) (nth 3 state) (nth 4 state) (promeni (nth 5 state)) (nth 6 state) (nth 7 state) (nth 8 state))))
  120.  
  121. (defun sredinaLevo (state)
  122. (safe (make-state (promeni (nth 0 state)) (nth 1 state) (nth 2 state) (promeni (nth 3 state)) (promeni (nth 4 state)) (nth 5 state) (promeni (nth 6 state)) (nth 7 state) (nth 8 state)))
  123. )
  124.  
  125. (defun sredinaSredina (state)
  126. (safe (make-state (nth 0 state) (promeni (nth 1 state)) (nth 2 state) (promeni (nth 3 state)) (promeni (nth 4 state)) (promeni (nth 5 state)) (nth 6 state) (promeni (nth 7 state)) (nth 8 state)))
  127. )
  128.  
  129. (defun sredinaDesno (state)
  130. (safe (make-state (nth 0 state) (nth 1 state) (promeni (nth 2 state)) (nth 3 state) (promeni (nth 4 state)) (promeni (nth 5 state)) (nth 6 state) (nth 7 state) (promeni (nth 8 state))))
  131. )
  132.  
  133. (defun doluLevo (state)
  134. (safe (make-state (nth 0 state) (nth 1 state) (nth 2 state) (promeni (nth 3 state)) (nth 4 state) (nth 5 state) (promeni (nth 6 state)) (promeni (nth 7 state)) (nth 8 state)))
  135. )
  136.  
  137. (defun doluDolu (state)
  138. (safe (make-state (nth 0 state) (nth 1 state) (nth 2 state) (nth 3 state) (promeni (nth 4 state)) (nth 5 state) (promeni (nth 6 state)) (promeni (nth 7 state)) (promeni (nth 8 state))))
  139. )
  140.  
  141. (defun doluDesno (state)
  142. (safe (make-state (nth 0 state) (nth 1 state) (nth 2 state) (nth 3 state) (nth 4 state) (promeni (nth 5 state)) (nth 6 state) (promeni (nth 7 state)) (promeni (nth 8 state))))
  143. )
  144.  
  145. (print (run-breadth *initial* '((0 0 1) (0 1 1) (0 2 1) (1 0 1) (1 1 1) (1 2 1) (2 0 1) (2 1 1) (2 2 1)) '(goreLevo goreGore goreDesno sredinaLevo sredinaSredina sredinaDesno doluLevo doluDolu doluDesno)))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement