3 ms·
"...all N-Queen problem comes to mere 8 lines of code" If you are optimizing in LOC, it can come down to basically a single expression; of course that's not a
by reinhardt 15y ago
"...all N-Queen problem comes to mere 8 lines of code"
If you are optimizing in LOC, it can come down to basically a single expression; of course that's not a reason to write it as a one-liner:
def n_queen(n):
return (p for p in itertools.permutations(xrange(n))
if all(j-i != abs(p[i] - p[j])
for i in xrange(n - 1)
for j in xrange(i + 1, n)))
- beagle3 15y agoIf you are optimizing for LOC, use a suitable language: from http://nsl.com/papers/qn.htm http://nsl.com/papers/qn.htm , the code that computes the solution is qn:{[n],/{:[n=#*x;,*x;,/_f'f x,\:/:(!n)_dvl,/x]}'(f:0 -1 1+/:)@!n} And it uses backtracking, which makes it infinitely faster. But armin isn't optimizing LOC.