-
Notifications
You must be signed in to change notification settings - Fork 21
/
52. N-Queens II.java
executable file
·109 lines (92 loc) · 2.85 KB
/
52. N-Queens II.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
H
tags: Backtracking
time: O(n!)
space: O(n)
跟 N-Queens 一样, 不是找所有结果, 而是count多少结果.
#### Backtracking (with replacement)
- Each row has just 1 Queen value
- As CC book suggests, use `int[] columns` of length n to store all queen col positions for n rows
- `int[] columns` is slightly easier to backtrack by updating certain index i with new col
- list will usualy has the add/remove pattern for backtracking
#### Backtracking
- 当list.size() == n 的时候,说明找到了一个Solution。
- 1. dfs function (List<Integer>, n)
- 2. validate function
```
/*
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.
Tags Expand
Recursion
Thoughts:
Exact same as NQueens, except we don't print the map. Instead, simply add the record in rst.
At the end, return rst.size(), which would be unique answer.
*/
class Solution {
public int totalNQueens(int n) {
if (n <= 0) return 0;
return dfs(new int[n], 0);
}
private int dfs(int[] columns, int row) {
if (row == columns.length) return 1;
int count = 0;
for (int col = 0; col < columns.length; col++) {
if (validate(columns, row, col)) {
columns[row] = col; // place queen
count += dfs(columns, row + 1);
}
}
return count;
}
// Validate the prior row, colomn & diagnal
private boolean validate(int[] columns, int row, int col) {
for (int newRow = 0; newRow < row; newRow++) {
int newCol = columns[newRow];
if (col == newCol || Math.abs(row - newRow) == Math.abs(col - newCol)) {
return false;
}
}
return true;
}
}
/*
Thougths:
Goal: dfs and count all solutions
1. dfs function (List<Integer>, n)
2. validate function
*/
class Solution {
public int totalNQueens(int n) {
if (n <= 0) {
return 0;
}
return dfs(new ArrayList<>(), n);
}
private int dfs(List<Integer> list, int n) {
if (list.size() == n) {
return 1;
}
int count = 0;
for (int col = 0; col < n; col++) {
if (validateBoard(list, col)) {
list.add(col);
count += dfs(list, n);
list.remove(list.size() - 1);
}
}
return count;
}
private boolean validateBoard(List<Integer> list, int newColNum) {
int newRowNum = list.size();
for (int rowNum = 0; rowNum < list.size(); rowNum++) {
int colNum = list.get(rowNum);
if (colNum == newColNum || Math.abs(newColNum - colNum) == Math.abs(newRowNum - rowNum)) {
return false;
}
}
return true;
}
}
```