The N-Queens problem is a classic chess puzzle: place N queens on an N×N chessboard so that no two queens threaten each other.
This means no two queens can share the same row, column, or diagonal.
The N-Queens problem is a perfect example of backtracking:
Here's how we can implement the N-Queens solution using backtracking:
O(N!), where N is the size of the board. In the worst case, we need to explore all possible placements of queens.
O(N²) for the board representation, plus O(N) for the recursion stack.
Learn how to solve the classic N-Queens problem using backtracking.
The N-Queens problem is a classic chess puzzle: place N queens on an N×N chessboard so that no two queens threaten each other.
This means no two queens can share the same row, column, or diagonal.
Example for N=4:
One of the two solutions for the 4-Queens problem
The N-Queens problem is a perfect candidate for backtracking because:
We can build the solution one queen at a time, row by row.
We can easily check if placing a queen violates any constraints.
We can abandon invalid paths early without exploring all possibilities.
Backtracking can find all possible solutions to the problem.
Begin with an empty N×N chessboard.
Try placing a queen in the first row, first valid column.
Move to the next row and try placing a queen in a valid position.
If no valid position exists, backtrack to the previous row and try a different position.
Continue until all queens are placed or all possibilities are exhausted.
Here's how we can implement the N-Queens solution using backtracking:
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556function solveNQueens(n) { const result = []; const board = Array(n).fill().map(() => Array(n).fill('.')); function backtrack(row) { // Base case: If we've placed queens in all rows, we have a valid solution if (row === n) { // Convert the board to the required format and add to result const solution = board.map(row => row.join('')); result.push(solution); return; } // Try placing a queen in each column of the current row for (let col = 0; col < n; col++) { if (isValid(row, col)) { // Place the queen board[row][col] = 'Q'; // Recursively place queens in the next row backtrack(row + 1); // Backtrack: remove the queen to try other positions board[row][col] = '.'; } } } function isValid(row, col) { // Check if there's a queen in the same column for (let i = 0; i < row; i++) { if (board[i][col] === 'Q') { return false; } } // Check upper-left diagonal for (let i = row - 1, j = col - 1; i >= 0 && j >= 0; i--, j--) { if (board[i][j] === 'Q') { return false; } } // Check upper-right diagonal for (let i = row - 1, j = col + 1; i >= 0 && j < n; i--, j++) { if (board[i][j] === 'Q') { return false; } } return true; } backtrack(0); return result;}
O(N!), where N is the size of the board.
In the worst case, we need to explore all possible placements of queens. For the first row, we have N options, for the second row, we have at most N-1 options, and so on.
O(N²) for the board representation, plus O(N) for the recursion stack.
We need an N×N board to represent the chessboard, and the recursion stack can go up to depth N in the worst case.
The N-Queens problem is a classic chess puzzle: place N queens on an N×N chessboard so that no two queens threaten each other.
This means no two queens can share the same row, column, or diagonal.
The N-Queens problem is a perfect example of backtracking:
Here's how we can implement the N-Queens solution using backtracking:
O(N!), where N is the size of the board. In the worst case, we need to explore all possible placements of queens.
O(N²) for the board representation, plus O(N) for the recursion stack.
Learn how to solve the classic N-Queens problem using backtracking.
The N-Queens problem is a classic chess puzzle: place N queens on an N×N chessboard so that no two queens threaten each other.
This means no two queens can share the same row, column, or diagonal.
Example for N=4:
One of the two solutions for the 4-Queens problem
The N-Queens problem is a perfect candidate for backtracking because:
We can build the solution one queen at a time, row by row.
We can easily check if placing a queen violates any constraints.
We can abandon invalid paths early without exploring all possibilities.
Backtracking can find all possible solutions to the problem.
Begin with an empty N×N chessboard.
Try placing a queen in the first row, first valid column.
Move to the next row and try placing a queen in a valid position.
If no valid position exists, backtrack to the previous row and try a different position.
Continue until all queens are placed or all possibilities are exhausted.
Here's how we can implement the N-Queens solution using backtracking:
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556function solveNQueens(n) { const result = []; const board = Array(n).fill().map(() => Array(n).fill('.')); function backtrack(row) { // Base case: If we've placed queens in all rows, we have a valid solution if (row === n) { // Convert the board to the required format and add to result const solution = board.map(row => row.join('')); result.push(solution); return; } // Try placing a queen in each column of the current row for (let col = 0; col < n; col++) { if (isValid(row, col)) { // Place the queen board[row][col] = 'Q'; // Recursively place queens in the next row backtrack(row + 1); // Backtrack: remove the queen to try other positions board[row][col] = '.'; } } } function isValid(row, col) { // Check if there's a queen in the same column for (let i = 0; i < row; i++) { if (board[i][col] === 'Q') { return false; } } // Check upper-left diagonal for (let i = row - 1, j = col - 1; i >= 0 && j >= 0; i--, j--) { if (board[i][j] === 'Q') { return false; } } // Check upper-right diagonal for (let i = row - 1, j = col + 1; i >= 0 && j < n; i--, j++) { if (board[i][j] === 'Q') { return false; } } return true; } backtrack(0); return result;}
O(N!), where N is the size of the board.
In the worst case, we need to explore all possible placements of queens. For the first row, we have N options, for the second row, we have at most N-1 options, and so on.
O(N²) for the board representation, plus O(N) for the recursion stack.
We need an N×N board to represent the chessboard, and the recursion stack can go up to depth N in the worst case.