Skip to content

Instantly share code, notes, and snippets.

@samclearman
Last active December 25, 2017 20:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save samclearman/64fd5f4d9b1c20e14f64662d88d1c425 to your computer and use it in GitHub Desktop.
Save samclearman/64fd5f4d9b1c20e14f64662d88d1c425 to your computer and use it in GitHub Desktop.
#!/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