I understand the idea of backtracking(eight queen it´s the classsic problem), i´m programmer of C,C++, JAVA,... I'm learning LISP, i know a bit about funtions, operators, etc...
I want compile the code and throw me the solution, but i have problem to do that!!! 2012/7/7 Abhishek Sharma <[email protected]> > whats your purpose behind that ?? > > a) u want to understand the algorithm ? then go through backtracking and > then try to solve the problem > b) u want to learn LISP ?? then this is not the right place... u may want > to go through LIPS tutorials and then have a look at at this problem again > > > On Sun, Jul 8, 2012 at 12:15 AM, Victor Manuel Grijalva Altamirano < > [email protected]> wrote: > >> The next code, it's about the problem Eight Queens, i found it in >> internet, but i'm new in LISP and i don´t know how to compile it... >> Can you help me? >> >> (defun find-queen (arr p d) >> (destructuring-bind ((px py) (dx dy)) (list p d) >> (do ((x px (+ x dx)) >> (y py (+ y dy))) >> ((not (array-in-bounds-p arr x y))) >> (when (= (aref arr x y) 1) (return t))))) >> >> (defun queen-in-row (board row) >> (find-queen board (list row 0) '(0 1))) >> >> (defun queen-in-col (board col) >> (find-queen board (list 0 col) '(1 0) )) >> >> (defun queen-in-diags (board x y) >> (member t (mapcar (lambda (p) >> (find-queen board (list x y) p)) >> '((1 1) (-1 -1) (1 -1) (-1 1))))) >> >> (defun queen-in-range (board x y) >> (or (queen-in-row board x) >> (queen-in-col board y) >> (queen-in-diags board x y))) >> >> (defun backtracking (pred explore node) >> (if (funcall pred node) (list node) >> (mapcan (lambda (n) (backtracking pred explore n)) >> (funcall explore node)))) >> >> (defun count-queens (board) >> (loop for i below (array-total-size board) >> for box = (row-major-aref board i) >> count (> box 0))) >> >> (defun solutionp (board) >> (and board (= 8 (count-queens board)))) >> >> (defun put-queens (board) >> (loop for i below 8 >> when (not (queen-in-row board i)) >> return >> (loop for j below 8 >> for b = (copy-array board) >> when (not (queen-in-range board i j)) >> do (setf (aref b i j) 1) >> and collect b))) >> >> (defun 8-queens () >> (backtracking #'solutionp #'put-queens board)) >> >> (defvar board (make-array '(8 8) :initial-element 0)) >> >> >> -- >> Victor Manuel Grijalva Altamirano >> Universidad Tecnologica de La Mixteca >> >> -- >> You received this message because you are subscribed to the Google Groups >> "Algorithm Geeks" group. >> To post to this group, send email to [email protected]. >> To unsubscribe from this group, send email to >> [email protected]. >> For more options, visit this group at >> http://groups.google.com/group/algogeeks?hl=en. >> > > -- > You received this message because you are subscribed to the Google Groups > "Algorithm Geeks" group. > To post to this group, send email to [email protected]. > To unsubscribe from this group, send email to > [email protected]. > For more options, visit this group at > http://groups.google.com/group/algogeeks?hl=en. > -- Victor Manuel Grijalva Altamirano Universidad Tecnologica de La Mixteca -- You received this message because you are subscribed to the Google Groups "Algorithm Geeks" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.
