The Eight Queens Problem is a classic backtracking algorithm challenge where the goal is to place eight queens on an 8x8 chessboard such that no two queens attack each other. The problem has two types of constraints: implicit constraints (no two queens in the same row, column, or diagonal) and explicit constraints (each row must contain exactly one queen, represented by a solution vector where each element xi ranges from 1 to 8). The backtracking algorithm systematically explores possible placements by placing queens row by row, checking constraints at each step, and backtracking when a valid placement is not found. The problem has 92 valid solutions, with mirror image solutions being common variations.
Deep Dive
Prerequisite Knowledge
- No data available.
Where to go next
- No data available.
Deep Dive
8 Queen's Problem using Backtracking || Design and analysis of algorithms (DAA) || Solved ExampleAdded:
Now let us discuss about eight queens problem using backtracking.
So here an 8 by8 chessboard is given.
We have to place eight queens on the 8x8 chessboard such that no two queens attack each other. So here we have 8 by8 chessboard. We have eight rows as well as eight columns.
So totally we have 8 into 8 64 cells. So in those 64 cells we have to place eight queens such that no two queens attack each other. Okay. Uh now let us see some uh constraints.
Here we have two types of constraints in backtracking. So we have to implement this concept using backtracking.
So eight queens problem comes under queens problem. So we have mainly two examples of the n queens problem. First one is four queens problem. Second one is eight queens problem. So let us see the constraints here. Uh so the first constraint is uh uh implicit constraints.
Here we have three implicit constraints.
First one is no two queens are in the same row. So we can't place two queens in the same row.
Second constraint.
No two queens are in the same column. So we can't place more than one queen in the same column.
Next third constraint.
No two queens are in the same diagonal.
So we can't place more than one more than one queen in the same diagonal. So these are the implicit constraints. The second one is explicit constraints.
explicit constraints. Here uh uh in eight queens problem the solution vector is represented by x i. So totally we have to place eight queens here.
Uh so here the queens are denoted by like this from q1 to q8. So q1, q2, q3, q4, q5, q6, q7, q8. Here the value of this xi. So this x i is nothing but uh the column number.
x i ranges from uh 1 to 8.
So x i value may be either greater than 1, greater than or equal to 1 or less than or equal to 8. So this is nothing but uh explicit constraints here. So explicit constraint specifies each row should contain only one queen.
So if you take this row, so in this row we have to place only one queen. In the first row we have to place only first queen. In the second row we have to place only the second queen. So that is nothing but uh explicit constraints.
Each row must contain only one queen. So there is only one queen per each row. So here the solution vector is represented by x i. So here in in in the eight rows so totally we have to place eight queens. Okay. So here this xi value may be from either 1 to 8. Okay. Uh now let us see the problem here. Uh uh totally there are 92 valid solutions are there for eight pins problem. So it is extremely difficult to show all the backtrackings. So we know when backtracking is needed. So if there is a mistake, if there is no other path then we have to do the backtracking and uh uh we need to explore some other paths. But here totally there are 92 possible solutions are there. As there are so many cells here so it is difficult to demonstrate uh all those uh solutions.
Whereas if you take four pins problem we have only two solutions. So it is easy to show all those solutions. But here it is difficult to show all the 92 solutions with the corresponding backtracking. Uh here I'm showing one simple solution. So let us see that solution. Uh let us place the first queen one in the first row. So for that what I'm doing is I'm calculating middle of the middle of the middle of the this first value and the last value.
So 1 + 8 means 9. So 9 by2 means 4.5.
So I am placing the first queen in the fourth cell.
So place queen one in the fourth cell. Fourth column. Fourth column. So we can't place another queen in the same row or we can't place another queen in the same column.
We can't place another queen in the same diagonal.
Same diagonal. Okay. So, let us write this dash. So, this dash specifies we can't place another queen in the corresponding cell.
Now, let us place second queen. So, for second queen, how many possibilities are there? So, 1 2 3 4 5. So, five possibilities are there. So for placing the second queen it is better to use the rightmost cells from the Q1. So this is the rightmost cell from the Q1. So let us place queen two in in this cell. So we can't place another queen in the same row. Next same column.
Next in the same diagonal also we can't place. In the same diagonal also we can't place. Okay. Next let us place queen three in the third row.
Third row. So let us take the rightmost cells from queen two. So here we have only one rightmost empty cell from this cell. So let us place here.
So we can't place another queen in the same row, same column.
Next same diagonal. Okay, same diagonal.
Next let us place queen four here. Here the boundary is reached. This rightmost boundary is reached. So it is better to uh set from the leftmost boundary. Here this empty cell is nearer to the boundary. Here we have second empty cell from the leftmost border. Whereas here we have fourth empty cell from the rightmost border. So it is better to choose this one. Why? Because this empty cell is nearer to the leftmost boundary.
So let us place four here. So we can't place another queen in the same row, same column.
Next, same diagonal.
Same diagonal.
Same diagonal. Okay. Next, let us place uh queen five. Yeah, for queen four, this is also diagonal.
Next, let us place five. Queen five. So here from the right border. So this cell this empty cell is nearer to this border. So it is better to place queen five in this cell. Okay. So we can't place another queen in the same row.
Same column.
Next same diagonal.
Same diagonal. Okay.
Same diagonal. Okay. All are empty only.
Uh next let us place six.
Queen six. So here this empty cell is nearer to this border. So let us place here queen six. So we can't place another queen in the same row. Same column.
Yeah. Same diagonal.
Next let us place queen seven. So in the seventh row we have only one empty cell.
Let us place queen eight. So here we have only one empty cell. So here the solution is so we have to place the first queen in the fourth row.
Here we have to mention I'm sorry fourth column here. Here this cell represents the column number. Column number. Next place second pin in the sixth column.
Next place third queen in the eighth column.
Next fourth queen in the second column.
Next fifth queen in seventh column.
Next sixth queen in first column.
Seventh king third column. Eighth one fifth column. So this is one possible solution for way eight pins problem.
Another possible solution is mirror image. So mirror image means reverse the values. So 53 1 7 2 8 6 4 5 3 1 72 864 that mean so here the second solution is mirror image of this one. So queen one is placed in fifth column. Queen two third column. Queen three first column. Queen four seventh column. Queen five second column. Queen 6 eighth column. Queen seven sixth column. and queen eight fourth column.
Related Videos
Agentforce NOW AMA: Build with React and Salesforce Multi-Framework
SalesforceDevs
490 views•2026-05-28
How agent o11y differs from traditional o11y — Phil Hetzel, Braintrust
aiDotEngineer
450 views•2026-05-28
WEB TECHNOLOGIES UNIT-2 | Degree 4th sem BCOM Computers web technologies unit-2 full explanation💯✅
LearnwithSahera
1K views•2026-05-29
More tests are always better? How to use AI to identify tests that bring little value
Alliance4Qualification
335 views•2026-05-29
Search Algorithms Explained in 60 Seconds! 🤖💨
samarthtuliofficial
218 views•2026-06-01
People of Game of Thrones using JavaScript DOM
AltCampus
296 views•2026-05-30
Introduction to Problem Solving Part - 1 | Lecture 1 | Intermediate DSA
ascensionix
107 views•2026-05-29
🚀 BCS613C Compiler Design | Module 1 to 5 Schema Evaluation 🔥 | VTU 6th Sem 💯 #VTU #bcs613c #exam
Pranavaa-y4y
104 views•2026-06-02











