# [Leetcode] Backtracking回溯法(又称DFS,递归)全解

liuqi627

## 回溯全集

### 识别回溯

1. Find a path to success 有没有解
2. Find all paths to success 求所有解

• 求所有解的个数
• 求所有解的具体信息
3. Find the best path to success 求最优解

good leaf都在leaf上。good leaf是我们的goal state，leaf node是final state，是解空间的边界。

``````boolean solve(Node n) {
if n is a leaf node {
if the leaf is a goal node, return true
else return false
} else {
for each child c of n {
if solve(c) succeeds, return true
}
return false
}
}``````

Notice that the algorithm is expressed as a boolean function. This is essential to understanding the algorithm. If solve(n) is true, that means node n is part of a solution--that is, node n is one of the nodes on a path from the root to some goal node. We say that n is solvable. If solve(n) is false, then there is no path that includes n to any goal node.

``````boolean solve(Node n) {
if n is a leaf node {
if the leaf is a goal node, return true
else return false
} else {
for each child c of n {
if solve(c) succeeds, return true
}
return false
}
}
``````

``````void solve(Node n) {
if n is a leaf node {
if the leaf is a goal node, count++, return;
else return
} else {
for each child c of n {
solve(c)
}
}
}``````

``````void solve(Node n) {
if n is a leaf node {
if the leaf is a goal node, update best result, return;
else return
} else {
for each child c of n {
solve(c)
}
}
}
``````

### 题目

#### 八皇后 N-Queens

##### 问题

1.给个n，问有没有解；
2.给个n，有几种解；(Leetcode N-Queens II)
3.给个n，给出所有解；(Leetcode N-Queens I)

##### 解答
###### 1.有没有解

helper函数：
boolean solve(int i, int[][] matrix)

solve要在给定的matrix上试图给第i行每个位置放queen。

``````public static boolean solve1(int i, List<Integer> matrix, int n) {
if (i == n) {
if (isValid(matrix))
return true;
return false;
} else {
for (int j = 0; j < n; j++) {
if (isValid(matrix)) {    //剪枝
if (solve1(i + 1, matrix, n))
return true;
}
matrix.remove(matrix.size() - 1);
}
return false;
}
}
``````
###### 2.求解的个数

helper函数：
void solve(int i, int[][] matrix)

solve要在给定的matrix上试图给第i行每个位置放queen。

``````public static void solve2(int i, List<Integer> matrix, int n) {
if (i == n) {
if (isValid(matrix))
count++;
return;
} else {
for (int j = 0; j < n; j++) {
if (isValid(matrix)) {    //剪枝
solve2(i + 1, matrix, n);
}
matrix.remove(matrix.size() - 1);
}
}
}
``````
###### 3.求所有解的具体信息

helper函数：
void solve(int i, int[][] matrix)

solve要在给定的matrix上试图给第i行每个位置放queen。

``````public static void solve3(int i, List<Integer> matrix, int n) {
if (i == n) {
if (isValid(matrix))
return;
} else {
for (int j = 0; j < n; j++) {
if (isValid(matrix)) {    //剪枝
solve3(i + 1, matrix, n);
}
matrix.remove(matrix.size() - 1);
}
}
}``````
##### 优化

Utility(给一个list判断他的最后一行是否和前面冲突):

``````public static boolean isValid(List<Integer> list){
int row = list.size() - 1;
int col = list.get(row);
for (int i = 0; i <= row - 1; i++) {
int row1 = i;
int col1 = list.get(i);
if (col == col1)
return false;
if (row1 - row == col1 - col)
return false;
if (row1 - row == col - col1)
return false;
}
return true;

}

``````

361 声望
98 粉丝
0 条评论