Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
#Snapshot of exact code at the end of the test.
#I wanted to first find all solutions to place N queen such no two queens threaten each other
#From that, I can then find a max threat
#Hardcoding due to insufficient time
N = 4
#{3,1,4, 1}
board = [
[0, 0, 1, 0],
[1, 0, 0, 0],
[0, 0, 0, 1],
[0, 1, 0, 0],
def solve_until(board, col):
Recurse function to solve the problem using backtracking. Not a good solution for big N
if col >= 4:
return True
for i in xrange(N):
if is_safe(board, i, col):
board[i][col]= 1;
if solve_until(board, col+1):
return true;
return False
def is_safe(board, row, col):
Check is it safe to put queen on board. INCOMPLETE
for i in xrange(col):
return bool(board[row][i])
for i in xrange(row, N):
for j in xrange(col, 0, -1):
return bool(board[i][j])
return True
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.