I have tried solving this problem with backtracking and it prints all possible solutions.
Two questions came up:
1.Can i implement n queen using other techniques?
2.Is it possible to make the code below print only the first solution and then terminate?
My current code uses backtracking:
n = 8
x = [-1 for x in range(n)]
def safe(k,i):
for j in xrange(k):
if x[j] == i or abs(x[j] - i) == abs(k - j) :
return False
return True
def nqueen(k):
for i in xrange(n):
if safe(k,i):
x[k] = i
if k == n-1:
print "SOLUTION", x
else:
nqueen(k+1)
nqueen(0)
Note: I am interested in techniques that do not depend on a particular programming language.
According to Wikipedia, you can do using heuristics:
This heuristic solves N queens for any N ≥ 4. It forms the list of numbers for vertical positions (rows) of queens with horizontal position (column) simply increasing. N is 8 for eight queens puzzle.
This heuristics is O(n)
since it's just printing the result after some if
statements.
Regarding your second question: "Is it possible to make code below to print only first solution and then terminate?"
You can just call sys.exit(0)
after you print:
import sys
n = 8
x = [-1 for x in range(n)]
def safe(k,i):
for j in xrange(k):
if x[j] == i or abs(x[j] - i) == abs(k - j) :
return False
return True
def nqueen(k):
for i in xrange(n):
if safe(k,i):
x[k] = i
if k == n-1:
print "SOLUTION", x
sys.exit(0)
else:
nqueen(k+1)
nqueen(0)
or, alternatively you can return a value and then propagate the value if it indicates termination:
n = 8
x = [-1 for x in range(n)]
def safe(k,i):
for j in xrange(k):
if x[j] == i or abs(x[j] - i) == abs(k - j) :
return False
return True
def nqueen(k):
for i in xrange(n):
if safe(k,i):
x[k] = i
if k == n-1:
print "SOLUTION", x
return True # Found a solution, send this good news!
else:
if nqueen(k+1): # Good news!
return True # Share the good news to parent!
return False # We have searched every possible combinations, and I regret to tell you that I could not find any solution.
nqueen(0)
As for the time complexity, since it's a complete search, it's n^n
. Although due to the pruning (using safe(k,i)
), in practice it's a lot faster.