Eight queen problem
Task:
- Think of a chess field 8x8.
- Place 8 queens in a way that no one menaces another one.
Intention:
- Writing an algorithm to provide all solutions
- Adjusting the algorithm in a way that it can handle chess fields by other sizes n x n.
Other languages:
- My recipe 578497 for JavaScript
- ...
- If you would like avoiding the output of the solution then change the value of variable OUTPUT.
- It's a solution with recursion.
- For good background information and inspiration:
Further Notes:
- I have been trying to use a dictionary instead of a list; intention was to avoid the correction of the value for a diagonal (as done for ixDiag2) and to allocate as much space as needed only (avoid numberOfDiagonals). It's slower.
- Another intention has been to use a cell class which has the pre-calculated values so I need not calculate the positions while running the algorithm. The algorithm is then very small and readable but it's two times slower.
- One of my last changes have been:
- Using self.lastRow instead of self.with-1
- Using reduced columnRange on each next row; that's why I'm also using run now for starting the calculation. Positive effect: I don't need the one check for the locked columns and I can remove the reset for a locked column.
import time
OCCUPIED = 1 # field is in use
FREE = 0 # field is not used
OUTPUT = True # enable/disable of printing the solutions
# returning a container without the given value
def remove(container, value):
container.remove(value)
return container
class Queen:
def __init__(self, width = 8):
self.width = width
self.lastRow = self.width-1
# locked columns
self.columns = self.width * [-1]
# locked diagonals
numberOfDiagonals = 2 * self.width - 1
self.diagonals1 = numberOfDiagonals * [0]
self.diagonals2 = numberOfDiagonals * [0]
# list of solutions
self.solutions = []
# starts the search with initial parameters
def run(self):
self.calculate(row = 0, columnRange = list(range(self.width)))
# searches for all possible solutions
def calculate(self, row, columnRange):
for column in columnRange:
# relating diagonale '\' depending on current row and column
ixDiag1 = row + column
if self.diagonals1[ixDiag1] == OCCUPIED:
continue
# relating diagonale '/' depending on current row and column
ixDiag2 = self.width - 1 - row + column
# is one of the relating diagonals OCCUPIED by a queen?
if self.diagonals2[ixDiag2] == OCCUPIED:
continue
# occupying column and diagonals depending on current row and column
self.columns[column] = row
self.diagonals1[ixDiag1] = OCCUPIED
self.diagonals2[ixDiag2] = OCCUPIED
if row == self.lastRow:
# all queens have been placed - remembering solution!
self.solutions.append(self.columns[0:])
else:
# trying to place next queen...
self.calculate(row + 1, remove(columnRange[0:], column))
# Freeing column and diagonals depending on current row and column
self.diagonals1[ixDiag1] = FREE
self.diagonals2[ixDiag2] = FREE
def main():
instance = Queen(8)
print("Queen raster (%dx%d)" % (instance.width, instance.width))
Start = time.time()
instance.run()
print("... calculation took %f seconds" % (time.time() - Start))
print("... with %d solutions" % (len(instance.solutions)))
if OUTPUT:
# output of all solutions
for solution in instance.solutions:
line = ""
for ix in range(len(solution)):
line += "(%d,%d)" % (ix+1, solution[ix]+1)
print(line)
if __name__ == '__main__':
main()