N Queens

Hard
0/120
Average time to solve is 55m
132 upvotes
Asked in companies
GoogleShareChatDunzo

Problem statement

You are given an integer 'N'. For a given 'N' x 'N' chessboard, find a way to place 'N' queens such that no queen can attack any other queen on the chessboard.

A queen can be killed when it lies in the same row, or same column, or the same diagonal of any of the other queens. You have to print all such configurations.

Detailed explanation ( Input/output format, Notes, Images )
Input Format:
The first and the only line of input contains an integer 'N' representing the size of the chessboard and the number of queens.
Output Format:
Each line would be representing a single configuration.

Each configuration would contain 'N '* 'N' elements printed row-wise separated by spaces. The position where we can place the queen will have the value 1 rest will have the value 0.
Note:
You do not need to print anything, it has already been taken care of. Just implement the given function.
Constraints:
1 <= 'N' <= 10

Time Limit: 1sec


For Example:
For a chessboard of size 4*4
The configurations are 

alt text

Sample Input 1:
4
Sample Output 1:
0 0 1 0 1 0 0 0 0 0 0 1 0 1 0 0
0 1 0 0 0 0 0 1 1 0 0 0 0 0 1 0 
Explanation For Sample Input 1:
Output depicts two possible configurations of the chessboard for 4 queens.

The Chessboard matrix for the first configuration looks as follows:-

0 0 1 0
1 0 0 0
0 0 0 1
0 1 0 0

Queen contained cell is depicted by 1. As seen, No queen is in the same row, column, or diagonal as the other queens. Hence this is a valid configuration.
Sample Input 2:
3
Sample Output2:
      (Blank)
Explanation For Sample Input 2:
Since no possible configuration exists for 3 Queen's.The output remains Empty.
Hint

Can we use Backtracking to generate all possible configurations?

Approaches (2)
Backtracking
  1. Instead of checking all the places on the chessboard, we can use backtracking and place queen row-wise or column-wise.
  2. Suppose we place queens row-wise, and we start with the very first row. Place the queen and move to the next row until either there is a solution or there are no viable cells left.
  3. As we backtrack, check to place the queen in the different columns of the same row.
  4. When we can place a queen at the ‘N’th row, it means we have successfully placed all the ‘N’ queens so add this configuration to our final answer vector.
  5. At last, we will return the vector containing all the configurations in which we can successfully place the ‘N’ queens.
Time Complexity

O(N!), where ‘N’ is the number of queens and ‘!’ represents factorial.

 

For the first row, we check ‘N’ columns, for the second row we check 'N - 1 column and so on. hence time complexity will be N * (N-1) * (N-2) …. i.e N!

Space Complexity

O(N^2), where ‘N’ is the number of queens.

 

As we are using a 2-D array of size N rows and N columns, and also because of recursion recursive stack will have a linear space here. So, the overall space complexity will be O(N^2). 

Video Solution
Unlock at level 3
(75% EXP penalty)
Code Solution
(100% EXP penalty)
N Queens
Full screen
Console