常用十种算法—马踏棋盘算法
声明:以下是学的尚硅谷网课并结合网上资料所记的笔记。可能会有一些错误,发现了会修改。
马踏棋盘算法(骑士周游问题)
介绍
分析
- 马踏棋盘问题(骑士周游问题)实际上是图的深度优先搜索(DFS)的应用。
- 如果使用回溯(深度优先搜索)来解决,加入马儿踏了53个点,走到第53个,发现到了尽头,那就只能回退,查看其他路径,就在棋盘上不停的回溯…
- 可以在上面的基础上,使用贪心算法(greedyalgorithm)进行优化。
骑士周游问题的解决步骤和思路
非递减排序:如1,2,2,3,4,4,5,8这种序列就是非递减序列。
代码实现
import java.awt.Point;
import java.util.ArrayList;
import java.util.Comparator;
public class HorseChessboard {
private static int X; //表示棋盘的列数
private static int Y; //表示棋盘的行数
//创建一个数组,标记棋盘的各个位置是否被访问过
private static boolean[] visited;
//使用一个属性,标记是否棋盘的所有位置都被访问
private static boolean finished; //如果为true,表示成功
public static void main(String[] args) {
System.out.println("骑士周游算法开始运行!");
//测试算法是否正确
X = 8;
Y = 8;
int row = 1; //马儿初始位置的行,从1开始编号
int column = 1; //马儿初始位置的列,从1开始编号
//创建棋盘
int[][] chessboard = new int[X][Y];
visited = new boolean[X * Y]; //初始值都是false
//测试一下耗时
long start = System.currentTimeMillis();
traversalChessboard(chessboard, row-1, column-1, 1);
long end = System.currentTimeMillis();
System.out.println("共耗时:" + (end - start) + "毫秒");
//输出棋盘的最后情况
for(int[] rows : chessboard) {
for(int step : rows) {
System.out.print(step + "\t");
}
System.out.println();
}
}
/**
* 功能:完成骑士周游问题的算法
* @param chessboard 棋盘
* @param row 马儿当前的位置的行,从0开始
* @param column 马儿当前的位置的列,从0开始
* @param step 是第几步,初始位置就是第1步
*/
public static void traversalChessboard(int[][] chessboard, int row, int column, int step) {
chessboard[row][column] = step; //把当前走的这步的值替换成步数
visited[row * X + column] = true; //用的是一个一维数组描述二维数组的访问情况,标记该位置已经访问
//获取当前位置可以走的下一个位置的集合
ArrayList<Point> ps = next(new Point(column, row));
//对ps进行排序,排序的规则就是对ps的所有的point对象的下一步可走位置的数目,进行非递减排序
sort(ps);
//遍历ps
while(!ps.isEmpty()) {
Point p = ps.remove(0); //取出一个可以走的位置
//判断该点是否已经访问过
if(!visited[p.y * X + p.x]) { //还没有被访问过
traversalChessboard(chessboard, p.y, p.x, step + 1);
}
}
//判断马儿是否完成了任务,使用step和应该走的步数比较,如果没有达到数量,则表示没有完成任务,将整个棋盘置为0
/**
* 说明:step < X*Y 成立的情况有两种
* 1. 棋盘到目前为止,仍然没有走完
* 2. 棋盘处于一个回溯过程
*/
if(step < X * Y && !finished) {
chessboard[row][column] = 0;
visited[row * X + column] = false;
} else {
finished = true;
}
}
/**
* 功能:根据当前位置(Point对象),计算马儿还能走哪些位置(Point),并放入到一个集合(ArrayList),最多有8个位置
* @param curPoint
* @return
*/
public static ArrayList<Point> next(Point curPoint){
//创建一个ArrayList
ArrayList<Point> ps = new ArrayList<Point>();
//创建一个Point
Point p1 = new Point();
//判断马是否可以走5这个位置
if((p1.x = curPoint.x - 2) >= 0 && (p1.y = curPoint.y - 1) >= 0) {
ps.add(new Point(p1));
}
//判断马是否可以走6这个位置
if((p1.x = curPoint.x - 1) >= 0 && (p1.y = curPoint.y - 2) >= 0) {
ps.add(new Point(p1));
}
//判断马是否可以走7这个位置
if((p1.x = curPoint.x + 1) < X && (p1.y = curPoint.y - 2) >= 0) {
ps.add(new Point(p1));
}
//判断马是否可以走0这个位置
if((p1.x = curPoint.x + 2) < X && (p1.y = curPoint.y - 1) >= 0) {
ps.add(new Point(p1));
}
//判断马是否可以走1这个位置
if((p1.x = curPoint.x + 2) < X && (p1.y = curPoint.y + 1) < Y) {
ps.add(new Point(p1));
}
//判断马是否可以走2这个位置
if((p1.x = curPoint.x + 1) < X && (p1.y = curPoint.y + 2) < Y) {
ps.add(new Point(p1));
}
//判断马是否可以走3这个位置
if((p1.x = curPoint.x - 1) >= 0 && (p1.y = curPoint.y + 2) < Y) {
ps.add(new Point(p1));
}
//判断马是否可以走4这个位置
if((p1.x = curPoint.x - 2) >= 0 && (p1.y = curPoint.y + 1) < Y) {
ps.add(new Point(p1));
}
return ps;
}
//根据当前这一步的所有的下一步的选择位置,进行非递减排序(如1,2,2,3,4,4,5就是非递减)
//减少回溯的次数
public static void sort(ArrayList<Point> ps) {
ps.sort(new Comparator<Point>() {
public int compare(Point o1, Point o2) {
//先获取o1的下一步的所有位置的个数
int count1 = next(o1).size();
//先获取o2的下一步的所有位置的个数
int count2 = next(o2).size();
return count1 - count2;
}
});
}
}
运行结果:
- 没有进行优化前的时间:34522毫秒
- 用贪心算法优化后的时间:23毫秒
--------------------------------------- 个人学习笔记----------------------------------------