Eight queens
def queens(n, board=[], plus_diag=[], minus_diag=[]):
return [board] if len(board) == n else [b for i in range(n) if i not in board and i + len(board) not in plus_diag and i - len(board) not in minus_diag for b in queens(n, board + [i], plus_diag + [i + len(board)], minus_diag + [i - len(board)])]
print(queens(8))
One-line python function that outputs all possible ways to place eight queens on an 8x8 chessboard such that no queen can attack any other. (There’s 92 ways to do this for 8x8)
Below is one such solution chosen at random alongside its corresponding intlist. Each queen’s rank and file is given by value and index respectively.