Skip to content

Instantly share code, notes, and snippets.

@yuvipanda
Created April 20, 2010 12:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save yuvipanda/372380 to your computer and use it in GitHub Desktop.
Save yuvipanda/372380 to your computer and use it in GitHub Desktop.
from math import *
import sys
x = {}
n = int(sys.argv[1])
def place(k, i):
if (i in x.values()):
return False
j = 1
while(j < k):
if abs(x[j]-i) == abs(j-k):
return False
j+=1
return True
def NQueens(k):
for i in range(1, n + 1):
if place(k, i):
x[k] = i
if (k==n):
for j in x:
print x[j]
else:
NQueens(k+1)
NQueens(1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment