-4

I have a problem with my program in python of n queens (how many possible ways are to place n queens in a nxn board). Seems like there's a problem with my recursion but I'm really helpless. Could someone figure out what's wrong?

def queens(N):

    ''' how many ways to place n queens on an NXN board? '''

    partial = []    # list representing partial placement of queens on the left columns
    return queens_rec(N,partial)

def queens_rec(N, partial):
    '''Given a partial solution to the n-Queens Problem ,
        return the number of options to place the rest of the queens.
        Recursively, in the end we will get the number of the options for
        the whole NxN board'''

    if len(partial)==N:
        return 1

    total = 0 #total of full solutions found
    row = 0

    while row<N:
        if isUnderAttack(partial,N,row)==False: #means it is not under Attack
            partial+=[row]

            total=total+queens_rec(N, partial)

            row+=1
            current = len(partial)

            partial = partial[0:current-1]

        else:
            row+=1

    return total

def isUnderAttack(partial, N, newRow):
    '''Checking if we can add a queen in row newRow, to the next column'''

    newCol = len(partial)

    for col in range(newCol): #not inculding newCol, checking all the previous columns
        oldRow = partial[col]


        #Checking horizontal attack from existing queen:
        if (newRow == oldRow):

            return True

        if (newCol - col == newRow - oldRow):

            return True

        if (newCol - col == oldRow - newRow):

            return True        

    return False
quamrana
  • 37,849
  • 12
  • 53
  • 71
CnR
  • 351
  • 1
  • 6
  • 15
  • I always get 0 solutions – CnR Dec 21 '13 at 17:50
  • Hello CnR, can you please be more specific? What are you expecting, and what is the error that you are getting? Have you tried stepping through your code with a debugger? putting break points? adding logging that would show you what is happening? You are getting 0 solutions because you haven't done all these basic things that would help you to solve the problem yourself... – user1725145 Dec 21 '13 at 17:52
  • why in queen_rec you return 1 if len(partial)==N? – Emanuele Paolini Dec 21 '13 at 17:53
  • I'm trying to figure out what's wrong for many hours, I did put print commands to see what's going on but deleted them for the convenience. – CnR Dec 21 '13 at 17:54
  • @manu-fatto becuase if we arranged the whole n queens in the board, it means we have a solution – CnR Dec 21 '13 at 17:54

1 Answers1

0

You wrote:

            partial+=[row]
            ...
            partial = partial[0:current-1]

the first command modifies the list partial in place. The second one makes a copy and leaves the original array untouched.

You should write:

partial.append(row) # this is equivalent to:  partial += [row]
...
partial.pop() # modifes list in place
Emanuele Paolini
  • 9,912
  • 3
  • 38
  • 64