
Recently, In Chinses Visual Lisp forum. There also exist some Challenges. This is the fourth Challenge problem.
http://bbs.mjtd.com/forum.php?mod=viewthread&tid=90628&page=1#pid493821I write this following code, it find all the solutions of n queens. certainly this code is not as good as Lee Mac's and Evgeniy's.
;;;; n queen solutions
(defun q:queen (n lst / i)
(setq i 0)
(repeat qn
(if (not (q:queen:check i lst))
(if (= n (1- qn))
(setq final (cons (reverse (cons i lst)) final))
(q:queen (1+ n) (cons i lst)))
)
(setq i (1+ i))
)
)
;;;;position check
(defun q:queen:check (n1 lst1 / res x j)
(setq j 1)
(foreach x lst1
(setq res (append res (list (- x j) x (+ x j)))j (1+ j))
)
(member n1 (vl-sort res '<))
)
;;;;Main solution
(defun c:test(/ final qn)
(setq qn 8)
(q:queen 0 nil)
(foreach x (reverse final) (princ "\n") (princ x))
(princ)
)
(princ "\n By qjchen@gmail.com, n Queens solution,The command is test")
(princ)
This following codes is to remove the same construction solutions and get the 12 unique solutions in 8 queens.
;;;; n queen solutions
(defun q:queen (n lst / i)
(setq i 0)
(repeat qn
(if (not (q:queen:check i lst))
(if (= n (1- qn))
(setq final (cons (reverse (cons i lst)) final))
(q:queen (1+ n) (cons i lst)))
)
(setq i (1+ i))
)
)
;;;;position check
(defun q:queen:check (n1 lst1 / res x j)
(setq j 1)
(foreach x lst1
(setq res (append res (list (- x j) x (+ x j)))j (1+ j))
)
(member n1 (vl-sort res '<))
)
;;;;inversion of queen lst
(defun q:queen:inv(lst / res)
(setq i 0)
(repeat (length lst) (setq res (cons (vl-position i lst) res) i (1+ i)))
(reverse res)
)
;;;;mirror of y axis of queen lst
(defun q:queen:mirrory(lst)
(mapcar '(lambda(x) (- (length lst) 1 x)) lst)
)
;;;;mirror of x axis of queen lst
(defun q:queen:mirrorx(lst) (reverse lst))
;;;;rotate 90 degreed of queen lst
(defun q:queen:rot90(lst) (q:queen:mirrory (q:queen:inv lst)))
;;;;all 9 same construction solutions
(defun q:queen:allsame(lst / res l1 l2 l3)
(setq l1 (q:queen:rot90 lst) res (cons (q:queen:mirrorx l1) (cons (q:queen:mirrory l1) (cons l1 res))))
(setq l2 (q:queen:rot90 l1) res (cons (q:queen:mirrorx l2) (cons (q:queen:mirrory l2) (cons l2 res))))
(setq l3 (q:queen:rot90 l2) res (cons (q:queen:mirrorx l1) (cons (q:queen:mirrory l3) (cons l3 res))))
res
)
;;;;deal the same construction solution
(defun q:queen:removeallsame(lst / res a1)
(while (car lst)
(setq res (cons (setq a1 (car lst)) res)
lst (q:list:removebfroma (cdr lst) (q:queen:allsame a1)))
)
res
)
;;;;remove lstb from lsta
(defun q:list:removebfroma(lsta lstb / x)
(foreach x lstb (setq lsta (vl-remove x lsta)))
)
;;;;Main solution
(defun c:test(/ final qn)
(setq qn (getint "\n Please input the number of queens (suggest to be less than 10):"))
(if (not qn) (setq qn 8))
(q:queen 0 nil)
(setq final (q:queen:removeallsame (reverse final)))
(foreach x (reverse final) (princ "\n") (princ x))
(princ)
)
(princ "\n By qjchen@gmail.com, n Queens solution,The command is test")
(princ)
BTW, in MJTD, the Chinese LISP forum.
The other recent three challenges are
1. How to Calculate PI to 10000 digits
http://bbs.mjtd.com/thread-90139-1-1.htmlIn this post, I write the code by Machin algorithm, but it is not so quick, and Highflybird write a very quick code.
2. To draw the 10000 paths of the light from one point to a curve (which represent the boundary between two different isotropic media) and then refract. (That means, you select a curve for boundary, a point for sun, then input the indices of refraction of the two medias, then draw 10000 paths)
http://bbs.mjtd.com/thread-90226-1-1.html3. The problem of Squaring the square
http://bbs.mjtd.com/thread-90547-1-1.htmlThis is a very difficult problem. I have still no ideas on it.
To Find the Lowest-order perfect squared square
http://en.wikipedia.org/wiki/Squaring_the_square
My dear friends in theSwamp, I hope you will also like these Challenges.