-
Notifications
You must be signed in to change notification settings - Fork 171
/
N-Queens.java
57 lines (53 loc) · 1.8 KB
/
N-Queens.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
/*
Author: King, [email protected]
Date: Jul 25, 2013
Problem: N-Queens
Difficulty: Medium
Source: https://oj.leetcode.com/problems/n-queens/
Notes:
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.
For example,
There exist two distinct solutions to the 4-queens puzzle:
[
[".Q..", // Solution 1
"...Q",
"Q...",
"..Q."],
["..Q.", // Solution 2
"Q...",
"...Q",
".Q.."]
]
Solution: Recursion (DFS). Use bit-manipulation solution (See N-QueensII for more details).
*/
public class Solution {
public List<String[]> solveNQueens(int n) {
List<String[]> res = new ArrayList<String[]>();
List<char[]> sol = new ArrayList<char[]>();
solveNQueensRe(n, 0, 0, 0, sol, res);
return res;
}
public void solveNQueensRe(int n, int row, int ld, int rd, List<char[]> sol, List<String[]> res) {
if (row == (1<<n) -1 ) {
String[] temp = new String[n];
for (int i = 0; i < n; ++i)
temp[i] = String.valueOf(sol.get(i));
res.add(temp);
return;
}
int avail = ~(row | ld | rd);
for (int i = n -1; i >= 0; --i) {
int pos = 1 << i;
if ((int)(avail & pos) != 0) {
char[] str = new char[n];
Arrays.fill(str, '.');
str[i] = 'Q';
sol.add(str);
solveNQueensRe(n, row | pos, (ld|pos)<<1, (rd|pos)>>1, sol, res);
sol.remove(sol.size()-1);
}
}
}
}