学习数据结构和算法的日常Demo
骑士周游算法介绍
算法分析
代码实现
public class HorseChessBoard {
private static int X = 8; // 棋盘的列数(左右)
private static int Y = 8; // 行数(上下)
// 创建数组,标记棋盘的各个位置是否被访问过
private static boolean visited[] = new boolean[X * Y]; // 初始值都是false
// 使用属性标记是否棋盘的所有位置都被访问过
private static boolean finished; // 为true则访问
public static void main(String args[]) {
int row = 1; // 初始位置的行,从1开始编号
int col = 1; // 初始位置的列,1开始编号
// 创建棋盘
long start = System.currentTimeMillis();
int chessboard[][] = new int[X][Y];
travel(chessboard, row - 1, col - 1, 1);
for (int[] ints : chessboard) {
for (int i : ints) {
System.out.printf("%5d", i);
}
System.out.println()