N Queen code (python) only working for n as 1 or 5. Doesnt return anything for anything else

54 Views Asked by At

So I tried coding a program to solve NQueen problem in python. It is only outputing correct response for n as 1 or 5. It doesn't throw an error or anything for the rest of numbers.

Here is the code:

import numpy as np

def isSafe(arr, row, col, n):
    for ind in range(row):
        if(arr[ind][col] == 1):
            return False
    x = row
    y = col
    while x >= 0 and y >= 0:
        if arr[x][y] == 1:
            return False
        x -= 1
        y -= 1
    x = row
    y = col

    while x >= 0 and y < n:
        if arr[x][y] == 1:
            return False
        x -= 1
        y += 1
    
    return True

def nQueen(arr, row, n):
    if row >= n:
        return True
    for col in range(n):
        if isSafe(arr,row,col,n) == True:
            arr[row][col] = 1
            if nQueen(arr, row+1, n) == True:
                return True
    
    return False

def main():
    n = int(input("Enter the size of board: "))
    arr = np.zeros((n,n)).astype(int)
    if nQueen(arr, 0, n):
        for x in range(n):
            for y in range(n):
                print(arr[x][y], end='\t')
            print("\n")
    else:
        print("No Solution: ")

if __name__ == '__main__':
    main()\


Using Lists:
import numpy as np

def isSafe(arr, row, col, n):
    for ind in range(row):
        if(arr[ind][col] == 1):
            return False
    x = row
    y = col
    while x >= 0 and y >= 0:
        if arr[x][y] == 1:
            return False
        x -= 1
        y -= 1
    x = row
    y = col

    while x >= 0 and y < n:
        if arr[x][y] == 1:
            return False
        x -= 1
        y += 1
    
    return True

def nQueen(arr, row, n):
    if row >= n:
        return True
    for col in range(n):
        if isSafe(arr,row,col,n) == True:
            arr[row][col] = 1
            if nQueen(arr, row+1, n) == True:
                return True
    
    return False

def main():
    n = int(input("Enter the size of board: "))
    arr = [[0]*n]*n
    if nQueen(arr, 0, n):
        for x in range(n):
            for y in range(n):
                print(arr[x][y], end='\t')
            print("\n")
    else:
        print("No Solution: ")

if __name__ == '__main__':
    main()

I tried using every single integer from 0 to 20 but only 1 and 5 seemed to output something while any other number just outputted "No Solution".

Also, The lists one doesnt even output an answer for 5, just 1

1

There are 1 best solutions below

0
chepner On

When you determine that an assignment arr[row][col] is not safe, you never reset it for future tests.

def nQueen(arr, row, n):
    if row >= n:
        return True
    for col in range(n):
        if isSafe(arr,row,col,n) == True:
            arr[row][col] = 1  # Put a queen here

            if nQueen(arr, row+1, n) == True:
                return True

            arr[row][col] = 0  # Take it back; it didn't work here
    
    return False