Monday, September 12, 2011

Eight queen problem in one line of Python

Write a one line python code (excluding any imports) that prints all the solutions of the 8-queen problem.

1 comment:

Kundan Singh said...

>>> import itertools
>>> print "\n\n".join([("\n".join([' '.join([('x' if i==x else '.') for i in xrange(8)]) for x in v])) for v in itertools.permutations(xrange(8)) if 8 == len(set(v[i]+i for i in xrange(8))) == len(set(v[i]-i for i in xrange(8)))])