14.3 马踏棋盘

骑士周游问题的解决步骤和思路
1.创建棋盘chessBoard,是一个二维数组
2.将当前位置设置为已经访问,然后根据当前位置,计算马儿还能走哪些位置,并放入到一个集合中(ArrayList),最多有8个位置,每走一步,就使用step+13.遍历ArrayList中存放的所有位置,看看哪个可以走通,如果走通,就继续,走不通,就回溯.
4.判断马儿是否完成了任务,使用step和应该走的步数比较,如果没有达到数量,则表示没有完成任务,将整个棋盘置0.
注意:马儿不同的走法(策略),会得到不同的结果,效率也会有影响(优化)
在这里插入图片描述
代码如下:

当前位置,下一步能走的集合

//根据当前位置,计算马儿还能走哪些位置,并放入到一个集合中,最多8个位置
    public static ArrayList<Point> next(Point curPoint){
        ArrayList<Point> ps = new ArrayList<>();
        Point p1 = new Point();
        //马儿是否能走5号位置
        if ((p1.x = curPoint.x - 2) >= 0 && (p1.y = curPoint.y - 1) >= 0){
            ps.add(new Point(p1));
        }
        //马儿是否能走6号位置
        if ((p1.x = curPoint.x - 1) >= 0 && (p1.y = curPoint.y - 2) >= 0){
            ps.add(new Point(p1));
        }
        //马儿是否能走7号位置
        if ((p1.x = curPoint.x + 1) < X && (p1.y = curPoint.y - 2) >= 0){
            ps.add(new Point(p1));
        }
        //马儿是否能走0号位置
        if ((p1.x = curPoint.x + 2) < X && (p1.y = curPoint.y - 1) >= 0){
            ps.add(new Point(p1));
        }
        //马儿是否能走1号位置
        if ((p1.x = curPoint.x + 2) < X && (p1.y = curPoint.y + 1) < Y){
            ps.add(new Point(p1));
        }
        //马儿是否能走2号位置
        if ((p1.x = curPoint.x + 1) < X && (p1.y = curPoint.y + 2) < Y){
            ps.add(new Point(p1));
        }
        //马儿是否能走3号位置
        if ((p1.x = curPoint.x - 1) >= 0 && (p1.y = curPoint.y + 2) < Y){
            ps.add(new Point(p1));
        }
        //马儿是否能走4号位置
        if ((p1.x = curPoint.x - 2) >= 0 && (p1.y = curPoint.y + 1) < Y){
            ps.add(new Point(p1));
        }
        return ps;
    }

马踏棋盘算法

//马踏棋盘的算法
    /*
    * chessboard 棋盘
    * row 马儿当前位置的行
    * column 马儿当前位置的列
    * step 马儿走到第几步
    * */
    public static void traverChess(int[][] chessboard, int row, int column, int step){
        //记录第几步走的哪一个位置
        chessboard[row][column] = step;
        //标记改访问的位置
        visited[row * X + column] = true;
        //获取下一步可走位置的集合
        ArrayList<Point> ps = next(new Point(column, row));
        //导入贪心算法进行优化
        sort(ps);
        //遍历ps
        while (!ps.isEmpty()){
            Point p = ps.remove(0);//取出第一个可以走的位置
            //判断该点是否已经访问
            if (!visited[p.y * X + p.x]){
                traverChess(chessboard, p.y, p.x, step + 1);
            }
        }
        if (step < X * Y && !finished){
            chessboard[row][column] = 0;
            visited[row * X + column] =false;
        } else {
            finished = true; //找到正确答案,为了跳出if (step < X * Y && !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();
        traverChess(chessboard, row - 1, column - 1, 1);
        long end = System.currentTimeMillis();
        System.out.println(end - start);

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

结果:
在这里插入图片描述
此算法可以通过贪心算法进行优化,在马进行下一步走法,我们可以选择下一步的下一步集合最少的步骤可以进行优化

贪心算法优化

 //根据当前位置的下一个位置的大小进行非递减排序
    public static void sort(ArrayList<Point> ps){
        ps.sort(new Comparator<Point>() {
            @Override
            public int compare(Point o1, Point o2) {
                int count = next(o1).size();
                int count1 = next(o2).size();
                if (count < count1){
                    return -1;
                } else if (count == count1){
                    return 0;
                } else {
                    return -1;
                }

            }
        });
    }

在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值