English
51. N-Queens
Problem Statement:
The n-queens puzzle is the problem of placing n
queens on an n x n
chessboard such that no two queens attack each other.
Given an integer n
, return all distinct solutions to the n-queens puzzle
. You may return the answer in any order
.
Each solution contains a distinct board configuration of the n-queens' placement, where 'Q'
and '.'
both indicate a queen and an empty space, respectively.
Example 1:
Input: n = 4
Output: [[".Q..","...Q","Q...","..Q."],["..Q.","Q...","...Q",".Q.."]]
Explanation: There exist two distinct solutions to the 4-queens puzzle as shown above
Example 2:
Input: n = 1
Output: [["Q"]]
Constraints:
- 1 <= n <= 9
Solution:
java
public class NQueens2 {
public static List<List<String>> nQueens(int n) {
// List of Lists of boards
List<List<String>> allBoards = new ArrayList<>();
char[][] board = new char[n][n];
helper(board, allBoards, 0);
return allBoards;
}
static void helper(char[][] board, List<List<String>> allBoards, int col) {
// save board to allBoards after placing Queens on all possible cols
if (col == board.length) {
saveBoard(board, allBoards);
return;
}
for (int row = 0; row < board.length; row++) {
// if it's safe, place queen at row
if (isSafe(row, col, board)) {
board[row][col] = 'Q';
helper(board, allBoards, col + 1);
// backtracking
board[row][col] = '.';
}
}
}
// Fn to check if it's safe to place Queen
static boolean isSafe(int row, int col, char[][] board) {
int len = board.length;
// traverse in all cols to check if a queen is already present or not
for (int i = 0; i < len; i++) {
if (board[row][i] == 'Q')
return false;
}
// traverse in all rows to check if a queen is already present or not
for (int i = 0; i < len; i++) {
if (board[i][col] == 'Q')
return false;
}
// traverse through upper left diagonal to check if queen is present
int r = row;
for (int c = col; c >= 0 && r >= 0; r--, c--) {
if (board[r][c] == 'Q')
return false;
}
// traverse through upper right diagonal to check if queen is present
r = row;
for (int c = col; c < len && r >= 0; r--, c++) {
if (board[r][c] == 'Q')
return false;
}
// traverse through lower left diagonal to check if queen is present
r = row;
for (int c = col; c >= 0 && r < len; r++, c--) {
if (board[r][c] == 'Q')
return false;
}
// traverse through lower right diagonal to check if queen is present
r = row;
for (int c = col; c < len && r < len; r++, c++) {
if (board[r][c] == 'Q')
return false;
}
return true;
}
// Fn to save a board to List of Boards
static void saveBoard(char[][] board, List<List<String>> allBoards) {
int len = board.length;
String row = "";
List<String> newBoard = new ArrayList<>();
for (int i = 0; i < len; i++) {
row = "";
for (int j = 0; j < len; j++) {
if (board[i][j] == 'Q')
row += 'Q';
else
row += '.';
}
// this adds the row to the newBoards -> "Q..." or "..Q."
newBoard.add(row);
}
// this add the board to the list of boards -> [[..Q., Q..., ...Q, .Q..],...]
allBoards.add(newBoard);
}
}
...