如何解决方案中的 n-queens

Posted

技术标签:

【中文标题】如何解决方案中的 n-queens【英文标题】:How to solve n-queens in scheme 【发布时间】:2013-11-30 22:14:39 【问题描述】:

我正在尝试解决方案中的 n-queens 问题。我的教授告诉我使用单个向量作为棋盘,其中向量的第 i 个元素代表棋盘的第 i 列。该元素的值是皇后所在的行,如果该列为空,则为 -1。所以,[0 1 2 -1 -1] 有两列没有皇后,三个皇后被非法放置。 当我运行此代码时: (place-n-queens 0 4 #(-1 -1 -1 -1)) 我得到 #(0 1 2 3) 显然所有四个皇后都是非法放置的。我认为问题在于我没有在 cond in place-queen-on-n 检查足够的东西,但我不确定要添加什么来解决在同一对角线上获得皇后的问题。

(define (return-row vector queen) 
  (vector-ref vector (return-col vector queen)))
(define (return-col vector queen) 
  (remainder queen (vector-length vector)))

(define (checkrow vector nq oq) 
  (cond
   ((= (vector-ref vector nq) -1) #f)
   ((= (vector-ref vector oq) -1) #f)
   (else (= (return-row vector nq) (return-row vector oq)))))
(define (checkcol vector nq oq) 
  (= (return-col vector nq) (return-col vector oq)))
(define (checkdiagonal vector nq oq)
  (cond 
    ((= (vector-ref vector nq) -1) #f)
    ((= (vector-ref vector oq) -1) #f)
    (else (= (abs (- (return-row vector nq) (return-row vector oq)))
      (abs (- (return-col vector nq) (return-col vector oq)))))))

(define (checkdiagonalagain vector r c oq)
   (= (abs (- r (return-row vector oq)))
    (abs (- c (return-col vector oq)))) )
(define (checkrowagain vector r oq)
   (= r (return-row vector oq)))

(define (checkinterference vector nq oq)
   (or (checkrow vector nq oq) (checkcol vector nq oq) (checkdiagonal vector nq oq)))

(define (place-queen-on-n vector r c)
 (local ((define (foo x)
        (cond
          ((checkrowagain vector r x) -1)            
          ((= c x) r)
          ((checkinterference vector c x) -1)
          ((map (lambda (y) (eq? (vector-ref vector x) y)) 
                (build-list (vector-length vector) values)) (vector-ref vector x))
          ((eq? (vector-ref vector x) -1) -1)
          (else -1))))
 (build-vector (vector-length vector) foo)))

(define (place-a-queen vector)
 (local ((define (place-queen collist rowlist)
        (cond
          ((empty? collist) '())
          ((empty? rowlist) '())
          (else (append (map (lambda (x) (place-queen-on-n vector x (car collist))) rowlist)
                        (try vector (cdr collist) rowlist)))
          )))
 (place-queen (get-possible-col vector) (get-possible-row (vector->list vector) vector))))

(define (try vector collist rowlist)
 (cond
  ((empty? collist) '())
          ((empty? rowlist) '())
 (else (append (map (lambda (x) (place-queen-on-n vector x (car collist))) rowlist)
    (try vector (cdr collist) rowlist)))))

(define (get-possible-col vector)
 (local ((define (get-ava index)
        (cond
          ((= index (vector-length vector)) '())
          ((eq? (vector-ref vector index) -1)
           (cons index (get-ava (add1 index))))
          (else (get-ava (add1 index))))))
  (get-ava 0)))

;list is just vector turned into a list
(define (get-possible-row list vector)
  (filter positive? list)
  (define (thislist) (build-list (vector-length vector) values))
  (remove* list (build-list (vector-length vector) values))
)

(define (place-n-queens origination destination vector)
 (cond
  ((= origination destination) vector)
  (else (local ((define possible-steps
                (place-n-queens/list (add1 origination)
                                     destination
                                     (place-a-queen vector))))
        (cond
          ((boolean? possible-steps) #f)
          (else possible-steps))))))

(define (place-n-queens/list origination destination boards)
 (cond
  ((empty? boards) #f)
  (else (local ((define possible-steps 
                (place-n-queens origination destination (car boards))))         
        (cond
          ((boolean? possible-steps) (place-n-queens/list origination destination (cdr boards)))
          (else possible-steps))
        ))))

感谢任何帮助以使其正常工作!

【问题讨论】:

【参考方案1】:

这很难理解。通常 n-queens 是通过某种回溯完成的,我没有看到你在哪里回溯。困难的部分是管理使用向量时的副作用。在返回之前,您必须将板设置为先前的状态。

(define (n-queens size)
 (let ((board (make-vector size -1)))
   (let loop ((col 0) (row 0))
     (cond ((= col size) board)
           ((= row size)    ;;dead end
            (if (= col 0)   ;;if first collumn
                #f          ;;then no solutions
                (begin (vector-set! board (- col 1) -1))
                       #f)))            
                  ;;else undo changes made by previous level and signal the error
           ((safe? col row board) 
            (vector-set! board col row) 
            (or (loop (+ col 1) 0) 
                   ;;only precede to next column if a safe position is found
                (loop col (+ row 1))))
                   ;; keep going if hit a dead end. 
           (else (loop col (+ row 1)))))))

写作安全吗?不过,这取决于你。

也不确定您为什么要从矢量移动到列表。它只是真的阻塞了逻辑,所以我很难理解。另外,您应该能够自在地通过向量自己移动。在place-queen-on-n 中,您在向量上使用构建列表,以便您可以映射它。 而某种矢量折叠可能更合适。此外,该映射将始终返回一个始终不为假的列表,这意味着 cond 中的任何代码都不会被命中。是不是你的问题,我不知道,但这是个问题。

【讨论】:

以上是关于如何解决方案中的 n-queens的主要内容,如果未能解决你的问题,请参考以下文章

Scheme中如何解决N-Queens?

n-Queen 算法的所有可能解

解决 n-queen 谜题

解决N-Queens问题......我们能走多远?

N-Queens 谜题,但包含所有棋子

LeetCode 52.N-Queens II