1 ! Copyright (c) 2012 Anonymous
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: kernel sequences math math.combinatorics formatting io locals ;
4 IN: rosetta-code.n-queens
6 ! http://rosettacode.org/wiki/N-queens_problem
8 ! Solve the eight queens puzzle. You can extend the problem to
9 ! solve the puzzle with a board of side NxN.
11 :: safe? ( board q -- ? )
22 : solution? ( board -- ? )
23 dup length <iota> [ dupd safe? ] all? nip ;
26 <iota> all-permutations [ solution? ] filter ;
29 queens [ [ 1 + "%d " printf ] each nl ] each ;