【发布时间】:2015-09-30 07:55:10
【问题描述】:
我正在使用 Lisp 中的回溯来解决 N-queens 问题。到目前为止,我的代码打印了 n>=4 的所有可能解决方案。但是,我希望只打印任何 n 值的第一个解决方案。
(defun backtracksearch (row n)
// (if () *I probably need a line here to stop once the first solution is found*
(if (< row n)
(loop for j below n
do (when (is-safe row j n)
(setf (aref *chessboard* row j) 'board)
(backtracksearch (+ 1 row) n)
(setf (aref *chessboard* row j) 'nil)))
(print-solution n)))
我尝试使用与回溯 n-queens 的 C++ 解决方案中相同的实现/逻辑。 任何关于可能的前进方向的建议都会有所帮助。
【问题讨论】:
-
为什么标记为
c++?将其标记为lisp。 -
“回溯 n-queens 的 C++ 解决方案”只有你知道。如果您想告诉我们,请提供参考;否则就别提了。