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

8 Queens Problem

Problem

Uploaded by

jyotikedar0705
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 views

8 Queens Problem

Problem

Uploaded by

jyotikedar0705
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/ 3

8 Queens problem

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.
We will retain the backtracking approach and go through its
time optimization. Let's start with the problem statement of
solving 8 queens problem using backtracking in the next
section.

Problem Statement
Given an 8x8 chess board, you must place 8 queens on the
board so that no two queens attack each other. Print all
possible matrices satisfying the conditions with positions with
queens marked with '1' and empty spaces with '0'. You must
solve the 8 queens problem using backtracking.
 Note 1: A queen can move vertically, horizontally and

diagonally in any number of steps.


 Note 2: You can also go through the N-Queen Problem for

the general
 approach to solving this problem.

Sample Example
Example: One possible solution to the 8 queens problem
using backtracking is shown below. In the first row, the queen
is at E8 square, so we have to make sure no queen is in
column E and row 8 and also along its diagonals. Similarly,
for the second row, the queen is on the B7 square, thus, we
have to secure its horizontal, vertical, and diagonal squares.
The same pattern is followed for the rest of the queens.

Output:

00001000
01000000
00010000
00000010
00100000
00000001
00000100
10000000

Bruteforce Approach

One brute-force approach to solving this problem is as


follows:
 Generate all possible permutations of the numbers 1 to 8,

representing the columns on the chessboard.


 For each permutation, check if it represents a valid

solution by checking that no two queens are in the same


row or diagonal.
 If a valid solution is found, print the board layout.

While this approach works for small numbers, it quickly


becomes inefficient for larger sizes as the number of
permutations to check grows exponentially. More efficient
algorithms, such as backtracking or genetic algorithms, can be
used to solve the problem in a more optimized way.

Backtracking Approach
This approach rejects all further moves if the solution is
declined at any step, goes back to the previous step and
explores other options.

Algorithm
Let's go through the steps below to understand how this
algorithm of solving the 8 queens problem using backtracking
works:

 Step 1: Traverse all the rows in one column at a time and


try to place the queen in that position.
 Step 2: After coming to a new square in the left column,
traverse to its left horizontal direction to see if any queen
is already placed in that row or not. If a queen is found,
then move to other rows to search for a possible position
for the queen.
 Step 3: Like step 2, check the upper and lower left
diagonals. We do not check the right side because it's
impossible to find a queen on that side of the board yet.
 Step 4: If the process succeeds, i.e. a queen is not found,
mark the position as '1' and move ahead.
 Step 5: Recursively use the above-listed steps to reach the
last column. Print the solution matrix if a queen is
successfully placed in the last column.
 Step 6: Backtrack to find other solutions after printing one
possible solution.

You might also like