76. N Queen

The challenge of arranging n queens on a n × n chessboard so that no two queens attack one another is known as the "n-queens puzzle."

Return every unique solution to the n-queens puzzle given an integer n. The answer can be returned in any sequence.

Every solution has a unique board arrangement for the placement of the n-queens, where 'Q' and '.' stand for a queen and an empty space, respectively.

Here are the attack rules for N-Queens:

  1. Same Row - No two queens can be in the same row.
  2. Same Column - No two queens can be in the same column.
  3. Same Diagonal (top-left to bottom-right) - No two queens can share the same diagonal where (row - col) is equal.
  4. Same Anti-Diagonal (top-right to bottom-left) - No two queens can share the same anti-diagonal where (row + col) is equal.

Example 1:

Input : n = 4

Output : [[".Q.." , "...Q" , "Q..." , "..Q."] , ["..Q." , "Q..." , "...Q" , ".Q.."]]

Explanation : There are two possible combinations as shown below.

Example 2:

Input : n = 2

Output : [ [] ]

Explanation : There is no possible combination for placing two queens on a board of size 2*2.

Now Your Turn!

Pick the correct output for the given input

Input : n = 1

Still unsure what the problem is asking ?

Let’s go through a few more examples, step by step, to make it clearer.

Constraints:

  • 1 <= n <= 9

Hints

Frequently Occurring Doubts

Interview Follow-up Questions

Fun Facts

0
class Solution {
public:
vector<vector<string> > solveNQueens(int n) {
//your code goes here
}
};
Test Case

Input:

N