马踏棋盘游戏代码实现
package com.wxit.horse;
import java.awt.*;
import java.util.ArrayList;
/**
* @Author wj
**/
public class HorseChessboard {
private static int X; //棋盘的列数
private static int Y; //棋盘的行数
//创建一个数组,标记棋盘各个位置是否被访问过
private static boolean visited[];
//使用一个属性,标记是否棋盘所位置都被访问
private static boolean finished;//如果为true,表示成功
public static void main(String[] args) {
System.out.println("开始测试骑士周游算法");
//测试骑士周游算法是否正确
X = 6;
Y = 6;
int row = 2;//马儿初始位置的行,从1开始编号
int column = 4; //马儿初始位置的列,从1开始编号
//创建棋盘
int[][] chessboard = new int[X][Y];
visited = new boolean[X * Y];//初始值都是false
//测试一下耗时
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();
}
}
/**
* 完成骑士周游问题的算法
* @param chessboard 棋盘
* @param row 马儿当前位置的行 从0开始
* @param column 马儿当前位置的列,从0开始
* @param step 第几步 初始位置就是第一步
*/
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));
//遍历ps
while (!ps.isEmpty()){
Point p = ps.remove(0);//取出下一个还可以走的位置
//判断该点是否已经访问过
if (!visited[p.y * X + p.x]){//说明还没有访问过
traversalChessboard(chessboard,p.y,p.x,step + 1);
}
}
//判断马儿是否完成了任务,使用step和应该走的步数比较,如果没有达到数量,则表示内有完成任务,将整个棋盘置零
//说明:step < X * Y成立的情况有两种
//1.棋盘到目前位置,仍然没有走完
//2.棋盘处于一个回溯的过程
if (step < X * Y && !finished){
chessboard[row][column] = 0;
visited[row * X + column] = false;
} else {
finished = true;
}
}
/**
* 功能:根据当前位置(Point对象),计算马儿还能走那些位置(Point),并放入到一个集合中(ArrayList),最多有8个位置
* @param curPoint
* @return
*/
public static ArrayList<Point> next(Point curPoint){
//创建一个ArrayList
ArrayList<Point> ps = new ArrayList<>();
//创建一个point
Point p1 = new Point();
//表示马儿可以走 5 这个位置
if((p1.x = curPoint.x - 2) >= 0 && (p1.y = curPoint.y -1) >= 0) {
ps.add(new Point(p1));
}
//判断马儿可以走 6 这个位置
if((p1.x = curPoint.x - 1) >=0 && (p1.y=curPoint.y-2)>=0) {
ps.add(new Point(p1));
}
//判断马儿可以走 7 这个位置
if ((p1.x = curPoint.x + 1) < X && (p1.y = curPoint.y - 2) >= 0) {
ps.add(new Point(p1));
}
//判断马儿可以走 0 这个位置
if ((p1.x = curPoint.x + 2) < X && (p1.y = curPoint.y - 1) >= 0) {
ps.add(new Point(p1));
}
//判断马儿可以走 1 这个位置
if ((p1.x = curPoint.x + 2) < X && (p1.y = curPoint.y + 1) < Y) {
ps.add(new Point(p1));
}
//判断马儿可以走 2
if ((p1.x = curPoint.x + 1) < X && (p1.y = curPoint.y + 2) < Y) {
ps.add(new Point(p1));
}
//判断马儿可以走 3 这个位置
if ((p1.x = curPoint.x - 1) >= 0 && (p1.y = curPoint.y + 2) < Y) {
ps.add(new Point(p1));
}
//判断马儿可以走 4 这个位置
if ((p1.x = curPoint.x - 2) >= 0 && (p1.y = curPoint.y + 1) < Y) {
ps.add(new Point(p1));
}
return ps;
}
}