1、基本介绍
2、应用实例(骑士周游)
package algorithm.horse;
import java.awt.*;
import java.util.*;
public class HorseChessDemo {
private static int X = 8; // 棋盘的行数
private static int Y = 8; // 棋盘的列数
private static boolean[] visited = new boolean[X*Y]; // 用于标记该位置被访问
private static boolean isFinished = false; // 标记棋盘上所有的位置是否都已访问
public static void main(String[] args) {
int[][] chessBoard = new int[X][Y];
int row = 5; // 第一行
int column = 6; // 第一列
int step = 1;
long start = System.currentTimeMillis();
System.out.println("骑士周游~");
traverseChessBoard(chessBoard, row - 1, column - 1, step);
long end = System.currentTimeMillis();
System.out.printf("运行时间:%d毫秒\n\n", end-start);
System.out.println("棋盘如下:");
printChessBoard(chessBoard);
}
private static void traverseChessBoard(int[][] chessBoard, int rowIndex, int columnIndex, int step) {
chessBoard[rowIndex][columnIndex] = step;
visited[rowIndex*X + columnIndex] = true; // 将当前位置设置为已访问(index = 4*8+4)
ArrayList<Point> points = next(new Point(rowIndex, columnIndex)); // Point表示位置,找出根据当前位置可走的其它位置
sort(points); // 通过排序,减少回溯次数(算法核心)
while(!points.isEmpty()) {
Point point = points.remove(0); // 取出下一个位置
if (!visited[point.x * X + point.y]) { // 判断该位置是否已走过
traverseChessBoard(chessBoard, point.x, point.y, step+1);
}
}
// 判断马儿是否完成任务(条件成立的情况:1、棋盘到目前位置,仍然未走完,2、棋盘处于回溯的过程)
if (step < X * Y && !isFinished) {
chessBoard[rowIndex][columnIndex] = 0;
visited[rowIndex*X + columnIndex] = false;
} else {
isFinished = true; // 表示马儿已完成任务,退出循环
}
}
// 通过非递减排序,减少回溯的次数
private static void sort(ArrayList<Point> points) {
points.sort(new Comparator<Point>() {
// 实现非递减排序
@Override
public int compare(Point o1, Point o2) {
if (next(o1).size() < next(o2).size()) {
return -1;
} else if (next(o1).size() == next(o2).size()) {
return 0;
} else {
return 1;
}
}
});
}
// 根据当前位置,计算出马儿还能走哪些位置
private static ArrayList<Point> next(Point currPint) {
ArrayList<Point> points = new ArrayList();
Point point = new Point();
// 判断马儿走5这个位置
if ((currPint.y - 2 >= 0) && (currPint.x - 1 >= 0)) {
point.x = currPint.x - 1;
point.y = currPint.y - 2;
points.add(new Point(point));
}
// 判断马儿走6这个位置
if ((currPint.y - 1 >= 0) && (currPint.x - 2 >= 0)) {
point.x = currPint.x - 2;
point.y = currPint.y - 1;
points.add(new Point(point));
}
// 判断马儿走7这个位置
if ((currPint.y + 1 < Y) && (currPint.x - 2 >= 0)) {
point.x = currPint.x - 2;
point.y = currPint.y + 1;
points.add(new Point(point));
}
// 判断马儿走0这个位置
if ((currPint.y + 2 < Y) && (currPint.x - 1 >= 0)) {
point.x = currPint.x - 1;
point.y = currPint.y + 2;
points.add(new Point(point));
}
// 判断马儿走1这个位置
if ((currPint.y + 2 < Y) && (currPint.x + 1 < X)) {
point.x = currPint.x + 1;
point.y = currPint.y + 2;
points.add(new Point(point));
}
// 判断马儿走2这个位置
if ((currPint.y + 1 < Y) && (currPint.x + 2 < X)) {
point.x = currPint.x + 2;
point.y = currPint.y + 1;
points.add(new Point(point));
}
// 判断马儿走3这个位置
if ((currPint.y - 1 >= 0) && (currPint.x + 2 < X)) {
point.x = currPint.x + 2;
point.y = currPint.y - 1;
points.add(new Point(point));
}
// 判断马儿走4这个位置
if ((currPint.y - 2 >= 0) && (currPint.x + 1 < X)) {
point.x = currPint.x + 1;
point.y = currPint.y - 2;
points.add(new Point(point));
}
return points;
}
private static void printChessBoard(int[][] chessBoard) {
for (int[] temp : chessBoard) {
System.out.println(Arrays.toString(temp));
}
}
}