Skip to content

Instantly share code, notes, and snippets.

@GoWind
Created December 31, 2013 18:30
Show Gist options
  • Save GoWind/8200590 to your computer and use it in GitHub Desktop.
Save GoWind/8200590 to your computer and use it in GitHub Desktop.
a simple program for solving the nqueens problem using Python.
import sys
solutions = []
def place(positions,col):
if positions is None:
return True
current_row = len(positions)
if col in positions:
return False
#diagonal
d = 1
for prev_row in range(current_row-1,-1,-1):
if positions[prev_row] == col - d :
return False
d += 1
#off-diagonal
d = 1
for prev_row in range(current_row-1,-1,-1):
if positions[prev_row] == col + d:
return False
d += 1
return True
def solve_nqueens(positions,row,max_queens):
global solutions
if(row == max_queens):
solutions.append(positions)
return True
for col in range(0,max_queens):
if place(positions,col) :
Y = positions[:]
Y.append(col)
solve_nqueens(Y,row+1,max_queens)
else:
continue
return False
if __name__ == '__main__':
X = []
queens = int(sys.argv[1])
solve_nqueens(X,0,queens);
print solutions
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment