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

DAAL - Assignment No 7

Uploaded by

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

DAAL - Assignment No 7

Uploaded by

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

Department of Artificial Intelligence & Data Science

K. K. Wagh Institute of Engineering Education and Research


Hirabai Haridas Vidyanagari, Amrut Dham, Panchavati, Nashik-422003
Design and Analysis of Algorithms Lab
TY AI&DS Semester: I

Assignment No.: 7

Assignment Title: 8-Queen matrix is stored having first queen placed; use backtracking to place
remaining queens to generate the final 8-queen matrix using python.

Objectives:
 To develop a program that develop 8 queens problem using backtracking
Outcomes:

1. No two queens share the same diagonal.


2. No two queens share the same column
3. No two queens share the same row.

THEORY:
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.

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.

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 8queens 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.

8 queens problem using backtracking 1


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. The eight queens
problem is the problem of placing eight queens on an 8×8chessboard such that none of
them attack one another (no two are in the same row, column, or diagonal). More generally, the n
queens problem places n queens on an n×n chessboard.

Output:

 . The eight queens problem is the problem of placing eight queens on an


8×8chessboard such that none of them attack one another.

Conclusion: The backtracking algorithm is an efficient method to solve the 8-Queens problem by
systematically placing queens on the board, ensuring they do not threaten each other. With
backtracking, we explore different possibilities and backtrack when conflicts arise, ultimately arriving
at a valid solution.

8 queens problem using backtracking 2

You might also like