Created
January 6, 2019 19:55
-
-
Save cggonzal/ed1114a0048bba86103da5dae60cc95c to your computer and use it in GitHub Desktop.
This file contains 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
# min conflicts solver for NQueens problems | |
from nqueens import * | |
n = 8 | |
NQ = NQueens(n) | |
timer = 0 | |
while(not NQ.allQueensSafe()): | |
minAttacks = n + 1 # n + 1 is greater than any possibility of attacks so this is guaranteed to get minimized | |
pickedQueen = NQ.pickRandomQueen() | |
positions = NQ.availablePositions(pickedQueen) | |
minConflictPosition = (-1,-1) | |
for pos in positions: # iterate through all positions of pickedQueen and move to position of minimum conflict | |
NQ.moveQueen(pickedQueen,pos) | |
newNumberOfConflicts = NQ.specificQueenConflicts(pos) | |
if(newNumberOfConflicts < minAttacks ): | |
minConflictPosition = pos | |
minAttacks = newNumberOfConflicts | |
NQ.moveQueen(pos,pickedQueen) # move queen back | |
NQ.moveQueen(pickedQueen,minConflictPosition)# move queen to least conflict spot | |
print(NQ.printBoard()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment