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.
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.
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.
You do not need to print anything, it has already been taken care of. Just implement the given function.
1 <= T <= 10
1 <= N <= 10
Time Limit : 1 sec
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.
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 if it’s safe to place the queen here, by checking 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.
Note: There could be the only one queen in a row and the only one queen in a column.
Algorithm:
Create an output array that stores all the possible configurations of N - queen puzzle.
Checking if placing queen at square[row, col] is valid/safe:
At last, we will return the output array containing all the configurations in which we can successfully place the N queens.
We can optimize the previous algorithm by not using a 2-D array for checking whether it’s safe to place a queen or not. We will use the property of diagonals to achieve this.
For checking upper left diagonal and lower left diagonal, try finding a pattern between the index of row and column.
So to check for diagonals we will match that particular constant(difference for upper left and sum for lower left), if that constant as the index is set for that specific array, that means the queen is present in the diagonal, and that cell is not safe.
To make it more clear, let’s move to the algorithm.
Algorithm:
Create an output array that stores all the possible configurations of N - queen puzzle.
At last, we will return the array containing all the configurations in which we can successfully place the N queens.
Prime Digit Sum
Prime Digit Sum
Mario And His Princess
Combination Sum III
Combination Sum III
Combination Sum III
Combination Sum III
Combination Sum III
Generate All Strings
Generate All Strings
Generate All Strings
8-Queen Problem