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
n = 8 | |
NQ = NQueens(n) |
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
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) |
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
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 |
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) |
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
import random | |
class NQueens: | |
def __init__(self,n): | |
self.board, self.queenPositions = self.getNewBoard(n) | |
self.n = n | |
def getNewBoard(self,n): | |
# queens are represented as ones in 2d list of all zeros | |
# Since it's a 2d list, each element is a row of zeros except for the queen | |
board = [] |
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
print(NQ.printBoard()) |
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
def allQueensSafe(self): # returns true if problem is solved and all queens safe, false otherwise | |
for pos in self.queenPositions: | |
if(self.UnderAttack(pos)): | |
return False | |
return True |
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
def UnderAttack(self,position): | |
if(self.attackViaDiagonal(position)): | |
return True | |
if(self.attackViaRow(position)): | |
return True | |
if(self.attackViaCol(position)): | |
return True |
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
def attackViaCol(self,pos): | |
for queen in self.queenPositions: | |
if(pos[1] == queen[1] and queen != pos): # last inqueality checks to make sure you arent comparing the same queen | |
return True | |
return False | |
def attackViaRow(self,pos): | |
for queen in self.queenPositions: | |
if(pos[0] == queen[0] and queen != pos): | |
return True |
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
import random | |
class NQueens: | |
def __init__(self,n): | |
self.board, self.queenPositions = self.getNewBoard(n) | |
self.n = n | |
def getNewBoard(self,n): | |
# queens are represented as ones in 2d list of all zeros | |
# Since it's a 2d list, each element is a row of zeros except for the queen | |
board = [] |
OlderNewer