The N Queens puzzle is the problem of placing N chess queens on an N * N chessboard such that no two queens attack each other.
Given an integer ‘N’, print all distinct solutions to the ‘N’ queen puzzle.
Two queens on the same chessboard can attack each other if any of the below condition satisfies:
1. They share a row.
2. They share a column.
3. They share a diagonal.
Input Format:
The first line contains an integer 'T' which denotes the number of test cases. Then the test cases follow.
The first and the only line of each test case contains an integer ‘N’ denoting the size of the chessboard.
Output Format
For each test case, print all the possible solutions, each in a new line.
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.
The sequence of the configurations returned does not matter.
The output of each test case is printed in a separate line.
Note :
You do not need to print anything, it has already been taken care of. Just implement the given function.
Constraints :
1 <= T <= 10
1 <= N <= 10
Time Limit : 1 sec
1
4
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
The 4 queens can be placed in two ways in a 4*4 chessboard. Both the configurations are shown in the below figure.

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 we can see, No queen is in the same row, column or diagonal of the other queens. Hence this is a valid configuration.
Similarly, the chessboard matrix for the second configuration looks as follows:-
0 1 0 0
0 0 0 1
1 0 0 0
0 0 1 0
Queen contained cell is depicted by 1. As we can see, No queen is in the same row, column or diagonal of the other queens. Hence this is also a valid configuration.
These are the only two valid configurations for 4-Queens.
1
3
Since no possible configuration exists for 3 Queen's, the output remains empty.
Generate all configurations by placing N queens in N different rows/columns and check for the valid configurations.
A naive way to solve this problem is to generate all the possible combinations of given N queens on the N x N chessboard and check if the current combination satisfies the N Queen problem constraints or not. If it does, then add the configuration to the solution and move ahead to find other possible combinations.
Algorithm:
Create an output array that stores all the possible solutions of N - queen puzzle.
Checking if a configuration is valid:
At last, we will return the output array containing all the configurations in which we can successfully place the N queens.
O(N ^ N), where ‘N’ is the number of queens.
There are N possibilities to place the first queen, N possibilities to place the second queen, N possibilities to place the third queen, and so on. Thus, the final time complexity is O(N ^ N).
O(N * N), where ‘N’ is the number of queens in the configuration.
We are using a linear array of size N to keep information about queens in a column. Thus, the effective space complexity is O(N). Also, we are using a 2-D array to store all the configurations in which we can successfully place the N queens. Thus, the final space complexity is O(N + (N * N)) = O(N * N).