Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#!/usr/bin/env python
# Infinite queens
# Prints the lex-minimal infinite nonattacking queen placement
# in the first quadrant - amazingly, they form two lines with slopes
# phi and 1/phi
import sys
N = int(sys.argv[1])
queens = []
def attacking(q,r):
if q[0] == r[0]:
return True
if q[1] == r[1]:
return True
if (q[0] - r[0]) == (q[1] - r[1]):
return True
if (q[0] - r[0]) == (r[1] - q[1]):
return True
return False
for i in xrange(N):
j = 0
while any(attacking(q, (i,j)) for q in queens):
j += 1
queens.append((i,j))
for q in reversed(queens):
print ("-" * q[1]) + "Q" + "-" * (int(1.7*N) - q[1]) + "%.2f" % ((1.0 + q[1]) / (1.0 + q[0]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.