There is no input.
Return a list of all possible chessboard configurations of the 8-queens puzzle.
You don't need to print anything. Just implement the given function.
Time Limit: 1-sec
The naive approach simulates the whole process, i.e. to try out all the possibilities and select valid ones. Array ‘ans’ will keep all the valid configurations of the chessboard. Some things to remember before starting with the algorithm:
We will use a recursive function that helps to place queens so that no two queens attack each other. We will go through all the columns using this function.
Suppose, I am in column ‘i’. So, from column 1 to column (i - 1), all the queens are placed in such a configuration that no two queens attack each other. Now, we try to place the queen at the ith column so that it doesn’t get attacked by any other queen. For this, we iterate from row 1 to row 8, and at each row, we check whether I can place my queen at this row of column ‘i’. If we can place the queen here, we place the queen and move to the next column. Since we are using a recursive function, Let’s say after calling the function for the next column i.e. for (i + 1), we are again at the column ‘i’ (i.e. we completed the recursive function for column (i + 1)). We will mark this row to contain no queen, and check for the remaining rows (By doing this, we can get all the valid configurations). We will stop when our row number is 9 (i.e, out of the chessboard). Otherwise, we go to the next row of the column. We do this until we are out of the chessboard (i.e. our column number is 9). Once we reach here, this means that we got a valid configuration. So, we insert this configuration in our ‘ans’ array.
// Function to return a valid chessboard configuration.
function insertConfiguration(char[][] board):
// Function to check that queen can be placed at (row, column) or not
function isSafe(char[][] board, int row, int column):
// Function to place the queen at cells of the board
function validConfiguration(int[][] ans, char[][] board, int column):
// Function to find all valid chessboard configurations 8-queen puzzle
function eightQueenPuzzle():
Can You Print
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