Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
19
Task/N-queens-problem/Common-Lisp/n-queens-problem-1.lisp
Normal file
19
Task/N-queens-problem/Common-Lisp/n-queens-problem-1.lisp
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
(defun queens (n &optional (m n))
|
||||
(if (zerop n)
|
||||
(list nil)
|
||||
(loop for solution in (queens (1- n) m)
|
||||
nconc (loop for new-col from 1 to m
|
||||
when (loop for row from 1 to n
|
||||
for col in solution
|
||||
always (/= new-col col (+ col row) (- col row)))
|
||||
collect (cons new-col solution)))))
|
||||
|
||||
(defun print-solution (solution)
|
||||
(loop for queen-col in solution
|
||||
do (loop for col from 1 to (length solution)
|
||||
do (write-char (if (= col queen-col) #\Q #\.)))
|
||||
(terpri))
|
||||
(terpri))
|
||||
|
||||
(defun print-queens (n)
|
||||
(mapc #'print-solution (queens n)))
|
||||
40
Task/N-queens-problem/Common-Lisp/n-queens-problem-2.lisp
Normal file
40
Task/N-queens-problem/Common-Lisp/n-queens-problem-2.lisp
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
(defun queens1 (n)
|
||||
(let ((a (make-array n))
|
||||
(s (make-array n))
|
||||
(u (make-array (list (- (* 4 n) 2)) :initial-element t))
|
||||
y z (i 0) j p q (r (1- (* 2 n))) (m 0))
|
||||
(dotimes (i n) (setf (aref a i) i))
|
||||
(tagbody
|
||||
L1
|
||||
(if (>= i n) (go L5))
|
||||
(setf j i)
|
||||
L2
|
||||
(setf y (aref a j) z (aref a i))
|
||||
(setf p (+ (- i y) n -1) q (+ i y))
|
||||
(setf (aref a i) y (aref a j) z)
|
||||
(when (and (aref u p) (aref u (+ q r)))
|
||||
(setf (aref s i) j (aref u p) nil (aref u (+ q r)) nil)
|
||||
(incf i)
|
||||
(go L1))
|
||||
L3
|
||||
(incf j)
|
||||
(if (< j n) (go L2))
|
||||
L4
|
||||
(decf j)
|
||||
(if (= j i) (go L6))
|
||||
(rotatef (aref a i) (aref a j))
|
||||
(go L4)
|
||||
L5
|
||||
(incf m)
|
||||
L6
|
||||
(decf i)
|
||||
(if (minusp i) (go L7))
|
||||
(setf p (+ (- i (aref a i)) n -1) q (+ i (aref a i)) j (aref s i))
|
||||
(setf (aref u p) t (aref u (+ q r)) t)
|
||||
(go L3)
|
||||
L7)
|
||||
m))
|
||||
|
||||
> (loop for n from 1 to 14 collect (cons n (queens1 n)))
|
||||
((1 . 1) (2 . 0) (3 . 0) (4 . 2) (5 . 10) (6 . 4) (7 . 40) (8 . 92) (9 . 352)
|
||||
(10 . 724) (11 . 2680) (12 . 14200) (13 . 73712) (14 . 365596))
|
||||
21
Task/N-queens-problem/Common-Lisp/n-queens-problem-3.lisp
Normal file
21
Task/N-queens-problem/Common-Lisp/n-queens-problem-3.lisp
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
(defun queens2 (n)
|
||||
(let ((a (make-array n))
|
||||
(u (make-array (+ n n -1) :initial-element t))
|
||||
(v (make-array (+ n n -1) :initial-element t))
|
||||
(m 0))
|
||||
(dotimes (i n) (setf (aref a i) i))
|
||||
(labels ((sub (i)
|
||||
(if (= i n)
|
||||
;(push (copy-seq a) s)
|
||||
(incf m)
|
||||
(loop for k from i below n do
|
||||
(let ((p (+ i (aref a k)))
|
||||
(q (+ (- i (aref a k)) n -1)))
|
||||
(when (and (aref u p) (aref v q))
|
||||
(setf (aref u p) nil (aref v q) nil)
|
||||
(rotatef (aref a i) (aref a k))
|
||||
(sub (1+ i))
|
||||
(setf (aref u p) t (aref v q) t)
|
||||
(rotatef (aref a i) (aref a k))))))))
|
||||
(sub 0))
|
||||
m))
|
||||
Loading…
Add table
Add a link
Reference in a new issue