思路: 就是递归。先扫描所有可以走的点,然后利用遍历和递归。发现计算器等于格子数时完成。
优化: 遍历和递归的基础上再加贪心算法,第 i 步的位置按照 i+1 步可以落子位置的数量进行升序排列(优先遍历 i+1 步位置少的,以便减少遍历次数)
我这里没有优化,优化就是把我第二个方法再封装一个方法,稍微改动即可。
import java.util.ArrayList;
/**
* @author 王木风
* @create 2020-08-30 12:19
*/
public class HorseChessboard {
//计数器
static int step = 1;
public static void main(String[] args) {
int[][] checkerBoard = new int[8][8];
coreMethod(checkerBoard,0,0);
//验证
for (int[] ints :checkerBoard) {
for (int i :ints) {
System.out.print(i+"\t");
}
System.out.println();
}
System.out.println(step);
}
public static void coreMethod(int[][] checkerBoard,int i,int j) {
//记录该点是第几步
checkerBoard[i][j] = step;
//存放下一步落子位置的x,y
ArrayList<int[]> ints = new ArrayList<>();
if (i + 2 < 8 && j + 1 < 8 && checkerBoard[i+2][j+1] == 0) {
ints.add(new int[]{i+2,j+1});
}
if (i + 1 < 8 && j + 2 < 8 && checkerBoard[i+1][j+2] == 0) {
ints.add(new int[]{i+1,j+2});
}
if (i - 1 < 8 && j + 2 < 8 && i - 1 >= 0 && checkerBoard[i - 1][j + 2] == 0 ) {
ints.add(new int[]{i - 1, j + 2});
}
if (i - 2 < 8 && j + 1 < 8 && i - 2 >= 0 && checkerBoard[i - 2][j + 1] == 0) {
ints.add(new int[]{i - 2, j + 1});
}
if (i - 2 < 8 && j - 1 < 8 && i - 2 >= 0 && j - 1 >= 0 && checkerBoard[i - 2][j - 1] == 0) {
ints.add(new int[]{i - 2, j - 1});
}
if (i - 1 < 8 && j - 2 < 8 && i - 1 >= 0 && j - 2 >= 0 && checkerBoard[i - 1][j - 2] == 0) {
ints.add(new int[]{i - 1, j - 2});
}
if (i + 1 < 8 && j - 2 < 8 && j - 2 >= 0 && checkerBoard[i + 1][j - 2] == 0) {
ints.add(new int[]{i + 1, j - 2});
}
if (i + 2 < 8 && j - 1 < 8 && j - 1 >= 0 && checkerBoard[i + 2][j - 1] == 0) {
ints.add(new int[]{i + 2, j - 1});
}
//遍历可以落子位置
for (int[] arr :ints) {
if (step < 64) {
step++;
//递归
coreMethod(checkerBoard, arr[0], arr[1]);
}
}
if (step == 64) {
return;
} else {
//如果无位置可落,则计数器退1.该点置零
step--;
checkerBoard[i][j] = 0;
}
}
}