1 回溯
- 回溯可以理解为:通过选择不同的岔路口来通往目的地(找到想要的结果)
- 每一步都选择一条路出发,能进则进,不能进则退回上一步(回溯),换一条路再试
- 树、图的深度优先搜索(DFS)、八皇后、走迷宫都是典型的回溯应用
- 从图中不难看出来,回溯很适合使用递归,因为4做的事和根节点7做的事完全是一模一样的
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/25551d5f5fd6e9aa856031a9cef1a338.png)
2 练习 – 八皇后问题(Eight Queens)
- 在8x8格的国际象棋上摆放八个皇后,使其不能互相攻击:任意两个皇后都不能处于同一行、同一列、同一斜线上。请问有多少种摆法。
- 剪枝
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/4fff19d148301762f1f81c2bce06f1c7.png)
- 回溯
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/aff9ccc6fc1305dafa101a1714b9c69d.png)
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/756f9e35e2a82890834287eaa4006d10.png)
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/c3a3e713455f2fef39050b1504baf676.png)
- Queens:使用int[] cols表示几行几列已经拜访了皇后,数组索引为行号,元素为列号
package com.mj;
public class Queens {
public static void main(String[] args) {
new Queens().placeQueens(4);
}
int[] cols;
int ways;
void placeQueens(int n) {
if (n < 1) return;
cols = new int[n];
place(0);
System.out.println(n + "皇后一共有" + ways + "种摆法");
}
void place(int row) {
if (row == cols.length) {
ways++;
show();
return;
}
for (int col = 0; col < cols.length; col++) {
if (isValid(row, col)) {
cols[row] = col;
place(row + 1);
}
}
}
boolean isValid(int row, int col) {
for (int i = 0; i < row; i++) {
if (cols[i] == col) {
System.out.println("[" + row + "][" + col + "]=false");
return false;
}
if (row - i == Math.abs(col - cols[i])) {
System.out.println("[" + row + "][" + col + "]=false");
return false;
}
}
System.out.println("[" + row + "][" + col + "]=true");
return true;
}
void show() {
for (int row = 0; row < cols.length; row++) {
for (int col = 0; col < cols.length; col++) {
if (cols[row] == col) {
System.out.print("1 ");
} else {
System.out.print("0 ");
}
}
System.out.println();
}
System.out.println("------------------------------");
}
}
- Queens2:改为使用boolean[] cols标记哪列拜访了皇后、boolean[] leftTop与boolean[] rightTop表示哪条斜线保存了皇后
package com.mj;
public class Queens2 {
public static void main(String[] args) {
new Queens2().placeQueens(4);
}
int[] queens;
boolean[] cols;
boolean[] leftTop;
boolean[] rightTop;
int ways;
void placeQueens(int n) {
if (n < 1) return;
queens = new int[n];
cols = new boolean[n];
leftTop = new boolean[(n << 1) - 1];
rightTop = new boolean[leftTop.length];
place(0);
System.out.println(n + "皇后一共有" + ways + "种摆法");
}
void place(int row) {
if (row == cols.length) {
ways++;
show();
return;
}
for (int col = 0; col < cols.length; col++) {
if (cols[col]) continue;
int ltIndex = row - col + cols.length - 1;
if (leftTop[ltIndex]) continue;
int rtIndex = row +col;
if (rightTop[rtIndex]) continue;
queens[row] = col;
cols[col] = true;
leftTop[ltIndex] = true;
rightTop[rtIndex] = true;
place(row + 1);
cols[col] = false;
leftTop[ltIndex] = false;
rightTop[rtIndex] = false;
}
}
void show() {
for (int row = 0; row < cols.length; row++) {
for (int col = 0; col < cols.length; col++) {
if (queens[row] == col) {
System.out.print("1 ");
} else {
System.out.print("0 ");
}
}
System.out.println();
}
System.out.println("------------------------------");
}
}
- Queens3:当boolean数组不是很长时,使用byte cols、short leftTop、short rightTop来替代boolean数组,使用位运算来表示某列、斜线上,是否摆放了皇后。如果boolean数组非常长,那么也可以使用long[]来替代,其中一个long元素,代替boolean数组中的32个元素
package com.mj;
public class Queens3 {
public static void main(String[] args) {
new Queens3().place8Queens();
}
int[] queens;
byte cols;
short leftTop;
short rightTop;
int ways;
void place8Queens() {
queens = new int[8];
place(0);
System.out.println("8皇后一共有" + ways + "种摆法");
}
void place(int row) {
if (row == 8) {
ways++;
show();
return;
}
for (int col = 0; col < 8; col++) {
int cv = 1 << col;
if ((cols & cv) != 0) continue;
int lv = 1 << (row - col + 7);
if ((leftTop & lv) != 0) continue;
int rv = 1 << (row + col);
if ((rightTop & rv) != 0) continue;
queens[row] = col;
cols |= cv;
leftTop |= lv;
rightTop |= rv;
place(row + 1);
cols &= ~cv;
leftTop &= ~lv;
rightTop &= ~rv;
}
}
void show() {
for (int row = 0; row < 8; row++) {
for (int col = 0; col < 8; col++) {
if (queens[row] == col) {
System.out.print("1 ");
} else {
System.out.print("0 ");
}
}
System.out.println();
}
System.out.println("------------------------------");
}
}