I know N_Queens is a very well covered topic, but i've to find a good python solution which uses a 1D array (and importantly, solves via filling the 1d array and then via function transforms this into a 2d array).
I've got so far as completing the (what I think i made far too complex) print and check function, but I seem to be unable to complete the solve function.
global queens
queens = []
def printQ():
for i in queens:
for row in range(8):
if row == i:
print(" Q",end="")
else:
print(" .",end="")
print(" ")
def solve(x):
step = len(queens)
for indices in range(step):
if (x == queens[indices]):
return False
for i in range(step,0,-1):
if (x == queens[0-i]-i) or (x == queens[0-i]+i):
return False
else:
return True
def complete():
for i in range(8):
if solve(i) == True:
queens += [i]
elif solve(i) == False:
queens = queens - [i]
else:
return
Im trying to iterate through a list and place elements in global [queens] if possible. Each time I try to alter complete(), i either get an empty list or an error. I am trying to solve this via backtracking, but i'm unsure how exactly my psuedo-code should look for complete()
Any advice would be greatly appreciated, and apologies if my code isn't high-quality, very much still a beginner.
note: To prove both functions work, simply add items to the array [1,4,2] for example.