Skip to content

Instantly share code, notes, and snippets.

@TheSeamau5
Created September 25, 2016 23:41
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 TheSeamau5/a440698adaf55dd1452f5048dc4c9d50 to your computer and use it in GitHub Desktop.
Save TheSeamau5/a440698adaf55dd1452f5048dc4c9d50 to your computer and use it in GitHub Desktop.
N Queens in Python
#!usr/bin/python
from itertools import permutations
def n_queens(n):
cols = range(n)
for vec in permutations(cols):
if n == len(set(vec[i] + i for i in cols)) and n == len(set(vec[i] - i for i in cols)):
print("\n".join('.' * i + 'Q' + '.' * (n - i - 1) for i in vec) + "\n===\n")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment