[算法] 马踏棋盘 使用贪心算法

package com.guigu.horse;

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

/**
 * @author: guorui fu
 * @versiion: 1.0
 */
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 = 8;
        int column = 8;

        int[][] chessBoard = new int[Y][X];
        visited = new boolean[X * Y];
        System.out.println(System.currentTimeMillis());
        traversalChessBoard(chessBoard,row - 1,column - 1,1);
        System.out.println(System.currentTimeMillis());

        for (int[] rows : chessBoard) {
            for (int step : rows) {
                System.out.print(step + "\t");
            }
            System.out.println();
        }
    }

    //骑士周游算法 传入棋盘 当前位置 步数
    public static void traversalChessBoard(int[][] chessBoard,int row,int column,int step){
        chessBoard[row][column] = step;
        //row * X + column 标记二维棋盘
        visited[row * X + column] = true;
        //获取当前位置可以走的下一个位置
        ArrayList ps = next(new Point(column, row));
        //非递减排序 先从选择少的开始
        sort(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
        if (step < X * Y && !finished){
            chessBoard[row][column] = 0;
            visited[row * X + column] = false;
        }else {
            finished = true;//等于true回溯时就不用在进入上面的条件了
        }
    }

    //根据当前位置,计算还能走哪些位置
    public static ArrayList next(Point curPoint){
        //创建一个ArrayList
        ArrayList ps = new ArrayList<>();
        //创建一个Point
        Point p1 = new Point();
        //码可以走左二上一
        if ((p1.x = curPoint.x - 2) >= 0 && (p1.y = curPoint.y - 1) >= 0){
            ps.add(new Point(p1));
        }
        //左1上二
        if ((p1.x = curPoint.x - 1) >= 0 && (p1.y = curPoint.y - 2) >= 0){
            ps.add(new Point(p1));
        }
        //右1上2
        if ((p1.x = curPoint.x + 1) < X && (p1.y = curPoint.y - 2) >= 0){
            ps.add(new Point(p1));
        }
        //右2上1
        if ((p1.x = curPoint.x + 2) < X && (p1.y = curPoint.y - 1) >= 0){
            ps.add(new Point(p1));
        }
        //右2下1
        if ((p1.x = curPoint.x + 2) < X && (p1.y = curPoint.y + 1) < Y){
            ps.add(new Point(p1));
        }
        //右1下2
        if ((p1.x = curPoint.x + 1) < X && (p1.y = curPoint.y + 2) < Y){
            ps.add(new Point(p1));
        }
        //左1下2
        if ((p1.x = curPoint.x - 1) >= 0 && (p1.y = curPoint.y + 2) < Y){
            ps.add(new Point(p1));
        }
        //左2下1
        if ((p1.x = curPoint.x - 2) >= 0 && (p1.y = curPoint.y + 1) > Y){
            ps.add(new Point(p1));
        }
        return ps;
    }

    //根据当前下一步能走的所有位置 进行可走数的非递减排序
    public static void sort(ArrayList ps){
        ps.sort(new Comparator() {
            @Override
            public int compare(Point o1, Point o2) {
                int count1 = next(o1).size();
                int count2 = next(o2).size();
                if (count1 < count2){
                    return -1;
                }else if (count1 == count2){
                    return 0;
                }else {
                    return 1;
                }
            }
        });
    }
}

你可能感兴趣的:(算法,贪心算法,java)