-1

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.

  • 1
    A brief look at this shows that your indentation is incorrect. As you know, in python indentation influences the meaning of the code. Please first correct this. – trincot Jan 20 '19 at 10:12
  • Hi, I formatted this in a separate app (CodeBeautify) because when i placed it in the text it didnt paste as a block of code. I will manually re-indent now. Apologies. – philcode101 Jan 20 '19 at 10:27
  • There is no recursion in `complete`. It optimistically performs 8 iterations, sometimes adding an index, sometimes removing it. So at the end you'll not have 8 queens. The backtracking and recurring is missing from your algorithm. I would suggest to look into other solutions (that abound on the internet) and pick up how recursion/backtracking is used. Even if they would use a 2D representation it is still key to the solution with 1D as well. – trincot Jan 20 '19 at 10:39

1 Answers1

1

If you are changing global variable from a function you need to mark it as global, otherwise, function will treat that variable as local. That is what you get. You get empty queens list in your complete function. Please notice you can reference global (like you do it in printQ and solve functions, but if you going to change it (like you have in your complete function) you have to mark it as global. Here is an example of how to do it. Also you don't need to mark it as global on module level (first line in your code) it has to be on a function level

def complete():
    global queens
    for i in range(8):
        if solve(i) == True:
            queens += [i]
        elif solve(i) == False:
            queens = queens - [i]
        else:
            return
Vlad Bezden
  • 83,883
  • 25
  • 248
  • 179