Write a Python Program to implement the Backtracking approach to solve N Queen’s problem
The N-Queens problem is a classic example of a constraint satisfaction problem where you need to place N queens on an N x N chessboard so that no two queens can attack each other. This means no two queens can share the same row, column, or diagonal.
Backtracking Approach
The backtracking algorithm attempts to build a solution incrementally and abandons a partial solution as soon as it determines that it cannot be extended to a valid solution. For the N-Queens problem, this means placing queens one by one in different columns, and for each column, checking for conflicts.
Python Program
Python program that uses backtracking to solve the N-Queens problem:
def print_board(board):
"""Function to print the chessboard"""
for row in board:
print(" ".join("Q" if x else "." for x in row))
print()
def is_safe(board, row, col, N):
"""Check if it's safe to place a queen at board[row][col]"""
# Check this row on the left side
for i in range(col):
if board[row][i]:
return False
# Check upper diagonal on the left side
for i, j in zip(range(row, -1, -1), range(col, -1, -1)):
if board[i][j]:
return False
# Check lower diagonal on the left side
for i, j
_______ ____ ________ ________ _______ ________ ______ __________ ________ ___.
___ ____ _____ _____ _____.
______ __________ _______ ________ ____ _____ ___ ____ ___ ______.
_________ _____ __________ ________ _______.
________ ___ _______ ___ _________ ___.
__________ __________ _____ _____ _____ ____ ____ _________ ______ ___ _________ _____.
____ __________ __________ _______ ________ __________ ____ ______.
_________ _____ ___ ______ _________ ________ ____ _____.
______ ___ ______ __________ _______ ________ _________ ________ ______.
___ ____ ___ ________ ______ _________ ___ ____ ___ _____.
______ ________ __________ ______ _______ _______ ______ ______.
_____ _______ ___ _________ _______ _____ ______ ___.
_________ _______ ______ ____ ___ ______.
___ ___ _______ _______ _________ _____ ___ __________ ____ __________ ___ ______.
________ ____ __________ ____ ___ _________ ________ _________ _________ ________.
_________ __________ _____ ________ ____.
_______ _________ _____ _________ ____ _________ _______ __________.
______ __________ _____ _______ ___.
___ ___ ________ _________ _____ ____ ________ __________ ____ ___.
____ ____ __________ ___ _________.
_____ ___ _______ _____ _________ _____ _____ _____ _________ _______.
_____ ______ _______ _____ _________ _________ __________ ______ ______ _______ _____.
____ _________ ____ _________ _________ __________ _____ _____ _________.
_________ __________ __________ _____ _____ __________ _______ ______ _____ _____ ____ ___.
_______ _______ _______ _______ ______ ______ _______ _____.
____ _______ _______ ____ _________ ______ ______.
_____ ____ ___ ____ _______ _______ ______ ___.
________ _______ ___ _____ ____ ___ ________ ________ ____ ______ ________ _________.
___ ____ _________ _________ ____ _____ __________ _______ ______ _____.
______ ___ _____ ___ ______ _______ __________ ______ _____ __________.
_____ ___ ____ __________ ________ ________ _____ _________ ________ ______.
___ ___ ___ ____ __________ ____ __________.
_______ ___ _______ _______ __________ ___.
_________ __________ ________ ________ _______ ________ ___ _______ _____.
___ ________ _________ ___ ___.
_____ ___ _______ ________ _______ ______ __________ ___ _______ ______.
_________ ________ ______ ________ ______ _________ ________ _________ ______ ____ ________.
________ ________ ______ _____ __________ __________ _________ __________ ____ ______ ______ __________.
___ __________ ________ _________ ___ _________ ___ __________.
____ ___ ___ _______ ____ _______ _________ __________ _____ ____ ________.
______ ___ ____ __________ _______ ____ ___ __________ ____ ____ ____.
_______ ______ ___ __________ ___ _______ _______ ______ ________ ____.
_______ ___ ______ ________ __________ _____ ______ _________ ____ ____ ______ _________.
_____ _____ ________ _____ ______.
_______ _______ __________ ________ ____ ______ ____ ___ _____.
____ ______ ___ _____ ______ _______ _________ ________ ___.
___ _________ _____ _______ _________ ______ ______.
____ ___ _______ _________ ________ _______ ________.
____ ___ ______ _________ __________ _______.
_____ ________ ______ ____ _____ ___ ____.
__________ _________ ______ __________ ______ _________ ________ ___ ______ __________.
______ ___ _______ _______.
- Solved Assignments
- Study Notes
- Guess Papers
- Question Papers
✓ Unlock up to 60% of the answer
✓ No payment required