0% found this document useful (0 votes)
2 views

Assignment 5 N-Queen problem - Jupyter Notebook.word

The document contains a Jupyter Notebook implementation of the N-Queen problem, which includes functions to check if a position is safe for placing a queen, solve the problem recursively, and print the solutions. The user is prompted to enter the size of the chessboard, and for an input of 8, the program finds 92 solutions. The notebook demonstrates the output of the solutions in a structured format.

Uploaded by

maneomkar2004
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Assignment 5 N-Queen problem - Jupyter Notebook.word

The document contains a Jupyter Notebook implementation of the N-Queen problem, which includes functions to check if a position is safe for placing a queen, solve the problem recursively, and print the solutions. The user is prompted to enter the size of the chessboard, and for an input of 8, the program finds 92 solutions. The notebook demonstrates the output of the solutions in a structured format.

Uploaded by

maneomkar2004
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

4/18/24, 11:28 AM Assignment 5 N-Queen problem - Jupyter Notebook

In [2]: def is_safe(board, row, col, n):


for i in range(col):
if board[row][i] == 1:
return False

for i, j in zip(range(row, -1, -1), range(col, -1, -1)):
if board[i][j] == 1:
return False

for i, j in zip(range(row, n), range(col, -1, -1)):
if board[i][j] == 1:
return False

return True

def solve_n_queens_util(board, col, n, solutions):
if col >= n:
solutions.append([row[:] for row in board])
return

for i in range(n):
if is_safe(board, i, col, n):
board[i][col] = 1
solve_n_queens_util(board, col + 1, n, solutions)
board[i][col] = 0

def solve_n_queens(n):
board = [[0 for _ in range(n)] for _ in range(n)]
solutions = []
solve_n_queens_util(board, 0, n, solutions)
return solutions

def print_solutions(solutions):
print(f"Number of solutions: {len(solutions)}")
for idx, solution in enumerate(solutions):
print(f"Solution {idx+1}:")
for row in solution:
print(" ".join(map(str, row)))
print()

while True:
try:
n = int(input("Enter the size of the chessboard (n): "))
if n <= 0:
raise ValueError
break
except ValueError:
print("Please enter a valid positive integer for board size.")

solutions = solve_n_queens(n)
print_solutions(solutions)



localhost:8888/notebooks/Assignment 5 N-Queen problem.ipynb 2/3


4/18/24, 11:28 AM Assignment 5 N-Queen problem - Jupyter Notebook

Enter the size of the chessboard (n): 8


Number of solutions: 92
Solution 1:
1 0 0 0 0 0 0 0
0 0 0 0 0 0 1 0
0 0 0 0 1 0 0 0
0 0 0 0 0 0 0 1
0 1 0 0 0 0 0 0
0 0 0 1 0 0 0 0
0 0 0 0 0 1 0 0
0 0 1 0 0 0 0 0

Solution 2:
1 0 0 0 0 0 0 0
0 0 0 0 0 0 1 0
0 0 0 1 0 0 0 0
0 0 0 0 0 1 0 0
0 0 0 0 0 0 0 1
0 1 0 0 0 0 0 0
0 0 0 0 1 0 0 0
In [ ]: ​

localhost:8888/notebooks/Assignment 5 N-Queen problem.ipynb 3/3

You might also like