The n-queens puzzle is the problem of placing n queens on an n×n chessboard such that no two queens attack each other.

Given an integer n, return all distinct solutions to the n-queens puzzle.

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

There exist two distinct solutions to the 4-queens puzzle:

[
  // Solution 1
  [".Q..",
   "...Q",
   "Q...",
   "..Q."
  ],
  // Solution 2
  ["..Q.",
   "Q...",
   "...Q",
   ".Q.."
  ]
]
 1 class Solution {
 2     public List<List<String>> solveNQueens(int n) {
 3         List<List<String>> allList = new ArrayList<>();
 4         if (n <= 0) return allList;
 5         Integer[] row = new Integer[n];
 6         List<List<Integer>> integerList = new ArrayList<>();
 7         queen(0, n, row, new ArrayList<>());
 8 
 9         char[] arr = new char[n];
10         Arrays.fill(arr, '.');
11         for (List<Integer> list : integerList) {
12             List<String> temp = new ArrayList<String>();
13             for (int i = 0; i < list.size(); i++) {
14                 arr[list.get(i)] = 'Q';
15                 temp.add(new String(arr));
16                 arr[list.get(i)] = '.';
17             }
18             allList.add(new ArrayList<String>(temp));
19         }
20         return allList;
21 
22     }
23 
24     public void queen(int n, int count, Integer[] row, List<List<Integer>> list) {
25         if (n == count) {
26             list.add(new ArrayList<Integer>(Arrays.asList(row)));
27             return;
28         }
29 
30         for (int i = 0; i < count; i++) {
31             row[n] = i;
32             if (isSatisfied(n, row)) {
33                 queen(n + 1, count, row, list);
34             }
35         }
36     }
37 
38     public boolean isSatisfied(int n, Integer[] row) {
39         for (int i = 0; i < n; i++) {
40             if (row[i] == row[n]) return false;
41             if (Math.abs(row[n] - row[i]) == n - i) return false;
42         }
43         return true;
44     }
45 }

N-Queens II

Follow up for N-Queens problem.

Now, instead outputting board configurations, return the total number of distinct solutions.

Example

For n=4, there are 2 distinct solutions.

 1 class Solution {
 2     public int totalNQueens(int n) {
 3         int[] row = new int[n];
 4         int[] current = new int[1];
 5         queen(0, n, row, current);
 6         return current[0];
 7     }
 8     
 9     public void queen(int n, int count, int[] row, int[] current) {
10         if (n == count) {
11             current[0]++;
12             return;
13         }
14         for (int i = 0; i < count; i++) {
15             row[n] = i;
16             if (isSatisfied(n, row)) {
17                 queen(n + 1, count, row, current);
18             }
19         }
20     }
21 
22     public boolean isSatisfied(int n, int[] row) {
23         for (int i = 0; i < n; i++) {
24             if (row[i] == row[n]) return false;
25             if (Math.abs(row[n] - row[i]) == n - i) return false;
26         }
27         return true;
28     }
29 }