Question: Q 7 : N - Queens problem: There is an n x n grid where the value of n is 4 n 8 ( user
Q: Queens problem: There is an grid where the value of is user shall be asked at run time
for the value of he wants to keep Your task is to place queens on this board. As per the rules of chess, a
queen should have no other queen in its respective column, neither should it have any other queen in its row
nor should it have any within its diagonal cells. You can consider this case as placing each queen individually
per column such that it does not violate any of the constraints mentioned. It's quite easy to find solution
manually but your task is now to code it and find the correct positions for the queens.
This problem has to be solved through the concept of backtracking. So initially you will place the first
queen randomly at any location within column With respect to its location, now next queens'
domain ie the places where they can be placed might shrink up
Eg in this case queens have to placed and Q is placed on so positions represent the illegal
places now where other queens cannot be placed due to Q placement. This means for rest of the
queens some positions have been considered as illegal.
Further up when we will move forward, there might be a point where domain gets empty for any
particular queen, at that instance apply backtrack concept, which means that location of previous
queen will have to get changed.
Advise: Start building up your logic first on queen problem so that you may exactly understand the
flow then go for making a generalized code version of queens' placement.
Comments your code where necessary to make it clean and clear.
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
