N Queens by rolf

def q(l,n):
 if len(l)==n:print" ".join(map(str,l[::-1]));return
 for x in range(n):
  if c(x,l):q([x]+l,n)
def c(x,l):
  d=1
  for c in l:
   if c+d==x or c-d==x or c==x:return 0
   d+=1
  return 1
q([],input())

Note that non-ascii characters in the above source code will be escaped (such as \x9f).

download

return to the top page