Return a binary matrix that gives 1s for the block where queens are placed. We use Backtracking to solve this problem
Hello programmers I hope you have learned recursion properly before solving this problem.
So in recursion, we first take a base condition
- Here row = n is the base condition
The output we are finding is where we have to place N chess queens in a NxN board where no queens attack each other.
Explanation: We have to place each queen in a different column, when we place a queen in a column we have to check for clashes with other queens.
We have to go to each and every row and column and mark it as safe, otherwise if we find a clash we backtrack and return false.
After repeating this when we reach the end and when row = N we return true / we return the final output.
Submitted by Anirban Chatterjee (anirban20)
Download packets of source code on Coders Packet
Comments