0 Replies - 647 Views - Last Post: 29 September 2015 - 11:24 PM

#1 coderqueen  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 10-December 14

Help with backtracking function in Lisp

Posted 29 September 2015 - 11:24 PM

Hi, I am trying to code the N queens problem in Lisp using backtracking. I am supposed to be printing only one solution as the output. However, my backtracking function seems to be printing all possible solutions. I am not sure where and how should I exit from the recursive backtracking once I have received a possible solution.
Here is my backtracking function:

 (defun backtracksearch (row n)
  // (if ()
(if (< row n)
      (loop for i below n
          do (when (is-safe row i n)
                  (setf (aref *chessboard* row i) 'board)
                  (backtracksearch (+ row 1) n)
                  (setf (aref *chessboard* row i) 'nil)))
    (print-solution n)))



I have tried to use the same logic as that in the C++ solution of backtracking n-queens.
Any help/advice would be appreciated! Thanks

Is This A Good Question/Topic? 0
  • +

Page 1 of 1