Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/visual-studio-2010/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Recursion 求解N-Queen的递归lisp函数_Recursion_Common Lisp_N Queens - Fatal编程技术网

Recursion 求解N-Queen的递归lisp函数

Recursion 求解N-Queen的递归lisp函数,recursion,common-lisp,n-queens,Recursion,Common Lisp,N Queens,更新:代码应该现在编译,没有错误或警告。对上一个很抱歉。我现在遇到的问题是,当运行(或使用任何其他整数)时 函数getqueencol将返回nil,因为电路板上首先没有皇后,因此中会有一个(=数字nil)皇后可以放在这里,因为tcol将为nil。我认为,每当行中没有皇后作为参数传递给时,就会发生这种情况,皇后可以放在这里函数 请分享一些关于如何解决此问题的建议。先谢谢你 这是密码 (defvar *board* (make-array '(10 10) :initial-element nil)

更新:代码应该现在编译,没有错误或警告。对上一个很抱歉。我现在遇到的问题是,当运行(或使用任何其他整数)时

函数getqueencol将返回nil,因为电路板上首先没有皇后,因此中会有一个(=数字nil)皇后可以放在这里,因为tcol将为nil。我认为,每当行中没有皇后作为参数传递给时,就会发生这种情况,皇后可以放在这里函数

请分享一些关于如何解决此问题的建议。先谢谢你

这是密码

(defvar *board* (make-array '(10 10) :initial-element nil)) 


(defun getqueencol (row n)
"Traverses through the columns of a certain row
 and returns the column index of the queen."
  (loop for i below n
        do (if (aref *board* row i)
               (return-from getqueencol i))))

(defun print-board (n)
"Prints out the solution, e.g. (1 4 2 5 3),
 where 1 denotes that there is a queen at the first 
 column of the first row, and so on."
  (let ((solutionlist (make-list n)))
    (loop for row below n
          do (loop for col below n
                   do (when (aref *board* row col)
                        (setf (nth row solutionlist) col))))
    (print solutionlist)))


(defun queen-can-be-placed-here (row col n)
"Returns t if (row,col) is a possible place to put queen, otherwise nil."
  (loop for i below n
       do (let ((tcol (getqueencol i n)))
            (if (or (= col tcol) (= (abs (- row i)) (abs (- col tcol))))
                (return-from queen-can-be-placed-here nil)))))



(defun backtracking (row n)
"Solves the NxN-queen problem with backtracking"
  (if (< row n)
      (loop for i below n
          do (when (queen-can-be-placed-here row i n)
                  (setf (aref *board* row i) 't)
                  (return-from backtracking (backtracking (+ row 1) n))
                  (setf (aref *board* row i) 'nil))
    (print-board n))))

(defun NxNqueen-solver (k)
"Main program for the function call to the recursive solving of the problem"
  (setf *board* (make-array '(k k) :initial-element nil))
  (backtracking 0 k))
(定义变量*板*(生成数组(10):初始元素为零))
(第n行)
“遍历某一行的列
并返回女王的列索引。“
(i小于n的循环
do(如果(aref*董事会*第一行)
(从getqueencol返回i)))
(去风打印板(n)
“打印出解决方案,例如(1 4 2 5 3),
其中1表示第一个位置有一个女王
第一行的列,依此类推。“
(let((解决方案列表(生成列表n)))
(n以下行的循环)
do(针对n以下列的循环
do(当(aref*板*行列)
(setf(第n行解决方案列表)列)
(打印解决方案列表)))
(德芬女王可放置在此处(第n列)
如果(行,列)是放置皇后的可能位置,则返回t,否则返回nil
(i小于n的循环
do(let((tcol(getqueencol i n)))
(如果(或(=col tcol)(=(abs(-第一行))(abs(-col tcol)))
(女王返回可放置在此处零(()())))
(取消回溯(第n行)
“通过回溯解决NxN queen问题”
(如果(<第n行)
(i小于n的循环
do(何时(皇后可放置在此处第一排)
(setf(aref*板*第一排)'t)
(回溯返回(回溯(+第1行)n))
(setf(aref*董事会*第一行)“无”)
(印刷电路板)
(defun NxNqueen解算器(k)
“递归解决问题的函数调用主程序”
(setf*板*(生成数组(k):初始元素为零)
(回溯0 k))

您说您编译了代码。不可能是这样的,因为那时你会看到编译器抱怨错误。您希望确保您确实编译了代码并更正了代码,以便它在编译时不会出现错误和警告

您可能希望消除代码中的错误/问题(请参阅Renzo的评论),然后查看算法问题。当代码包含错误时,研究算法问题没有什么意义

  • SETQ
    没有引入变量,必须在某个地方定义变量

  • DEFVAR
    在函数内部没有意义

  • (让(x(sina))…)
    这样的东西看起来肯定是错的。
    LET
    的语法要求绑定列表周围有一对括号

  • RETURN-FROM
    将要从中返回的现有块的名称作为第一个参数。可选的第二个参数是返回值。获取正确的语法并从正确的块返回

  • 在调用
    MAKE-ARRAY
    时,指定默认值:
    (MAKE-ARRAY…:initial element nil)
    ,否则不清楚它是什么

  • 变量
    *board*
    未定义

风格

  • 循环中
    对于i到(1-n)
    更简单
    对于n以下的i

  • 您不需要引用
    NIL
    t

  • (if(eq foo t)…)
    可能更简单,可以写成
    (if foo…
    )。尤其是当
    foo
    的值为
    NIL
    T

  • (如果foo(progn…)


我不知道你在做什么,声称你的代码编译。它不编译

每个函数都有编译器警告。您应该检查编译器警告并修复问题。

(defun getqueencol (row)
"Traverses through the columns of a certain row
 and returns the column index of the queen."
  (loop for i below n
        do (if (aref board row i)
               (return-from getqueencol i))))
编译器抱怨:

;;;*** Warning in GETQUEENCOL: N assumed special
;;;*** Warning in GETQUEENCOL: BOARD assumed special
哪里定义了
n
board
来自哪里

(defun print-board (board)
"Prints out the solution, e.g. (1 4 2 5 3),
 where 1 denotes that there is a queen at the first 
 column of the first row, and so on."
  (let (solutionlist)
    (setq solutionlist (make-list n)))
  (loop for row below n
        do (loop for col below n
               do (when (aref board row col)
                      (setf (nth row solutionlist) col))))
  (print solutionlist))
(defun queen-can-be-placed-here (row col)
"Returns t if (row,col) is a possible place to put queen, otherwise nil."
  (loop for i below n
       do (let (tcol)
            (setq tcol (getqueencol i)))
       (if (or (= col tcol) (= (abs (- row i)) (abs (- col tcol))))
          (return-from queen-can-be-placed-here nil))))
LET
毫无意义<代码>(let(foo)(setq foo-bar)…)
(let((foo-bar))…)

为什么没有定义solutionlist?看一下
。。。这没有道理

n
来自哪里

(defun print-board (board)
"Prints out the solution, e.g. (1 4 2 5 3),
 where 1 denotes that there is a queen at the first 
 column of the first row, and so on."
  (let (solutionlist)
    (setq solutionlist (make-list n)))
  (loop for row below n
        do (loop for col below n
               do (when (aref board row col)
                      (setf (nth row solutionlist) col))))
  (print solutionlist))
(defun queen-can-be-placed-here (row col)
"Returns t if (row,col) is a possible place to put queen, otherwise nil."
  (loop for i below n
       do (let (tcol)
            (setq tcol (getqueencol i)))
       (if (or (= col tcol) (= (abs (- row i)) (abs (- col tcol))))
          (return-from queen-can-be-placed-here nil))))
n
来自哪里?
LET
毫无意义

(defun backtracking (row)
"Solves the NxN-queen problem with backtracking"
  (if (< row n)
      (loop for i below n
          do (when (queen-can-be-placed-here row i)
                  (setf (aref board row i) 't)
                  (return-from backtracking (backtracking (+ row 1)))
                  (setf (aref board row i) 'nil))
    (print-board board))))
如果您有一个
let
,为什么要使用
setq
?局部变量
n
board
未使用

MAKE-ARRAY
需要的是数字列表,而不是符号列表


我建议您使用基本的Lisp简介(-免费下载)和Lisp引用()。

您说您编译了代码。不可能是这样的,因为那时你会看到编译器抱怨错误。您希望确保您确实编译了代码并更正了代码,以便它在编译时不会出现错误和警告

您可能希望消除代码中的错误/问题(请参阅Renzo的评论),然后查看算法问题。当代码包含错误时,研究算法问题没有什么意义

  • SETQ
    没有引入变量,必须在某个地方定义变量

  • DEFVAR
    在函数内部没有意义

  • (让(x(sina))…)
    这样的东西看起来肯定是错的。
    LET
    的语法要求绑定列表周围有一对括号

  • RETURN-FROM
    将现有bl的名称作为第一个参数