java數(shù)據(jù)結(jié)構(gòu)和算法之馬踏棋盤算法
本文實(shí)例為大家分享了java實(shí)現(xiàn)算法之馬踏棋盤的具體代碼,供大家參考,具體內(nèi)容如下
一、馬踏棋盤算法介紹
馬踏棋盤算法也被稱為騎士周游問題
將馬隨機(jī)放在國際象棋的8×8棋盤Board[0~7][0~7]的某個(gè)方格中,馬按走棋規(guī)則(馬走日字)進(jìn)行移動(dòng)。要求每個(gè)方格只進(jìn)入一次,走遍棋盤上全部64個(gè)方格
二、騎士周游問題的思路分析

1、創(chuàng)建棋盤 chessBoard , 是一個(gè)二維數(shù)組
2、將當(dāng)前位置設(shè)置為已經(jīng)訪問,然后根據(jù)當(dāng)前位置,計(jì)算馬兒還能走哪些位置,并放入到一個(gè)集合中(ArrayList), 最多有8個(gè)位置, 每走一步,就使用step+1
3、遍歷ArrayList中存放的所有位置,看看哪個(gè)可以走通 , 如果走通,就繼續(xù),走不通,就回溯.
4、判斷馬兒是否完成了任務(wù),使用 step 和應(yīng)該走的步數(shù)比較 , 如果沒有達(dá)到數(shù)量,則表示沒有完成任務(wù),將整個(gè)棋盤置0
5、注意:馬兒不同的走法(策略),會(huì)得到不同的結(jié)果,效率也會(huì)有影響(優(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[]; //標(biāo)記棋盤的各個(gè)位置是否被訪問過
? ? ?static boolean finished; // 標(biāo)記是否棋盤的所有位置都被訪問 true:成功,false:失敗
? ? public static void main(String[] args) {
? ? ? ? System.out.println("騎士周游算法,開始運(yùn)行~~");
? ? ? ? X=8;
? ? ? ? Y=8;
? ? ? ? int row=1;//馬初始位置的行,從編號(hào)1開始
? ? ? ? int column=1;//馬初始位置的列,從編號(hào)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("總共耗時(shí):"+(endTime-startTime)+"毫秒");
? ? ? ? System.out.println("輸出棋盤的最后情況============");
? ? ? ? //輸出棋盤的最后情況
? ? ? ? for(int[] rows : chessboard){
? ? ? ? ? ? for(int step : rows){
? ? ? ? ? ? ? ? System.out.print(step + "\t");
? ? ? ? ? ? }
? ? ? ? ? ? System.out.println();
? ? ? ? }
? ? }
? ? /**?
? ? * @Description: 根據(jù)當(dāng)前位置(Point),計(jì)算馬還能走哪些位置(Point)
? ? ?* ? ? ? ? ? ? ? ?并放入到一個(gè)集合中(ArrayList),最多有8個(gè)位置
? ? * @Param: ?curPoint
? ? * @Author: xz ?
? ? */
? ? public static ArrayList<Point> next(Point curPoint){
? ? ? ? //創(chuàng)建一個(gè)ArrayList
? ? ? ? ArrayList<Point> list =new ArrayList<>();
? ? ? ? //創(chuàng)建一個(gè)Point
? ? ? ? Point point=new Point();
? ? ? ? //curPoint.x-2 表示當(dāng)前位置(curPoint)的列向左移動(dòng)2列
? ? ? ? //curPoint.x+2 表示當(dāng)前位置(curPoint)的列向右移動(dòng)2列
? ? ? ? //curPoint.y-1 表示當(dāng)前位置(curPoint)的列向上移動(dòng)1行
? ? ? ? //curPoint.y+1 表示當(dāng)前位置(curPoint)的列向下移動(dòng)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 ? ? ? ? 表示馬兒當(dāng)前的位置的行 從0開始
? ? * ? ? ? ? ? column ? ? ?表示馬兒當(dāng)前的位置的列 ?從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; //標(biāo)記該位置已經(jīng)訪問
? ? ? ? //獲取當(dāng)前位置可以走的下一個(gè)位置的集合
? ? ? ? ArrayList<Point> pointList= next(new Point(column, row));
? ? ? ? //對(duì)pointList進(jìn)行排序,排序的規(guī)則就是對(duì)pointList的所有的Point對(duì)象的下一步的位置的數(shù)目,進(jìn)行非遞減排序
? ? ? ? sort(pointList);
? ? ? ? //遍歷 list
? ? ? ? while(!pointList.isEmpty()) {
? ? ? ? ? ? Point p = pointList.remove(0);//取出下一個(gè)可以走的位置
? ? ? ? ? ? //判斷該點(diǎn)是否已經(jīng)訪問過
? ? ? ? ? ? if(!visited[p.y * X + p.x]) {//說明還沒有訪問過
? ? ? ? ? ? ? ? horseChessBoardAlgorithm(chessboard, p.y, p.x, step + 1);
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? //判斷馬兒是否完成了任務(wù),使用step 和應(yīng)該走的步數(shù)比較 ,
? ? ? ? //如果沒有達(dá)到數(shù)量,則表示沒有完成任務(wù),將整個(gè)棋盤置0
? ? ? ? //說明: step < X * Y ?成立的情況有兩種
? ? ? ? //1. 棋盤到目前位置,仍然沒有走完
? ? ? ? //2. 棋盤處于一個(gè)回溯過程
? ? ? ? if(step < X * Y && !finished ) {
? ? ? ? ? ? chessboard[row][column] = 0;
? ? ? ? ? ? visited[row * X + column] = false;
? ? ? ? } else {
? ? ? ? ? ? finished = true;
? ? ? ? }
? ? }
? ? /**
? ? ?* @Description: 根據(jù)當(dāng)前這個(gè)一步的所有的下一步的選擇位置,進(jìn)行非遞減排序, 減少回溯的次數(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的下一步的所有位置個(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;
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? });
? ? }
}2、運(yùn)行main函數(shù),輸出馬在棋盤中走的步驟和位置如下:

以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
JavaWeb項(xiàng)目實(shí)戰(zhàn)之表白墻和在線相冊(cè)
這篇文章主要給大家介紹了關(guān)于JavaWeb項(xiàng)目實(shí)戰(zhàn)之表白墻和在線相冊(cè)的相關(guān)資料,JavaWeb表白墻是一款基于JavaWeb技術(shù)開發(fā)的表白墻應(yīng)用,用戶可以在上面發(fā)布表白信息,也可以查看其他用戶的表白信息,需要的朋友可以參考下2023-03-03
SpringBoot使用@Cacheable時(shí)設(shè)置部分緩存的過期時(shí)間方式
這篇文章主要介紹了SpringBoot使用@Cacheable時(shí)設(shè)置部分緩存的過期時(shí)間方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-12-12
SpringBoot對(duì)不同Bean注解的區(qū)別和使用場景說明
這篇文章主要介紹了SpringBoot對(duì)不同Bean注解的區(qū)別和使用場景說明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-03-03
Spring中ApplicationContext的拓展功能詳解
這篇文章主要介紹了Spring中ApplicationContext的拓展功能詳解,相對(duì)于BeanFactory來說,ApplicationContext除了提供BeanFactory的所有功能外,還有一些其他的功能,主要包括國際化支持、資源訪問、事件傳遞,需要的朋友可以參考下2024-01-01
詳解spring項(xiàng)目中如何動(dòng)態(tài)刷新bean
這篇文章主要為大家介紹了詳解spring項(xiàng)目中如何動(dòng)態(tài)刷新bean,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-08-08
解決IDEA上循環(huán)依賴報(bào)錯(cuò)問題Error:java: Annotation processing&n
這篇文章主要介紹了解決IDEA上循環(huán)依賴報(bào)錯(cuò)問題Error:java: Annotation processing is not supported for module cycles,具有很好的參考價(jià)值,希望對(duì)大家有所幫助2023-10-10
Java實(shí)現(xiàn)藍(lán)橋杯G將軍的示例代碼
這篇文章主要介紹了Java實(shí)現(xiàn)藍(lán)橋杯G將軍的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-02-02

