java實(shí)現(xiàn)馬踏棋盤的算法
本文實(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開始編號 ? ? ? ? int column = 1;//初始位置的列,從1開始編號 ? ? ? ? //創(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)文章
SpringCloud-Alibaba-Nacos啟動(dòng)失敗解決方案
這篇文章主要介紹了SpringCloud-Alibaba-Nacos啟動(dòng)失敗解決方案,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-04-04遠(yuǎn)程調(diào)用@FeignClient注解屬性使用詳解
這篇文章主要為大家介紹了遠(yuǎn)程調(diào)用@FeignClient注解屬性使用示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-10-10javafx tableview鼠標(biāo)觸發(fā)更新屬性詳解
這篇文章主要為大家詳細(xì)介紹了javafx tableview鼠標(biāo)觸發(fā)更新屬性的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-08-08Spring boot基于JPA訪問MySQL數(shù)據(jù)庫的實(shí)現(xiàn)
本文主要介紹了Spring boot基于JPA訪問MySQL數(shù)據(jù)庫的實(shí)現(xiàn),Spring boot結(jié)合Jpa 能夠簡化創(chuàng)建 JPA 數(shù)據(jù)訪問層和跨存儲(chǔ)的持久層功能,用戶的持久層Dao接口只需要繼承定義好的接口,感興趣的可以了解一下2021-06-06

Java ArrayList 數(shù)組之間相互轉(zhuǎn)換

Java INPUTSTREAM如何實(shí)現(xiàn)重復(fù)使用