Created
December 31, 2013 18:30
-
-
Save GoWind/8200590 to your computer and use it in GitHub Desktop.
a simple program for solving the nqueens problem using 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
| 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