Jump to content

N-queens problem: Difference between revisions

changed lang tag for Clojure
m (J: add comment)
(changed lang tag for Clojure)
Line 480:
=={{header|Clojure}}==
This produces all solutions by essentially a backtracking algorithm. The heart is the ''extends?'' function, which takes a partial solution for the first ''k<size'' columns and sees if the solution can be extended by adding a queen at row ''n'' of column ''k+1''. The ''extend'' function takes a list of all partial solutions for ''k'' columns and produces a list of all partial solutions for ''k+1'' columns. The final list ''solutions'' is calculated by starting with the list of 0-column solutions (obviously this is the list ''[ [] ]'', and iterates ''extend'' for ''size'' times.
<lang lispclojure>(def size 8)
 
(defn extends? [v n]
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.