Skip to content

Instantly share code, notes, and snippets.

@cggonzal
Created January 6, 2019 19:55
Show Gist options
  • Save cggonzal/ed1114a0048bba86103da5dae60cc95c to your computer and use it in GitHub Desktop.
Save cggonzal/ed1114a0048bba86103da5dae60cc95c to your computer and use it in GitHub Desktop.
# 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