The N Queen is the problem of placing N chess queens on an N×N chessboard so that no two queens attack each other. For example, following is a solution for 4 Queen problem.
The N Queen is the problem of placing N chess queens on an N×N chessboard so that no two queens attack each other. For example, following are two solutions for 4 Queen problem.
In previous post, we have discussed an approach that prints only one possible solution, so now in this post the task is to print all solutions in N-Queen Problem. The solution discussed here is an extension of same approach.
The idea is to place queens one by one in different columns, starting from the leftmost column. When we place a queen in a column, we check for clashes with already placed queens. In the current column, if we find a row for which there is no clash, we mark this row and column as part of the solution. If we do not find such a row due to clashes then we backtrack and return false.
1) Start in the leftmost column 2) If all queens are placed return true 3) Try all rows in the current column. Do following for every tried row. a) If the queen can be placed safely in this row then mark this [row, column] as part of the solution and recursively check if placing queen here leads to a solution. b) If placing queen in [row, column] leads to a solution then return true. c) If placing queen doesn't lead to a solution then unmark this [row, column] (Backtrack) and go to step (a) to try other rows. 3) If all rows have been tried and nothing worked, return false to trigger backtracking.
There is only a slight modification in above algorithm that is highlighted in the code.
1- 0 0 1 0 1 0 0 0 0 0 0 1 0 1 0 0 2- 0 1 0 0 0 0 0 1 1 0 0 0 0 0 1 0
This article is contributed by Sahil Chhabra. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Printing brackets in Matrix Chain Multiplication Problem
- Printing string in plus ‘+’ pattern in the matrix
- Perfect Sum Problem
- The Celebrity Problem
- 8 queen problem
- N Queen Problem | Backtracking-3
- m Coloring Problem | Backtracking-5
- Gold Mine Problem
- The Knight's tour problem | Backtracking-1
- Word Break Problem using Backtracking
- N Queen Problem using Branch And Bound
- Mobile Numeric Keypad Problem
- Problem of 8 Neighbours of an element in a 2-D Matrix
- Warnsdorff's algorithm for Knight’s tour problem
- Travelling Salesman Problem implementation using BackTracking
- Rat in a Maze Problem when movement in all possible directions is allowed
- Maths behind number of paths in matrix problem
- N-Queen Problem | Local Search using Hill climbing with random neighbour