Created
September 25, 2016 23:41
-
-
Save TheSeamau5/a440698adaf55dd1452f5048dc4c9d50 to your computer and use it in GitHub Desktop.
N Queens in Python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!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