算法介绍和规则演示
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-OWYXozQn-1647610223982)(C:\Users\许正\AppData\Roaming\Typora\typora-user-images\image-20220318173745954.png)]](https://i-blog.csdnimg.cn/blog_migrate/9d0c0f500ee380ad3dc2a55eecebe3eb.png)
思路和步骤
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-81szyS1P-1647610223982)(C:\Users\许正\AppData\Roaming\Typora\typora-user-images\image-20220318174320439.png)]](https://i-blog.csdnimg.cn/blog_migrate/4b9dfe8aacbe7b2a62a491dc16ba01d6.png)
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-ZLeJpujA-1647610223983)(C:\Users\许正\AppData\Roaming\Typora\typora-user-images\image-20220318175210703.png)]](https://i-blog.csdnimg.cn/blog_migrate/adf00aebb2237b31f4e91d7f89ab9076.png)
代码实现
package com.xz.horse;
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;
public static void main(String[] args) {
System.out.println("骑士周游算法,开始运行~~");
X = 8;
Y = 8;
int row = 1;
int column = 1;
int[][] chessboard = new int[X][Y];
visited = new boolean[X * Y];
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();
}
}
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));
sort(ps);
while(!ps.isEmpty()) {
Point p = ps.remove(0);
if(!visited[p.y * X + p.x]) {
traversalChessboard(chessboard, p.y, p.x, step + 1);
}
}
if(step < X * Y && !finished ) {
chessboard[row][column] = 0;
visited[row * X + column] = false;
} else {
finished = true;
}
}
public static ArrayList<Point> next(Point curPoint) {
ArrayList<Point> ps = new ArrayList<Point>();
Point p1 = new Point();
if((p1.x = curPoint.x - 2) >= 0 && (p1.y = curPoint.y -1) >= 0) {
ps.add(new Point(p1));
}
if((p1.x = curPoint.x - 1) >=0 && (p1.y=curPoint.y-2)>=0) {
ps.add(new Point(p1));
}
if ((p1.x = curPoint.x + 1) < X && (p1.y = curPoint.y - 2) >= 0) {
ps.add(new Point(p1));
}
if ((p1.x = curPoint.x + 2) < X && (p1.y = curPoint.y - 1) >= 0) {
ps.add(new Point(p1));
}
if ((p1.x = curPoint.x + 2) < X && (p1.y = curPoint.y + 1) < Y) {
ps.add(new Point(p1));
}
if ((p1.x = curPoint.x + 1) < X && (p1.y = curPoint.y + 2) < Y) {
ps.add(new Point(p1));
}
if ((p1.x = curPoint.x - 1) >= 0 && (p1.y = curPoint.y + 2) < Y) {
ps.add(new Point(p1));
}
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) {
int count1 = next(o1).size();
int count2 = next(o2).size();
if(count1 < count2) {
return -1;
} else if (count1 == count2) {
return 0;
} else {
return 1;
}
}
});
}
}