0% found this document useful (0 votes)
14 views2 pages

N Queen

Uploaded by

Khushi Kumari
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views2 pages

N Queen

Uploaded by

Khushi Kumari
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Python Source Code:

def solve_n_queens(n):
def is_safe(board, row, col):
for i in range(row):
if board[i][col] == 'Q':
return False
for i, j in zip(range(row, -1, -1), range(col, -1, -1)):
if board[i][j] == 'Q':
return False
for i, j in zip(range(row, -1, -1), range(col, n)):
if board[i][j] == 'Q':
return False
return True

def backtrack(board, row):


if row == n:
return board
for col in range(n):
if is_safe(board, row, col):
board[row][col] = 'Q'
result = backtrack(board, row + 1)
if result is not None:
return result
board[row][col] = '0'
return None

board = [['0' for _ in range(n)] for _ in range(n)]


result = backtrack(board, 0)
if result is None:
print("No solution exists for n = {}".format(n))
else:
for row in result:
print(' '.join(row).replace('0', '.').replace('Q', 'Q '))

n = int(input("Enter the value of n: "))


solve_n_queens(n)
Sample Output:
/Users/yatindesai/PycharmProjects/N_Queens/venv/bin/python
/Users/yatindesai/PycharmProjects/N_Queens/main.py
Enter the value of n: 27
Q . . . . . . . . . . . . . . . . . . . . . . . . . .
. . Q . . . . . . . . . . . . . . . . . . . . . . . .
. . . . Q . . . . . . . . . . . . . . . . . . . . . .
. Q . . . . . . . . . . . . . . . . . . . . . . . . .
. . . Q . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . Q . . . . . . . . . . . . . . . . . .
. . . . . . . . . . Q . . . . . . . . . . . . . . . .
. . . . . . . . . . . . Q . . . . . . . . . . . . . .
. . . . . . . . . . . . . . Q . . . . . . . . . . . .
. . . . . . . . . . . . . . . . Q . . . . . . . . . .
. . . . . . . . . . . . . . . . . . Q . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . Q . . . .
. . . . . . . . . . . . . . . . . . . . . . . . Q . .
. . . . . . . . . . . . . . . . . . . . . . . . . . Q
. . . . . . . . . . . . . . . . . . . . . . . Q . . .
. . . . . . . . . . . . . . . . . . . . . . . . . Q .
. . . . . Q . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . Q . . . . . . . . . . . . . . . . .
. . . . . . Q . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . Q . . . . . . . . . . .
. . . . . . . Q . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . Q . . . . . . . . . . . . . . .
. . . . . . . . . . . . . Q . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . Q . . . . . .
. . . . . . . . . . . . . . . . . Q . . . . . . . . .
. . . . . . . . . . . . . . . . . . . Q . . . . . . .
. . . . . . . . . . . . . . . . . . . . . Q . . . . .

Process finished with exit code 0

You might also like