Last active
September 14, 2015 02:06
-
-
Save Syerram/99e8f4b8576fa131ac93 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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; | |
board[i][col]=0 | |
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