您当前的位置: 首页 >  Java

小志的博客

暂无认证

  • 0浏览

    0关注

    1217博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

java数据结构和算法——马踏棋盘算法

小志的博客 发布时间:2020-12-14 20:35:23 ,浏览量:0

目录
    • 一、马踏棋盘算法介绍
    • 二、骑士周游问题的思路分析
    • 三、骑士周游问题代码示例

一、马踏棋盘算法介绍
  • 马踏棋盘算法也被称为骑士周游问题
  • 将马随机放在国际象棋的8×8棋盘Board[0~7][0~7]的某个方格中,马按走棋规则(马走日字)进行移动。要求每个方格只进入一次,走遍棋盘上全部64个方格
二、骑士周游问题的思路分析

在这里插入图片描述

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

1、代码

package com.rf.data_structure_algorithm.algorithm.horseChessBoard;

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

/**
 * @description: 骑士周游算法示例
 * @author: xz
 */
public class HorseChessBoard {

     static int X;//棋盘的列数
     static int Y;//棋盘的行数
     static boolean visited[]; //标记棋盘的各个位置是否被访问过
     static boolean finished; // 标记是否棋盘的所有位置都被访问 true:成功,false:失败

    public static void main(String[] args) {
        System.out.println("骑士周游算法,开始运行~~");
        X=8;
        Y=8;
        int row=1;//马初始位置的行,从编号1开始
        int column=1;//马初始位置的列,从编号1开始
        //创建棋盘
        int[][] chessboard=new int[X][Y];
        visited=new boolean[X*Y];//初始值都是false
        //测试
        long startTime = System.currentTimeMillis();
        horseChessBoardAlgorithm(chessboard,row-1,column-1,1);
        long endTime = System.currentTimeMillis();
        System.out.println("总共耗时:"+(endTime-startTime)+"毫秒");
        System.out.println("输出棋盘的最后情况============");
        //输出棋盘的最后情况
        for(int[] rows : chessboard){
            for(int step : rows){
                System.out.print(step + "\t");
            }
            System.out.println();
        }

    }

    /** 
    * @Description: 根据当前位置(Point),计算马还能走哪些位置(Point)
     *                并放入到一个集合中(ArrayList),最多有8个位置
    * @Param:  curPoint
    * @Author: xz  
    */
    public static ArrayList next(Point curPoint){
        //创建一个ArrayList
        ArrayList list =new ArrayList();
        //创建一个Point
        Point point=new Point();

        //curPoint.x-2 表示当前位置(curPoint)的列向左移动2列
        //curPoint.x+2 表示当前位置(curPoint)的列向右移动2列
        //curPoint.y-1 表示当前位置(curPoint)的列向上移动1行
        //curPoint.y+1 表示当前位置(curPoint)的列向下移动1行
        // >= 0 表示仍然有空间可走
        if((point.x = curPoint.x-2) >= 0 && (point.y = curPoint.y-1) >= 0 ){//示例图中指定马可以走5的位置
            list.add(new Point(point));
        }
        if((point.x = curPoint.x - 1) >=0 && (point.y=curPoint.y-2)>=0) {//示例图中指定马可以走6的位置
            list.add(new Point(point));
        }
        if ((point.x = curPoint.x + 1) = 0) {//示例图中指定马可以走7的位置
            list.add(new Point(point));
        }
        if ((point.x = curPoint.x + 2) = 0) {//示例图中指定马可以走0的位置
            list.add(new Point(point));
        }
        if ((point.x = curPoint.x + 2)             
关注
打赏
1661269038
查看更多评论
0.0897s