一,基本介绍
1)马踏棋盘算法也称为骑士周游问题
2)将马随机放在国际象棋的8 × 8棋盘Board[0~7] [0~7]的某个方格中,马按走棋规则(马走日字)进行移动。要求每个方格只进入一次,走遍棋盘上全部64个方格
二,基本思路
1)马踏棋盘问题(骑士周游世界问题)实际上是图的深度优先搜索(DFS)的应用;
2)如果使用回溯(就是深度优先搜索)来解决,假如马儿踏了第53个,发现已经走到了尽头,没办法,那就只能回退了,查看其他的路径,就在棋盘上不停回溯…效率很低;
3)因此需要使用贪心算法(greedyalgorithm)进行优化。我们直接使用贪心算法,优化思路来解决马踏棋盘问题
三,思路分析
1)创建棋盘chessBoard,是一个二维数组;
2)将当前位置设置为已经访问,然后根据当前位置,计算马儿还能走哪些位置,并放入到一个集合中(ArrayList),最多有8个位置;
3)遍历ArrayList中存在的所有位置,看看哪个可以走通;如果走通,继续,走不通,就回溯;
4)判断马儿是否完成任务了任务step和应该走的步数比较
注意:马儿的不同走法(策略),会得到不同的结果,效率也会有影响(优化)
使用贪心算法对原来算法优化
1)我们获取当前位置,可以走下一个位置的集合
//获取当前位置,可以走的下一个位置集合
2)我们需要对ps中所有的point的下一步的所有集合的数目,进行非递减排序(1,1,2,3,4,4,5),就ok
四,代码实现
package com.algorithm.horse;
import java.awt.*;
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) {
//测试
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) + "ms");
//输出棋盘的最后情况
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 是第几步,初始位置就是第一步
*/
private 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
//1.棋盘到目前位置,仍然没有走完
//2.棋盘处于一个回溯过程
if (step < x * y && !finished) {
chessboard[row][column] = 0;
visited[row * x + column] = false;
} else {
finished = true;
}
}
/**
* 根据当前位置,计算马儿还能走哪些位置,并放入到一个集合中(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;
}
//根据当前这一步的所有下一步的选择位置,进行非递减排序
public static void sort(ArrayList<Point> ps) {
ps.sort(new Comparator<Point>() {
@Override
public int compare(Point o1, Point o2) {
//获取到o1点的下一步的所有位置个数
int count1 = next(o1).size();
int count2 = next(o2).size();
if (count1 < count2) {
return -1;
} else if (count1 == count2) {
return 0;
} else {
return 1;
}
}
});
}
}
五,运行结果
没优化前
优化后