Below is my implementation of PYTHON. You can use PYPY for more speed.
Its speed helps with the O (1) time method to check whether the next queen is attacked by those already on the board.
Assuming the program is "nqueen.py", an example of its launch is "python nqueen.py 6", where 6 is the size of the 6x6 board.
#!/bin/python # # TH @stackoverflow, 2016-01-20, "N Queens" with an O(1) time method to check whether the next queen is attacked # import sys board_size = int(sys.argv[1]) Attacked_H = { i:0 for i in range(0, board_size) } Attacked_DU = { i:0 for i in range(0, board_size*2) } Attacked_DD = { i:0 for i in range(-board_size, board_size) } def nqueen(B, N, row, col): if(row >= N): return 1 if(col >= N): print "board:\n" + str.join("\n", ["_|"*q + "Q|" + "_|"*(board_size - q - 1) for q in B]) return 0 B[col] = row if(0==(Attacked_H[row] + Attacked_DU[row+col] + Attacked_DD[row-col])): [Attacked_H[row], Attacked_DU[row+col], Attacked_DD[row-col]] = [ 1,1,1 ] nqueen(B, N, 0, col + 1) [Attacked_H[row], Attacked_DU[row+col], Attacked_DD[row-col]] = [ 0,0,0 ] nqueen(B, N, row + 1, col) nqueen(list(range(0, board_size)), board_size, 0, 0)
source share