骑士周游回溯算法

主要思想:深度优先遍历+回溯+贪心。从初始位置startPoint开始,获取下一步能到达的所有位置,将它们添加到集合ArrayList< Point >中,根据它们下一步所能到达的位置的个数k对ArrayList< Point >中所有位置进行非递减排序,优先对k较小的位置进行遍历,若此路不通,则回溯。

马踏棋盘游戏中马能够走的8个位置如下图所示

在这里插入图片描述
本次使用的实例

在这里插入图片描述

代码实现

package DataStructure;

import java.awt.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;

public class HorseChessDemo {
    public static void main(String[] args) {
        long start = System.currentTimeMillis();
        moveHorse(new Point(1, 0), 1);
        long end = System.currentTimeMillis();
        System.out.println("耗时:" + (end - start) + " ms");
        for (int[] row : chessBoard) {
            System.out.println(Arrays.toString(row));
        }
        /*
        耗时:4 ms
        [12, 1, 32, 21, 10, 7]
        [33, 20, 11, 8, 35, 22]
        [2, 13, 34, 31, 6, 9]
        [19, 30, 17, 36, 23, 26]
        [14, 3, 28, 25, 16, 5]
        [29, 18, 15, 4, 27, 24]
         */
    }

    // 棋盘大小为6x6
    public static final int X = 6;
    public static final int Y = 6;
    public static int[][] chessBoard = new int[Y][X];
    // 使用一位数组记录位置是否已访问
    public static boolean[] isVisited = new boolean[X * Y];
    // 标记是否走完了所有位置
    public static boolean finish = false;

    // 获取下一步能走的所有位置的集合
    public static ArrayList<Point> getNextPoints(Point curPoint) {
        Point p1 = new Point();
        ArrayList<Point> points = new ArrayList<>();
        //表示马儿可以走5这个位置
        if ((p1.x = curPoint.x - 2) >= 0 && (p1.y = curPoint.y - 1) >= 0) {
            points.add(new Point(p1));
        }
        //判断马儿可以走6这个位置
        if ((p1.x = curPoint.x - 1) >= 0 && (p1.y = curPoint.y - 2) >= 0) {
            points.add(new Point(p1));
        }
        //判断马儿可以走7这个位置
        if ((p1.x = curPoint.x + 1) < X && (p1.y = curPoint.y - 2) >= 0) {
            points.add(new Point(p1));
        }
        //判断马儿可以走0这个位置
        if ((p1.x = curPoint.x + 2) < X && (p1.y = curPoint.y - 1) >= 0) {
            points.add(new Point(p1));
        }
        //判断马儿可以走1这个位置
        if ((p1.x = curPoint.x + 2) < X && (p1.y = curPoint.y + 1) < Y) {
            points.add(new Point(p1));
        }
        //判断马儿可以走2这个位置
        if ((p1.x = curPoint.x + 1) < X && (p1.y = curPoint.y + 2) < Y) {
            points.add(new Point(p1));
        }
        //判断马儿可以走3这个位置
        if ((p1.x = curPoint.x - 1) >= 0 && (p1.y = curPoint.y + 2) < Y) {
            points.add(new Point(p1));
        }
        //判断马儿可以走4这个位置
        if ((p1.x = curPoint.x - 2) >= 0 && (p1.y = curPoint.y + 1) < Y) {
            points.add(new Point(p1));
        }
        return points;
    }

    /**
     * @param startPoint 起始位置
     * @param step 起始位置step=1,每走一步step+1
     */
    public static void moveHorse(Point startPoint, int step) {
        chessBoard[startPoint.y][startPoint.x] = step;
        // 一维数组表示二维数组,节省查询时间
        isVisited[startPoint.y * X + startPoint.x] = true;
        // 获取下一步能走的位置
        ArrayList<Point> nextPoints = getNextPoints(startPoint);
        // 按照下一步能走位置的个数对nextPoints中所有位置进行非递减排序(贪心算法,优先探查下一步能走位置较少的情况,可减少回溯次数)
        sort(nextPoints);
        while (!nextPoints.isEmpty()) {
            Point p = nextPoints.remove(0);
            if (!isVisited[p.y * X + p.x]) {
                moveHorse(p, step + 1);
            }
        }
        // 若该位置的所有下一步都已访问过,则要判断step
        // 若step < X * Y 且finish = false 则说明此路不通,需回溯,将该位置重新设置为未访问,棋盘相应位置重设为0
        // 若step >= X * Y 说明已完成任务,设置finish = true 依次退出递归调用,程序结束。
        if (step < X * Y && !finish) {
            chessBoard[startPoint.y][startPoint.x] = 0;
            isVisited[startPoint.y * X + startPoint.x] = false;
        }else {
            finish = true;
        }
    }

    // 非递减排序
    public static void sort(ArrayList<Point> points) {
        points.sort(new Comparator<Point>() {
            @Override
            public int compare(Point o1, Point o2) {
                // 位置o1的下一步能走的位置的个数
                int count1 = getNextPoints(o1).size();
                // 位置o2的下一步能走的位置的个数
                int count2 = getNextPoints(o2).size();
                return count1 - count2;
            }
        });
    }

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值