Groovy
n=8
s=(1..n).permutations().findAll{
def x=0,y=0
Set a=it.collect{it-x++}
Set b=it.collect{it+y++}
a.size()==it.size()&&b.size()==it.size()
}
Delivers a list of all queen solutions like this:
[ [4, 7, 3, 0, 6, 1, 5, 2],
[6, 2, 7, 1, 4, 0, 5, 3],
... ]
For graphical representation add:
s.each { def size = it.size()
it.each { (it-1).times { print "|_" }
print "|Q"
(size-it).times { print "|_" }
println "|"
}
println ""
}
which looks like this:
|_|Q|_|_|_|_|_|_|
|_|_|_|Q|_|_|_|_|
|_|_|_|_|_|Q|_|_|
|_|_|_|_|_|_|_|Q|
|_|_|Q|_|_|_|_|_|
|Q|_|_|_|_|_|_|_|
|_|_|_|_|_|_|Q|_|
|_|_|_|_|Q|_|_|_|
A000170 – Peter Taylor – 2013-04-12T15:52:40.427
There is no solution for case: N = 2,3. The wikipedia has a excellent write up about this classic problem. It documents ver well about the solution number from N = 1 to N = 14. (I am still new to Code Golf. Not sure what is the best way to participate yet. :)) – Dongshengcn – 2011-07-04T00:01:32.943
Do we need to handle 2 <= N <= 4 cases? If so how? – st0le – 2011-02-02T12:09:20.870