1203-2019- algorithm - horse riding board algorithm (depth-first traversal, backtracking + final greedy algorithm to be optimized)

The key is to understand the source code back and realize recursive procedure (adding sort ArrayList container) understand the main idea of ​​the optimized greedy algorithm: every step of the election of the current best choice, the best choice is that each horse riding board step of the election of the current minimum of this step by step that the next choice, you can omit a lot of backtracking.

package HorseChessBoard;

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

/**
 * @author pdzz
 * @create 2019-12-03 15:00
 */
public class HorseChessBoard {

    private static int X;
    private static int Y;
    //创建一个数组标记棋盘的各个位置是否被访问过。
    private static boolean[] visited;
    //使用一个属性,标记是否棋盘的所有位置都被访问过。
    private static boolean finished;

    public static void main(String[] args) {
        X = 8;
        Y = 8;
        int row = 1;
        int column = 1;
        int[][] chessBoard = new int[X][Y];
        visited = new boolean[X * Y];
        long start = System.currentTimeMillis();
        traversalChessboard(chessBoard,row - 1,column - 1,1);
        long end = System.currentTimeMillis();
        System.out.println("耗时:" + (end - start));
        for (int i = 0; i < chessBoard.length; i++) {
            for (int j = 0; j < chessBoard[i].length; j++) {
                System.out.print(chessBoard[i][j] + "\t");
            }
            System.out.println();
        }
    }

    public static void traversalChessboard(int[][] chessBoard,int row,int column,int step){
        /**
        *@Description
        *@Param [chessBoard, row, column, step] 棋盘,行,列,是第几步(从0开始)。
        *@Return void
        *@Author pdzz
        *@Date 2019/12/3
        *@Time 15:30
        */
        //棋盘上标出当前位置是哪一步
        chessBoard[row][column] = step;
        visited[row * X + column] = true;
        ArrayList<Point> points = next(new Point(column, row));
        sort(points);
        while (!points.isEmpty()){
            //取出下一个可以走的位置
            Point p = points.remove(0);
            //如果没走过就,递归。走不下去就回溯。
            if (!visited[p.y * X + p.x]){
                traversalChessboard(chessBoard,p.y,p.x,step + 1);
            }
        }
        //判断是否完成了任务
        //1.棋盘还没走完
        //2.棋盘处于一个回溯的过程,finished是true了,step不满足条件,但是已经走完了。
        if (step < X * Y && !finished){
            chessBoard[row][column] = 0;
            visited[row * X + column] = false;
        } else {
            finished = true;
        }
    }

    public static ArrayList<Point> next(Point curPoint){
        ArrayList<Point> points = new ArrayList<>();

        Point p1 = new Point();
        if ((p1.x = curPoint.x - 2) >= 0 && (p1.y = curPoint.y - 1) >= 0){
            points.add(new Point(p1));
        }
        if ((p1.x = curPoint.x - 2) >= 0 && (p1.y = curPoint.y + 1) < Y){
            points.add(new Point(p1));
        }
        if ((p1.x = curPoint.x + 2) < X && (p1.y = curPoint.y - 1) >= 0){
            points.add(new Point(p1));
        }
        if ((p1.x = curPoint.x + 2) < X && (p1.y = curPoint.y + 1) < Y){
            points.add(new Point(p1));
        }
        if ((p1.x = curPoint.x - 1) >= 0 && (p1.y = curPoint.y - 2) >= 0){
            points.add(new Point(p1));
        }
        if ((p1.x = curPoint.x - 1) >= 0 && (p1.y = curPoint.y + 2) < Y){
            points.add(new Point(p1));
        }
        if ((p1.x = curPoint.x + 1) < X && (p1.y = curPoint.y - 2) >= 0){
            points.add(new Point(p1));
        }
        if ((p1.x = curPoint.x + 1) < X && (p1.y = curPoint.y + 2) < Y){
            points.add(new Point(p1));
        }
        return points;
    }
    //根据当前的这一步的下一步的可以走的全部选择数进行非递减排序,减少回溯的次数
    public static void sort(ArrayList<Point> points){
        points.sort(new Comparator<Point>() {
            @Override
            public int compare(Point o1, Point o2) {
                int o1Num = next(o1).size();
                int o2Num = next(o2).size();
                return Integer.compare(o1Num,o2Num);
            }
        });
    }
}

Video Java data structures From Silicon Valley is still Hanshun Ping teacher

Published 98 original articles · won praise 0 · Views 2228

Guess you like

Origin blog.csdn.net/weixin_43221993/article/details/103371808