最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

java實(shí)現(xiàn)馬踏棋盤的算法

 更新時(shí)間:2022年02月14日 15:06:05   作者:努力呀kk  
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)馬踏棋盤的算法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了java實(shí)現(xiàn)馬踏棋盤的具體代碼,供大家參考,具體內(nèi)容如下

馬踏棋盤算法介紹

8X8棋盤,馬走日字,要求每個(gè)方格只進(jìn)入一次,走遍棋盤上全部64個(gè)方格。

代碼:

public class HorseChessBoard {
? ? private static int X;//棋盤的列數(shù)
? ? private static int Y;//棋盤的行數(shù)
? ? //創(chuàng)建一個(gè)數(shù)組,標(biāo)記棋盤的各個(gè)位置是否被訪問過
? ? private static boolean visited[];
? ? //使用一個(gè)屬性,標(biāo)記是否棋盤的所有位置都被訪問
? ? private static boolean finished;//如果為true,表示成功

? ? public static void main(String[] args) {
? ? ? ? System.out.println("騎士周游算法");
? ? ? ? //測試騎士周游算法是否正確
? ? ? ? X = 8;
? ? ? ? Y = 8;
? ? ? ? int row = 1;//初始位置的行,從1開始編號(hào)
? ? ? ? int column = 1;//初始位置的列,從1開始編號(hào)
? ? ? ? //創(chuàng)建棋盤
? ? ? ? int[][] chessboard = new int[X][Y];
? ? ? ? visited = new boolean[X*Y];//初始值都是false
? ? ? ? //測試一下耗時(shí)
? ? ? ?long start = System.currentTimeMillis();
? ? ? ?traversalChessboard(chessboard, row-1, column-1,1);
? ? ? ?long end = ?System.currentTimeMillis();
? ? ? ? System.out.println("共耗時(shí):" +( end-start)+"毫秒");

? ? ? ? //輸出棋盤的最后情況
? ? ? ? for (int[] rows : chessboard){
? ? ? ? ? ? for (int step : rows){
? ? ? ? ? ? ? ? System.out.print(step + "\t");
? ? ? ? ? ? }
? ? ? ? ? ? System.out.println();
? ? ? ? }
? ? }

? ? /**
? ? ?* 完成騎士周游問題的算法
? ? ?* @param chessboard 棋盤
? ? ?* @param row 馬兒當(dāng)前的位置的行 從0開始
? ? ?* @param column 馬兒當(dāng)前的位置的列 從0開始
? ? ?* @param step 第幾步,初始位置是第1步
? ? ?*/
? ? public static ?void traversalChessboard(int[][] chessboard, int row, int column, int step){
? ? ? ? chessboard[row][column] = step;

? ? ? ? visited[row * X + column] = true;//標(biāo)記該位置已訪問
? ? ? ? //獲取當(dāng)前位置可以走的下一個(gè)位置的集合
? ? ? ? ArrayList<Point> ps = ?next(new Point(column, row));

? ? ? ? //對ps進(jìn)行排序,排序的規(guī)則就是對所有的Point對象的下一步的位置的數(shù)目進(jìn)行非遞減排序;
? ? ? ? sort(ps);

? ? ? ? //遍歷ps
? ? ? ? while (!ps.isEmpty()){
? ? ? ? ? ? Point p = ps.remove(0);//取出下一個(gè)可以走的位置
? ? ? ? ? ? //判斷該點(diǎn)是否已經(jīng)訪問過
? ? ? ? ? ? if (!visited[p.y * X + p.x]){//說明還沒有訪問過
? ? ? ? ? ? ? ? traversalChessboard(chessboard, p.y, p.x, step + 1);

? ? ? ? ? ? }
? ? ? ? }
? ? ? ? //判斷是否完成任務(wù),使用step和應(yīng)該走的步數(shù)比較
? ? ? ? //如果沒有達(dá)到數(shù)量,則表示沒有完成任務(wù),將整個(gè)棋盤置0
? ? ? ? //說明:step < X * Y成立:①棋盤到目前為止仍然沒有走完;②棋盤處于一個(gè)回溯過程
? ? ? ? if (step < X * Y && !finished){
? ? ? ? ? ? chessboard[row][column] = 0;
? ? ? ? ? ? visited[row * X + column] = false;
? ? ? ? }else {
? ? ? ? ? ? finished = true;
? ? ? ? }
? ? }

? ? /**
? ? ?* 功能:根據(jù)當(dāng)前位置(Point對象),計(jì)算馬兒還能走哪些位置(Point),并放入到一個(gè)集合中ArrayList,最多有八個(gè)位置
? ? ?*
? ? ?* @param curPoint
? ? ?* @return
? ? ?*/
? ? public static ArrayList<Point> next(Point curPoint) {
? ? ? ? //創(chuàng)建一個(gè)ArrayList
? ? ? ? ArrayList<Point> ps = new ArrayList<Point>();
? ? ? ? //創(chuàng)建一個(gè)Point
? ? ? ? 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;
? ? ? ? }
? ? ? ? //根據(jù)當(dāng)前這一步的所有的下一步的選擇位置,進(jìn)行非遞減排序,減少回溯次數(shù)
? ? ? ? public static void sort(ArrayList<Point> ps){
? ? ? ? ps.sort(new Comparator<Point>() {
? ? ? ? ? ? @Override
? ? ? ? ? ? public int compare(Point o1, Point o2) {
? ? ? ? ? ? ? ? //先獲取o1的下一步的所有位置的個(gè)數(shù)
? ? ? ? ? ? ? ?int count1 = next(o1).size();
? ? ? ? ? ? ? ? //獲取o2的下一步的所有位置的個(gè)數(shù)
? ? ? ? ? ? ? ? int count2 = next(o2).size();
? ? ? ? ? ? ? if (count1 < count2){
? ? ? ? ? ? ? ? ? return -1;
? ? ? ? ? ? ? }else if(count1 == count2){
? ? ? ? ? ? ? ? ? return 0;
? ? ? ? ? ? ? ? }else {
? ? ? ? ? ? ? ? ? return 1;
? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? });
? ? }
? ? }

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • java Timer測試定時(shí)調(diào)用及固定時(shí)間執(zhí)行代碼示例

    java Timer測試定時(shí)調(diào)用及固定時(shí)間執(zhí)行代碼示例

    這篇文章主要介紹了java Timer測試定時(shí)調(diào)用及固定時(shí)間執(zhí)行代碼示例,分享了相關(guān)代碼示例,小編覺得還是挺不錯(cuò)的,具有一定借鑒價(jià)值,需要的朋友可以參考下
    2018-02-02
  • 使用@CacheEvict?多參數(shù)如何匹配刪除

    使用@CacheEvict?多參數(shù)如何匹配刪除

    這篇文章主要介紹了使用@CacheEvict?多參數(shù)如何匹配刪除,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-12-12
  • mybatis-plus復(fù)合主鍵的使用

    mybatis-plus復(fù)合主鍵的使用

    本文主要介紹了mybatis-plus復(fù)合主鍵的使用,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-03-03
  • 使用ftpClient下載ftp上所有文件解析

    使用ftpClient下載ftp上所有文件解析

    最近項(xiàng)目需要寫個(gè)小功能,需求就是實(shí)時(shí)下載ftp指定文件夾下的所有文件(包括子目錄)到本地文件夾中,保留文件到目錄路徑不變。今天小編給大家分享使用ftpClient下載ftp上所有文件的方法,需要的的朋友參考下吧
    2017-04-04
  • 詳細(xì)聊一聊java中封裝的那點(diǎn)事

    詳細(xì)聊一聊java中封裝的那點(diǎn)事

    封裝是一個(gè)非常廣泛的概念,小到一個(gè)屬性的封裝,大到一個(gè)框架或者一個(gè)項(xiàng)目的封裝,下面這篇文章主要給大家介紹了關(guān)于java中封裝的那點(diǎn)事,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2022-04-04
  • Java中Optional的使用指南

    Java中Optional的使用指南

    這篇文章主要給大家介紹了關(guān)于Java中Optional使用的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-01-01
  • JAVA使用TreeMap對字符串進(jìn)行排序

    JAVA使用TreeMap對字符串進(jìn)行排序

    這篇文章主要介紹了JAVA使用TreeMap對字符串進(jìn)行排序,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-02-02
  • 基于Spring MVC 簡介及入門小例子(推薦)

    基于Spring MVC 簡介及入門小例子(推薦)

    下面小編就為大家?guī)硪黄赟pring MVC 簡介及入門小例子(推薦)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-06-06
  • vue+springboot項(xiàng)目上傳部署tomcat的方法實(shí)現(xiàn)

    vue+springboot項(xiàng)目上傳部署tomcat的方法實(shí)現(xiàn)

    本文主要介紹了vue+springboot項(xiàng)目上傳部署tomcat的方法實(shí)現(xiàn),包括環(huán)境準(zhǔn)備、配置調(diào)整以及部署步驟,文中通過圖文及示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2024-01-01
  • SpringBoot整合Hbase的實(shí)現(xiàn)示例

    SpringBoot整合Hbase的實(shí)現(xiàn)示例

    這篇文章主要介紹了SpringBoot整合Hbase的實(shí)現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-12-12

最新評論

宜州市| 甘孜| 黄大仙区| 四川省| 淳安县| 常德市| 汉源县| 康乐县| 堆龙德庆县| 延津县| 漠河县| 东安县| 那坡县| 印江| 库伦旗| 雅江县| 库尔勒市| 普格县| 农安县| 蕲春县| 北辰区| 溧阳市| 新晃| 车险| 孟村| 吉木萨尔县| 江北区| 丹寨县| 靖江市| 布尔津县| 岢岚县| 那坡县| 沽源县| 宕昌县| 彭水| 连州市| 巴青县| 天峨县| 从江县| 丹寨县| 璧山县|