Professional Documents
Culture Documents
8 Queen Problem
8 Queen Problem
Introduction
Backtracking is a recursive approach for solving any problem where we
must search among all the possible solutions following some constraints.
More precisely, we can say that it is an improvement over the brute force
technique. In this blog, we will learn one popular DSA question: 8 queens
problem using Backtracking.
00001000
01000000
00010000
00000010
00100000
00000001
00000100
10000000
Implementation in Python:
def is_safe(board, row, col):
for i in range(col):
if board[row][i] == 1:
return False
if board[i][j] == 1:
return False
if board[i][j] == 1:
return False
return True
if col >= N:
return True
for i in range(N):
if is_safe(board, i, col):
board[i][col] = 1
return True
board[i][col] = 0
return False
def solve_n_queens():
return False
print_board(board)
return True
def print_board(board):
solve_n_queens()
OUTPUT:
10000000
00000010
00001000
00000001
01000000
00010000
00000100
00100000
CONCLUSION:
In summary, the algorithm for solving the Eight Queens Puzzle efficiently
utilizes backtracking to explore possible queen placements on the
chessboard while ensuring no two queens threaten each other. By
employing recursion and safety checks, the algorithm progressively
constructs valid configurations, backtracking when necessary. This approach
balances completeness and efficiency, providing a robust solution to the
puzzle.