利用Java解决骑士周游问题

利用Java解决骑士周游问题

概述

骑士周游问题是将马随机放在国际象棋8x8的棋盘中,马按照走步规则进行走动,要求马只能进入每个方格一次,不能重复走进已走过的方格,并走遍棋盘上所有的64个方格。

解答过程

1、建立一个二维数组模拟棋盘
//建立一个8x8的棋盘
public class chess(){
    private static int X = 8;
    private static int Y = 8;
    private static int[][] chessBoard = new int[X][Y];
    
    public static void mian(String[] args){
        //遍历棋盘
        for (int i = 0; i < X; i++) {
            for (int j = 0; j < Y; j++) {
                System.out.print(chessBoard[i][j] + "\t");
            }
            System.out.println();
        }
    }
}

8x8的棋盘

在这里插入图片描述

2、跟据马的行走规则,如图所示的马所处的位置下一步只能走到1到8位置中的任意一个位置,由这个规律可以判断马的下一步的位置是否可走。
//当前的位置可以走下一步的位置
public static ArrayList<Point> nextLocation(Point currentPoint) {
    //建立一个列表存放可以走的位置
    ArrayList<Point> pointList = new ArrayList<Point>();
    Point p = new Point();
    //判断1位置是否可走
    if ((p.x = currentPoint.x - 1) >= 0 && (p.y = currentPoint.y - 2) >= 0) {
        pointList.add(new Point(p));
    }
    //判断2位置是否可走
    if ((p.x = currentPoint.x + 1) < X && (p.y = currentPoint.y - 2) >= 0) {
        pointList.add(new Point(p));
    }
    //判断3位置是否可走
    if ((p.x = currentPoint.x + 2) < X && (p.y = currentPoint.y - 1) >= 0) {
        pointList.add(new Point(p));
    }
    //判断4位置是否可走
    if ((p.x = currentPoint.x + 2) < X && (p.y = currentPoint.y + 1) < Y) {
        pointList.add(new Point(p));
    }
    //判断5位置是否可走
    if ((p.x = currentPoint.x + 1) < X && (p.y = currentPoint.y + 2) < Y) {
        pointList.add(new Point(p));
    }
    //判断6位置是否可走
    if ((p.x = currentPoint.x - 1) >= 0 && (p.y = currentPoint.y + 2) < Y) {
        pointList.add(new Point(p));
    }
    //判断7位置是否可走
    if ((p.x = currentPoint.x - 2) >= 0 && (p.y = currentPoint.y + 1) < Y) {
        pointList.add(new Point(p));
    }
    //判断8位置是否可走
    if ((p.x = currentPoint.x - 2) >= 0 && (p.y = currentPoint.y - 1) >= 0) {
        pointList.add(new Point(p));
    }
    return pointList;
}
3、通过深度优先遍历、递归和回溯法实现对马可走位置进行遍历查找
//遍历下一步可以走的位置和期盘
public static void travelChessBoard(int[][] chessBoard, int row, int col, int step) {
    //记录当前点位置的步数
    chessBoard[row][col] = step;
    //设置当前点位置已访问
    visited[row * X + col] = true;
    //获取当前位置可以走下一步的位置点集合
    ArrayList<Point> points = nextLocation(new Point(col, row));
    //遍历所有可以走的点
    while (!points.isEmpty()) {
        //取出一个点
        Point p = points.remove(0);
        //如果该点没走过,递归遍历下一步所走的位置集合
        if (!visited[p.y * X + p.x]) {
            travelChessBoard(chessBoard, p.y, p.x, step + 1);
        }
    }
    //遍历完所有可以走的位置后,判断马是否走完所有的方格,如果没有成功则进行回溯继续遍历查找
    if (step < X * Y && !finished) {
        //重置当前位置步数
        chessBoard[row][col] = step;
        //重置当前位置没被访问
        visited[row * X + col] = false;
    } else {
        finished = true;
    }
}
4、进行测试检验是否达到目的
public class KnightTravel(){
    private static int X = 8;
    private static int Y = 8;
    private static int[][] chessBoard = new int[X][Y];
     //记录某个点是否走过
    private static boolean[] visited = new boolean[X * Y];
    //判断是否遍历完数组
    private static boolean finished = false;
    
    public static void mian(String[] args){
        //当前马的位置
   		int row = 3;
        int col = 5;
		//执行
        long start = System.currentTimeMillis();
        travelChessBoard(chessBoard, row - 1, col - 1, 1);
        long end = System.currentTimeMillis();
        //测试耗时时间
        System.out.println("耗时=" + (end - start));
    
        //遍历输出棋盘
        for (int i = 0; i < X; i++) {
            for (int j = 0; j < Y; j++) {
                System.out.print(chessBoard[i][j] + "\t");
            }
            System.out.println();
        }
    }
}

但是发现等了好久都没有出现结果,也没有报错,这并并不是我们的程序有问题,是因为这时的算法并不是最优算法,解决问题的速度有点慢,最后还是没耐心等结果了,下面让我们优化一下算法。

5、使用贪心算法进行优化

首先对当前位置的下一个可以走的位置的数量进行从小到大排序,先走下一个位置数量少的位置,如果走不通则往下一个继续走,以此类推,知道成功为止。

//对当前的位置可以走下一步的位置,可以走的位置从小到大排序
public static void sort(ArrayList<Point> pointList) {
    pointList.sort(new Comparator<Point>() {
        @Override
        public int compare(Point o1, Point o2) {
            return nextLocation(o1).size() - nextLocation(o2).size();
        }
    });
}
//遍历下一步可以走的位置和期盘
public static void travelChessBoard(int[][] chessBoard, int row, int col, int step) {
    //记录当前点位置的步数
    chessBoard[row][col] = step;
    //设置当前点位置已访问
    visited[row * X + col] = true;
    //获取当前位置可以走下一步的位置点集合
    ArrayList<Point> points = nextLocation(new Point(col, row));
    //使用贪心算法优化
	sort(points);
    //遍历所有可以走的点
    while (!points.isEmpty()) {
        //取出一个点
        Point p = points.remove(0);
        //如果该点没走过,递归遍历下一步所走的位置集合
        if (!visited[p.y * X + p.x]) {
            travelChessBoard(chessBoard, p.y, p.x, step + 1);
        }
    }
    //遍历完所有可以走的位置后,判断马是否走完所有的方格,如果没有成功则进行回溯继续遍历查找
    if (step < X * Y && !finished) {
        //重置当前位置步数
        chessBoard[row][col] = step;
        //重置当前位置没被访问
        visited[row * X + col] = false;
    } else {
        finished = true;
    }
}

代码执行结果

在这里插入图片描述

最后通过上面代码测试结果显示,此次的执行速度明显快了很多,只用了7毫秒就可以执行完毕,得到了每下一步要走的位置结果。所以对刚才的代码进行优化还是很有必要的,这才体现算法的魅力。

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值