马踏棋盘算法

1、基本介绍

 

2、应用实例(骑士周游)

package algorithm.horse;

import java.awt.*;
import java.util.*;

public class HorseChessDemo {
    private static int X = 8;                            // 棋盘的行数
    private static int Y = 8;                            // 棋盘的列数
    private static boolean[] visited = new boolean[X*Y]; // 用于标记该位置被访问
    private static boolean isFinished = false;           // 标记棋盘上所有的位置是否都已访问
    public static void main(String[] args) {
        int[][] chessBoard = new int[X][Y];
        int row = 5;    // 第一行
        int column = 6; // 第一列
        int step = 1;
        long start = System.currentTimeMillis();
        System.out.println("骑士周游~");
        traverseChessBoard(chessBoard, row - 1, column - 1, step);
        long end = System.currentTimeMillis();
        System.out.printf("运行时间:%d毫秒\n\n", end-start);
        System.out.println("棋盘如下:");
        printChessBoard(chessBoard);
    }

    private static void traverseChessBoard(int[][] chessBoard, int rowIndex, int columnIndex, int step) {
        chessBoard[rowIndex][columnIndex] = step;
        visited[rowIndex*X + columnIndex] = true;    // 将当前位置设置为已访问(index = 4*8+4)
        ArrayList<Point> points = next(new Point(rowIndex, columnIndex)); // Point表示位置,找出根据当前位置可走的其它位置
        sort(points); // 通过排序,减少回溯次数(算法核心)
        while(!points.isEmpty()) {
            Point point = points.remove(0);  //  取出下一个位置
            if (!visited[point.x * X + point.y]) { // 判断该位置是否已走过
                traverseChessBoard(chessBoard, point.x, point.y, step+1);
            }
        }
        //  判断马儿是否完成任务(条件成立的情况:1、棋盘到目前位置,仍然未走完,2、棋盘处于回溯的过程)
        if (step < X * Y && !isFinished) {
            chessBoard[rowIndex][columnIndex] = 0;
            visited[rowIndex*X + columnIndex] = false;
        } else {
            isFinished = true;  // 表示马儿已完成任务,退出循环
        }
    }

    //  通过非递减排序,减少回溯的次数
    private static void sort(ArrayList<Point> points) {
        points.sort(new Comparator<Point>() {
            //  实现非递减排序
            @Override
            public int compare(Point o1, Point o2) {
                if (next(o1).size() < next(o2).size()) {
                    return -1;
                } else if (next(o1).size() == next(o2).size()) {
                    return 0;
                } else {
                    return 1;
                }
            }
        });
    }

    //  根据当前位置,计算出马儿还能走哪些位置
    private static ArrayList<Point> next(Point currPint) {
        ArrayList<Point> points = new ArrayList();
        Point point = new Point();
        //  判断马儿走5这个位置
        if ((currPint.y - 2 >= 0) && (currPint.x - 1 >= 0)) {
            point.x = currPint.x - 1;
            point.y = currPint.y - 2;
            points.add(new Point(point));
        }
        //  判断马儿走6这个位置
        if ((currPint.y - 1 >= 0) && (currPint.x - 2 >= 0)) {
            point.x = currPint.x - 2;
            point.y = currPint.y - 1;
            points.add(new Point(point));
        }
        //  判断马儿走7这个位置
        if ((currPint.y + 1 < Y) && (currPint.x - 2 >= 0)) {
            point.x = currPint.x - 2;
            point.y = currPint.y + 1;
            points.add(new Point(point));
        }
        //  判断马儿走0这个位置
        if ((currPint.y + 2 < Y) && (currPint.x - 1 >= 0)) {
            point.x = currPint.x - 1;
            point.y = currPint.y + 2;
            points.add(new Point(point));
        }
        //  判断马儿走1这个位置
        if ((currPint.y + 2 < Y) && (currPint.x + 1 < X)) {
            point.x = currPint.x + 1;
            point.y = currPint.y + 2;
            points.add(new Point(point));
        }
        //  判断马儿走2这个位置
        if ((currPint.y + 1 < Y) && (currPint.x + 2 < X)) {
            point.x = currPint.x + 2;
            point.y = currPint.y + 1;
            points.add(new Point(point));
        }
        //  判断马儿走3这个位置
        if ((currPint.y - 1 >= 0) && (currPint.x + 2 < X)) {
            point.x = currPint.x + 2;
            point.y = currPint.y - 1;
            points.add(new Point(point));
        }
        //  判断马儿走4这个位置
        if ((currPint.y - 2 >= 0) && (currPint.x + 1 < X)) {
            point.x = currPint.x + 1;
            point.y = currPint.y - 2;
            points.add(new Point(point));
        }
        return points;
    }

    private static void printChessBoard(int[][] chessBoard) {
        for (int[] temp : chessBoard) {
            System.out.println(Arrays.toString(temp));
        }
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

来得晚一些也行

观众老爷,请赏~

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值