g0001_0100.s0051_n_queens.Solution Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of leetcode-in-java21 Show documentation
Show all versions of leetcode-in-java21 Show documentation
Java-based LeetCode algorithm problem solutions, regularly updated
package g0001_0100.s0051_n_queens;
// #Hard #Top_100_Liked_Questions #Array #Backtracking #Big_O_Time_O(N!)_Space_O(N)
// #2023_08_11_Time_1_ms_(100.00%)_Space_43.6_MB_(97.17%)
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
* 51 - N-Queens\.
*
* Hard
*
* 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:**
*
* ![](https://assets.leetcode.com/uploads/2020/11/13/queens.jpg)
*
* **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`
**/
public class Solution {
public List> solveNQueens(int n) {
boolean[] pos = new boolean[n + 2 * n - 1 + 2 * n - 1];
int[] pos2 = new int[n];
List> ans = new ArrayList<>();
helper(n, 0, pos, pos2, ans);
return ans;
}
private void helper(int n, int row, boolean[] pos, int[] pos2, List> ans) {
if (row == n) {
construct(n, pos2, ans);
return;
}
for (int i = 0; i < n; i++) {
int index = n + 2 * n - 1 + n - 1 + i - row;
if (pos[i] || pos[n + i + row] || pos[index]) {
continue;
}
pos[i] = true;
pos[n + i + row] = true;
pos[index] = true;
pos2[row] = i;
helper(n, row + 1, pos, pos2, ans);
pos[i] = false;
pos[n + i + row] = false;
pos[index] = false;
}
}
private void construct(int n, int[] pos, List> ans) {
List sol = new ArrayList<>();
for (int r = 0; r < n; r++) {
char[] queenRow = new char[n];
Arrays.fill(queenRow, '.');
queenRow[pos[r]] = 'Q';
sol.add(new String(queenRow));
}
ans.add(sol);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy