目录
1.马踏棋盘算法介绍和游戏演示
1)马踏棋盘算法也被称为骑士周游问题
2)将马随机放在国际象棋的8X8棋盘Board[0~ 7][0~7]的某个方格中,马按走棋规则(马走日字)进行移动。要求每个方格只进入一次,走遍棋盘上全部64个方格
3) 游戏演示:http://www.4399.com/flash/146267_2.htm
2.马踏棋盘游戏代码实现
1)马踏棋盘问题(骑士周游问题)实际上是图的深度,优先搜索(DFS)的应用。
2)如果使用回溯(就是深度优先搜索)来解决,假如马儿踏了53个点,如图:走到了第53个,坐标(1,0) ,发现已经走到尽头,没办法,那就只能回退了,查看其他的路径,就在棋盘.上不停的回溯
3) 分析第一种方式的问题,并使用贪心算法( greedyalgorithm)进行优化。解决马踏棋盘问题.
4)使用前面的游戏来验证算法是否正确。
3.骑士周游问题的解决步骤和思路
1.创建棋盘chessBoard,是一个二维数组
2.将当前位置设置为已经访问,然后根据当前位置,计算马儿还能走哪些位置,并放入到一个集合中(ArrayList),最多有8个位置,每走一步, 就使用step+1
3.遍历ArrayList中存放的所有位置,看看哪个可以走通,如果走通,就继续,走不通,就回溯
4.判断马儿是否完成了任务,使用step 和应该走的步数比较,如果没有达到数量,则表示没有完成任务,将整个棋盘置0
注意:马儿不同的走法(策略),会得到不同的结果,效率也会有影响(优化)
4.代码优化
使用该代码,我本地没有跑出结果,一直在运行,所以对代码进行下一步优化。
等了好久都木有结果:
由于马儿的不同走法会有不同的策略,如果回溯的话,是比较影响性能的,所有,我们在对马儿走下一步选择的点可以进行优化。
我们可以算出马儿选择走下一步的下一步有几种选择,使用贪心算法,算出最少选择进行下一步,下一步的下一步选择越少,则回溯所消耗的性能越少,所以,我们可以对下一步集合中的点进行排序,选出选择最少的。
优化前没有跑出结果,但是优化后:
通过结果玩游戏,完美win,但是不一定每次都能回到原点:
5.代码实现
package com.example.datastructureandalgorithm.horse;
import java.awt.*;
import java.util.ArrayList;
import java.util.Comparator;
/**
* 马踏棋盘算法
*
* @author 浪子傑
* @version 1.0
* @date 2020/6/27
*/
public class HorseChessDemo {
/**
* 棋盘的行数
*/
private static int X;
/**
* 棋盘的列数
*/
private static int Y;
/**
* 标记棋盘各个位置是否被访问
*/
private static boolean visited[];
/**
* 判断是否成功
*/
private static boolean isFinished;
public static void main(String[] args) {
System.out.println("骑士开始周游啦...");
X = 6;
Y = 6;
// 初始位置行数
int row = 1;
// 初始位置列数
int column = 2;
// 创建棋盘
int[][] chessboard = new int[X][Y];
visited = new boolean[X * Y];
// 耗时
long start = System.currentTimeMillis();
traversalChess(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();
}
}
public static void traversalChess(int[][] chessboard, int row, int column, int step) {
chessboard[row][column] = step;
// row * X + column表示被访问节点的位置,即从0开始数
visited[row * X + column] = true;
// 获取下一步可以走的位置的集合
ArrayList<Point> arrayList = next(new Point(column, row));
// 排序
sort(arrayList);
while (!arrayList.isEmpty()) {
Point point = arrayList.remove(0);
// 判断是否被访问过
if (!visited[point.y * X + point.x]) {
// 记得step+1,表示一共走的步数
traversalChess(chessboard, point.y, point.x, step + 1);
}
}
// 判断马儿是否完成了任务,使用step和棋盘总步数进行比较
// 如果没有完成,则将棋盘还原
if (step < X * Y && !isFinished) {
chessboard[row][column] = 0;
visited[row * X + column] = false;
} else {
isFinished = true;
}
}
/**
* 根据当前的位置,计算马儿还可以走哪些位置,并放入list中,list最大为8
*
* @param point
* @return
*/
public static ArrayList<Point> next(Point point) {
ArrayList<Point> arrayList = new ArrayList<>();
Point p1 = new Point();
// 表示马儿可以走上图中的位置1
if ((p1.x = point.x - 1) >= 0 && (p1.y = point.y - 2) >= 0) {
arrayList.add(new Point(p1));
}
// 表示马儿可以走上图中的位置2
if ((p1.x = point.x + 1) < X && (p1.y = point.y - 2) >= 0) {
arrayList.add(new Point(p1));
}
// 表示马儿可以走上图中的位置3
if ((p1.x = point.x + 2) < X && (p1.y = point.y - 1) >= 0) {
arrayList.add(new Point(p1));
}
// 表示马儿可以走上图中的位置4
if ((p1.x = point.x + 2) < X && (p1.y = point.y + 1) < Y) {
arrayList.add(new Point(p1));
}
// 表示马儿可以走上图中的位置5
if ((p1.x = point.x + 1) < X && (p1.y = point.y + 2) < Y) {
arrayList.add(new Point(p1));
}
// 表示马儿可以走上图中的位置6
if ((p1.x = point.x - 1) >= 0 && (p1.y = point.y + 2) < Y) {
arrayList.add(new Point(p1));
}
// 表示马儿可以走上图中的位置7
if ((p1.x = point.x - 2) >= 0 && (p1.y = point.y + 1) < Y) {
arrayList.add(new Point(p1));
}
// 表示马儿可以走上图中的位置8
if ((p1.x = point.x - 2) >= 0 && (p1.y = point.y - 1) >= 0) {
arrayList.add(new Point(p1));
}
return arrayList;
}
/**
* 对list进行排序
* @param arrayList
*/
public static void sort(ArrayList<Point> arrayList) {
arrayList.sort(new Comparator<Point>() {
@Override
public int compare(Point o1, Point o2) {
int count1 = next(o1).size();
int count2 = next(o2).size();
return count1 - count2;
}
});
}
}