Lecture Notes on 24 Feb 2014 This is the classic 8 Queens problem - how to place 8 Queens on a chess board such that no two Queens can capture each other. class Queens (object): # initialize the board def __init__ (self, n = 8): self.board = [] self.n = n for i in range (self.n): row = [] for j in range (self.n): row.append ('*') self.board.append (row) # check if no queen captures another def is_valid (self, row, col): for i in range (self.n): if (self.board[row][i] == 'Q' or self.board[i][col] == 'Q'): return False for i in range (self.n): for j in range (self.n): row_diff = abs (row - i) col_diff = abs (col - j) if (row_diff == col_diff) and (self.board[i][j] == 'Q'): return False return True # do a recursive solve on the problem def recursive_solve (self, col): if (col == self.n): return True else: for i in range (self.n): if (self.is_valid (i, col)): self.board[i][col] = 'Q' if (self.recursive_solve (col + 1)): return True self.board[i][col] = '*' return False # solve the problem def solve (self): for i in range (self.n): if (self.recursive_solve (i)): self.print_board() # print the board def print_board (self): for i in range (self.n): for j in range (self.n): print (self.board[i][j], end = ' ') print () def main (): # create object and solve queens = Queens (8) queens.solve() main() * Modify the program so that it prints 'No valid solution' where there is none, like a 3 x 3 board. * Modify the program so that it prints all possible solutions. (There are 92 such solutions.) * Modify the program so that it prints all unique solutions. (There are 12 unique solutions.) * What is the maximum number of queens that can be placed on a chess board such that all squares are under the influence of one or more queens? Show the placement of the queens. * On a 8 x 8 board place 32 knights, 14 bishops, 16 kings, or 8 rooks so that no two pieces attack each other.