欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

java數(shù)據(jù)結(jié)構(gòu)和算法之馬踏棋盤算法

 更新時間:2022年02月14日 15:42:59   作者:小志的博客  
這篇文章主要為大家詳細介紹了java數(shù)據(jù)結(jié)構(gòu)和算法之馬踏棋盤算法,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

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

一、馬踏棋盤算法介紹

馬踏棋盤算法也被稱為騎士周游問題
將馬隨機放在國際象棋的8×8棋盤Board[0~7][0~7]的某個方格中,馬按走棋規(guī)則(馬走日字)進行移動。要求每個方格只進入一次,走遍棋盤上全部64個方格

二、騎士周游問題的思路分析

1、創(chuàng)建棋盤 chessBoard , 是一個二維數(shù)組
2、將當前位置設(shè)置為已經(jīng)訪問,然后根據(jù)當前位置,計算馬兒還能走哪些位置,并放入到一個集合中(ArrayList), 最多有8個位置, 每走一步,就使用step+1
3、遍歷ArrayList中存放的所有位置,看看哪個可以走通 , 如果走通,就繼續(xù),走不通,就回溯.
4、判斷馬兒是否完成了任務,使用 step 和應該走的步數(shù)比較 , 如果沒有達到數(shù)量,則表示沒有完成任務,將整個棋盤置0
5、注意:馬兒不同的走法(策略),會得到不同的結(jié)果,效率也會有影響(優(yōu)化)

三、騎士周游問題代碼示例

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;//棋盤的列數(shù)
? ? ?static int Y;//棋盤的行數(shù)
? ? ?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開始
? ? ? ? //創(chuàng)建棋盤
? ? ? ? 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: 根據(jù)當前位置(Point),計算馬還能走哪些位置(Point)
? ? ?* ? ? ? ? ? ? ? ?并放入到一個集合中(ArrayList),最多有8個位置
? ? * @Param: ?curPoint
? ? * @Author: xz ?
? ? */
? ? public static ArrayList<Point> next(Point curPoint){
? ? ? ? //創(chuàng)建一個ArrayList
? ? ? ? ArrayList<Point> list =new ArrayList<>();
? ? ? ? //創(chuàng)建一個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) < X && (point.y = curPoint.y - 2) >= 0) {//示例圖中指定馬可以走7的位置
? ? ? ? ? ? list.add(new Point(point));
? ? ? ? }
? ? ? ? if ((point.x = curPoint.x + 2) < X && (point.y = curPoint.y - 1) >= 0) {//示例圖中指定馬可以走0的位置
? ? ? ? ? ? list.add(new Point(point));
? ? ? ? }
? ? ? ? if ((point.x = curPoint.x + 2) < X && (point.y = curPoint.y + 1) < Y) {//示例圖中指定馬可以走1的位置
? ? ? ? ? ? list.add(new Point(point));
? ? ? ? }
? ? ? ? if ((point.x = curPoint.x + 1) < X && (point.y = curPoint.y + 2) < Y) {//示例圖中指定馬可以走2的位置
? ? ? ? ? ? list.add(new Point(point));
? ? ? ? }
? ? ? ? if ((point.x = curPoint.x - 1) >= 0 && (point.y = curPoint.y + 2) < Y) {//示例圖中指定馬可以走3的位置
? ? ? ? ? ? list.add(new Point(point));
? ? ? ? }
? ? ? ? if ((point.x = curPoint.x - 2) >= 0 && (point.y = curPoint.y + 1) < Y) {//示例圖中指定馬可以走4的位置
? ? ? ? ? ? list.add(new Point(point));
? ? ? ? }
? ? ? ? return list;
? ? }

? ? /**?
? ? * @Description: ?騎士周游算法的方法
? ? * @Param: ?chessboard ?表示棋盤
? ? * ? ? ? ? ? row ? ? ? ? 表示馬兒當前的位置的行 從0開始
? ? * ? ? ? ? ? column ? ? ?表示馬兒當前的位置的列 ?從0開始
? ? * ? ? ? ? ? step ? ? ? ?表示是第幾步 ,初始位置就是第1步
? ? * @Author: xz ?
? ? */
? ? public static void horseChessBoardAlgorithm(int[][] chessboard, int row, int column, int step){
? ? ? ? chessboard[row][column] = step;
? ? ? ? visited[row * X + column] = true; //標記該位置已經(jīng)訪問
? ? ? ? //獲取當前位置可以走的下一個位置的集合
? ? ? ? ArrayList<Point> pointList= next(new Point(column, row));
? ? ? ? //對pointList進行排序,排序的規(guī)則就是對pointList的所有的Point對象的下一步的位置的數(shù)目,進行非遞減排序
? ? ? ? sort(pointList);
? ? ? ? //遍歷 list
? ? ? ? while(!pointList.isEmpty()) {
? ? ? ? ? ? Point p = pointList.remove(0);//取出下一個可以走的位置
? ? ? ? ? ? //判斷該點是否已經(jīng)訪問過
? ? ? ? ? ? if(!visited[p.y * X + p.x]) {//說明還沒有訪問過
? ? ? ? ? ? ? ? horseChessBoardAlgorithm(chessboard, p.y, p.x, step + 1);
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? //判斷馬兒是否完成了任務,使用step 和應該走的步數(shù)比較 ,
? ? ? ? //如果沒有達到數(shù)量,則表示沒有完成任務,將整個棋盤置0
? ? ? ? //說明: step < X * Y ?成立的情況有兩種
? ? ? ? //1. 棋盤到目前位置,仍然沒有走完
? ? ? ? //2. 棋盤處于一個回溯過程
? ? ? ? if(step < X * Y && !finished ) {
? ? ? ? ? ? chessboard[row][column] = 0;
? ? ? ? ? ? visited[row * X + column] = false;
? ? ? ? } else {
? ? ? ? ? ? finished = true;
? ? ? ? }
? ? }

? ? /**
? ? ?* @Description: 根據(jù)當前這個一步的所有的下一步的選擇位置,進行非遞減排序, 減少回溯的次數(shù)
? ? ?* @Param: ?ArrayList<Point>
? ? ?* @Author: xz
? ? ?*/
? ? public static void sort(ArrayList<Point> pointList) {
? ? ? ? pointList.sort(new Comparator<Point>() {
? ? ? ? ? ? @Override
? ? ? ? ? ? public int compare(Point o1, Point o2) {
? ? ? ? ? ? ? ? // TODO Auto-generated method stub
? ? ? ? ? ? ? ? //獲取到o1的下一步的所有位置個數(shù)
? ? ? ? ? ? ? ? int count1 = next(o1).size();
? ? ? ? ? ? ? ? //獲取到o2的下一步的所有位置個數(shù)
? ? ? ? ? ? ? ? int count2 = next(o2).size();
? ? ? ? ? ? ? ? if(count1 < count2) {
? ? ? ? ? ? ? ? ? ? return -1;
? ? ? ? ? ? ? ? } else if (count1 == count2) {
? ? ? ? ? ? ? ? ? ? return 0;
? ? ? ? ? ? ? ? } else {
? ? ? ? ? ? ? ? ? ? return 1;
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }

? ? ? ? });
? ? }


}

2、運行main函數(shù),輸出馬在棋盤中走的步驟和位置如下:

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

相關(guān)文章

  • java多線程實現(xiàn)文件下載功能

    java多線程實現(xiàn)文件下載功能

    這篇文章主要介紹了java多線程實現(xiàn)文件下載功能的相關(guān)資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-01-01
  • SpringBoot注冊FilterRegistrationBean相關(guān)情況講解

    SpringBoot注冊FilterRegistrationBean相關(guān)情況講解

    這篇文章主要介紹了SpringBoot注冊FilterRegistrationBean相關(guān)情況,借助FilterRegistrationBean來注冊filter,可以避免在web.xml種配置filter這種原始的寫法
    2023-02-02
  • SpringBoot3集成Thymeleaf的過程詳解

    SpringBoot3集成Thymeleaf的過程詳解

    在現(xiàn)代的Web開發(fā)中,構(gòu)建靈活、動態(tài)的用戶界面是至關(guān)重要的,Spring Boot和Thymeleaf的結(jié)合為開發(fā)者提供了一種簡單而強大的方式來創(chuàng)建動態(tài)的Web應用,本文將介紹如何在Spring Boot項目中集成Thymeleaf,并展示一些基本的使用方法,需要的朋友可以參考下
    2024-01-01
  • Java算法之重新排列數(shù)組例題

    Java算法之重新排列數(shù)組例題

    這篇文章主要介紹了Java算法之重新排列數(shù)組例題,文章圍繞主題展開詳細的內(nèi)容介紹,具有一定的參考價值,感興趣的小伙伴可以參考一下
    2022-08-08
  • Java設(shè)計模式七大原則之開閉原則詳解

    Java設(shè)計模式七大原則之開閉原則詳解

    開閉原則,又稱為OCP原則,即一個軟件實體如類,模塊和函數(shù)應該對擴展開放,對修改關(guān)閉。本文將詳細介紹Java設(shè)計模式七大原則之一的開閉原則,需要的可以參考一下
    2022-02-02
  • Java 線程池ThreadPoolExecutor源碼解析

    Java 線程池ThreadPoolExecutor源碼解析

    這篇文章主要介紹了Java 線程池ThreadPoolExecutor源碼解析
    2022-03-03
  • Java面向?qū)ο笾鄳B(tài)

    Java面向?qū)ο笾鄳B(tài)

    這篇文章主要介紹了Java面向?qū)ο笾鄳B(tài),文章以什么是多態(tài)、多態(tài)的實現(xiàn)條件、多態(tài)的訪問特點、多態(tài)的優(yōu)點和缺點的相關(guān)資料展開文章內(nèi)容,需要的小伙伴可以參考一下
    2021-10-10
  • Spring中的spring.factories文件用法(Spring如何加載第三方Bean)

    Spring中的spring.factories文件用法(Spring如何加載第三方Bean)

    這篇文章主要介紹了Spring中的spring.factories文件用法(Spring如何加載第三方Bean),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-10-10
  • springMVC如何防止表單重復提交詳解

    springMVC如何防止表單重復提交詳解

    平時開發(fā)的項目中經(jīng)常會遇到表單重復提交,造成數(shù)據(jù)重復,增加服務器負載,嚴重甚至會造成服務器宕機,因此有效防止表單重復提交有一定的必要性,這篇文章主要給大家介紹了關(guān)于springMVC如何防止表單重復提交的相關(guān)資料,需要的朋友可以參考下
    2021-11-11
  • IDEA如何在當前類中查找方法快捷鍵

    IDEA如何在當前類中查找方法快捷鍵

    這篇文章主要介紹了IDEA如何在當前類中查找方法快捷鍵問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-11-11

最新評論