tasks a-s
This commit is contained in:
parent
47bf37c096
commit
b83f433714
12433 changed files with 156208 additions and 123 deletions
22
Task/N-queens-problem/Common-Lisp/n-queens-problem.lisp
Normal file
22
Task/N-queens-problem/Common-Lisp/n-queens-problem.lisp
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
(defun n-queens (n m)
|
||||
(if (= n 1)
|
||||
(loop for x from 1 to m collect (list x))
|
||||
(loop for sol in (n-queens (1- n) m) nconc
|
||||
(loop for col from 1 to m when
|
||||
(loop for row from 0 to (length sol) for c in sol
|
||||
always (and (/= col c)
|
||||
(/= (abs (- c col)) (1+ row)))
|
||||
finally (return (cons col sol)))
|
||||
collect it))))
|
||||
|
||||
(defun show-solution (b n)
|
||||
(loop for i in b do
|
||||
(format t "~{~A~^~}~%"
|
||||
(loop for x from 1 to n collect (if (= x i) "Q " ". "))))
|
||||
(terpri))
|
||||
|
||||
(let ((i 0) (n 8))
|
||||
(mapc #'(lambda (s)
|
||||
(format t "Solution ~a:~%" (incf i))
|
||||
(show-solution s n))
|
||||
(n-queens n n)))
|
||||
Loading…
Add table
Add a link
Reference in a new issue