package com.kingdz.algorithm.time201704;
import java.util.ArrayList;
import java.util.List;
/**
* 马踏棋盘递归版本,不确定是不是对的
*
* @author kingdz
*
*/
public class Algo21 {
private static List<ChessPoint> move;
static int curStep = 0;
static {
move = new ArrayList<ChessPoint>();
move.add(new ChessPoint(-1, -2));
move.add(new ChessPoint(-2, -1));
move.add(new ChessPoint(-2, 1));
move.add(new ChessPoint(-1, 2));
move.add(new ChessPoint(1, 2));
move.add(new ChessPoint(2, 1));
move.add(new ChessPoint(2, -1));
move.add(new ChessPoint(1, -2));
}
public static void main(String[] args) {
int[][] chess = new int[8][8];
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) {
chess[i][j] = 0;
}
}
travel(new ChessPoint(0, 0), chess);
}
private static void travel(ChessPoint now, int[][] chess) {
curStep++;
if (curStep > 64) {
return;
} else {
chess[now.getX()][now.getY()] = curStep;
for (int i = 0; i < move.size(); i++) {
Algo20.printChess(chess);
System.out.println("---------------------");
ChessPoint cp = move.get(i);
int x = now.getX() + cp.getX();
int y = now.getY() + cp.getY();
if (x < 0 || x > 7) {
continue;
} else if (y < 0 || y > 7) {
continue;
} else if (chess[x][y] != 0) {
continue;
}
travel(new ChessPoint(x, y), chess);
}
chess[now.getX()][now.getY()] = 0;
curStep--;
}
}
}
【算法】程序猿不写代码是不对的43
最新推荐文章于 2023-06-14 14:48:29 发布