摘要:騎士周游問(wèn)題又叫馬踏棋盤(pán)問(wèn)題未優(yōu)化前沒(méi)有策略定義棋盤(pán)的行數(shù)和列數(shù)定義棋盤(pán)上的某個(gè)點(diǎn)是否被訪(fǎng)問(wèn)過(guò)記錄是否周游結(jié)束從第一行第一列開(kāi)始走,第一次走算第一步,即展示下是棋盤(pán),
騎士周游問(wèn)題又叫馬踏棋盤(pán)問(wèn)題
public class Main { //定義棋盤(pán)的行數(shù)和列數(shù) static int X = 8; static int Y = 8; //定義棋盤(pán)上的某個(gè)點(diǎn)是否被訪(fǎng)問(wèn)過(guò) static boolean[] isVisited; //記錄是否周游結(jié)束 static boolean isFinished = false; public static void main(String[] args) { isVisited = new boolean[X * Y]; int[][] chessBoard = new int[X][Y]; //從第一行第一列開(kāi)始走,第一次走算第一步,即step=1 travelChessboard(chessBoard,0,0,1); //展示下chessBoard for (int[] row: chessBoard){ for (int step: row){ System.out.print(step + " "); } System.out.println(); } } /** * * @param chessBoard 是棋盤(pán),因?yàn)檫f歸,所以在不斷變化 * @param row 是馬所在的行,也就是y值 * @param column 是馬所在的列,也就是x值 * @param step 馬走到的第幾步 */ public static void travelChessboard(int[][] chessBoard,int row,int column,int step){ //假定這一點(diǎn)是可以走的,所以設(shè)置已訪(fǎng)問(wèn),步數(shù)也得加上 chessBoard[row][column] = step;//假定可以走,所以先走過(guò)去看看,設(shè)置走過(guò)去的步數(shù) isVisited[row * X + column] = true;//假定可以走,所以先走過(guò)去看看,設(shè)置成已訪(fǎng)問(wèn) //得到下一步可以走的點(diǎn)的集合 ArrayList<Point> nextPoints = getNext(new Point(column, row)); while (!nextPoints.isEmpty()){ //首先取出一個(gè)來(lái) Point nextPoint = nextPoints.remove(0); //如果這個(gè)點(diǎn)沒(méi)有被訪(fǎng)問(wèn)過(guò) if (!isVisited[nextPoint.y * X + nextPoint.x]){ travelChessboard(chessBoard,nextPoint.y,nextPoint.x,step + 1);//這里我一開(kāi)始寫(xiě)了step++ 其實(shí)應(yīng)該是step+1 } } //如果假定失敗,其實(shí)這個(gè)點(diǎn)是不可以走的,那么我們就進(jìn)行回溯?。。?/span> if (step < X * Y && !isFinished){ chessBoard[row][column] = 0; isVisited[row * X + column] = false; }else { isFinished = true; } } /** * 傳入當(dāng)前點(diǎn),得到能走的下一個(gè)點(diǎn)的集合 * @param curPoint * @return */ public static ArrayList<Point> getNext(Point curPoint){ //創(chuàng)建結(jié)果集 ArrayList<Point> nextPoints = new ArrayList<>(); Point nextPoint = new Point(); //可以走0 if ((nextPoint.x = curPoint.x + 2) < X && (nextPoint.y = curPoint.y - 1) >= 0){ nextPoints.add(new Point(nextPoint)); } //表示馬可以走1 if ((nextPoint.x = curPoint.x + 2) < X && (nextPoint.y = curPoint.y + 1) < Y){ nextPoints.add(new Point(nextPoint)); } //可以走2 if ((nextPoint.x = curPoint.x + 1) < X && (nextPoint.y = curPoint.y + 2) < Y){ nextPoints.add(new Point(nextPoint)); } //可以走3 if ((nextPoint.x = curPoint.x - 1) >= 0 && (nextPoint.y = curPoint.y + 2) < Y){ nextPoints.add(new Point(nextPoint)); } //可以走4 if ((nextPoint.x = curPoint.x - 2) >= 0 && (nextPoint.y = curPoint.y + 1) < Y){ nextPoints.add(new Point(nextPoint)); } //可以走5 if ((nextPoint.x = curPoint.x - 2) >= 0 && (nextPoint.y = curPoint.y - 1) >= 0){ nextPoints.add(new Point(nextPoint)); } //可以走6 if ((nextPoint.x = curPoint.x - 1) >= 0 && (nextPoint.y = curPoint.y - 2) >= 0){ nextPoints.add(new Point(nextPoint)); } //可以走7 if ((nextPoint.x = curPoint.x + 1) < X && (nextPoint.y = curPoint.y - 2) >= 0){ nextPoints.add(new Point(nextPoint)); } return nextPoints; }}
結(jié)果略去,等結(jié)果時(shí)間太長(zhǎng)了
主要是添加了這個(gè)方法
添加了這個(gè)方法后,可以減少回溯的次數(shù),極大的提高效率
public static void getNextNext(ArrayList<Point> arrayList){ //重寫(xiě)集合的sort方法,將其按下一步可選點(diǎn)數(shù)目由小到大的順序排列,再準(zhǔn)確一點(diǎn)就是非遞減排序(因?yàn)橛邢嗤c(diǎn)) arrayList.sort(new Comparator<Point>() { @Override public int compare(Point o1, Point o2) { //得到o1的下一步可選點(diǎn)的數(shù)目 int count1 = getNext(o1).size(); //得到o2的下一步可選點(diǎn)的數(shù)目 int count2 = getNext(o2).size(); if (count1 > count2){ return -1; }else if (count1 == count2){ return 0; }else { return 1; } } }); }
結(jié)果
1 16 43 32 3 18 45 22 42 31 2 17 44 21 4 19 15 56 53 60 33 64 23 46 30 41 58 63 54 61 20 5 57 14 55 52 59 34 47 24 40 29 38 35 62 51 6 9 13 36 27 50 11 8 25 48 28 39 12 37 26 49 10 7
文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請(qǐng)注明本文地址:http://systransis.cn/yun/121665.html
摘要:應(yīng)用分治法是一種很重要的算法。字面上的解釋是分而治之,就是把一個(gè)復(fù)雜的問(wèn)題分成兩個(gè)或更多的相同或相似的子問(wèn)題,再把子問(wèn)題分成更小的子問(wèn)題直到最后子問(wèn)題可以簡(jiǎn)單的直接求解,原問(wèn)題的解即子問(wèn)題的解的合并。 ...
摘要:?jiǎn)栴}勝利鄉(xiāng)有個(gè)村莊現(xiàn)在需要修路把個(gè)村莊連通各個(gè)村莊的距離用邊線(xiàn)表示權(quán)比如距離公里問(wèn)如何修路保證各個(gè)村莊都能連通并且總的修建公路總里程最短代碼重點(diǎn)理解中的三層循環(huán) 問(wèn)...
摘要:遞歸實(shí)現(xiàn)不考慮相同數(shù)二分查找,不考慮有相同數(shù)的情況遞歸找到了考慮有相同數(shù)二分查找考慮有相同元素的情況遞歸要查找的值 1.遞歸實(shí)現(xiàn) ①不考慮相同數(shù) /** * 二分查...
摘要:學(xué)習(xí)資料迪杰斯特拉計(jì)算的是單源最短路徑,而弗洛伊德計(jì)算的是多源最短路徑代碼不能設(shè)置為,否則兩個(gè)相加會(huì)溢出導(dǎo)致出現(xiàn)負(fù)權(quán)創(chuàng)建頂點(diǎn)和邊 學(xué)習(xí)資料 迪杰斯特拉計(jì)算的是單源最...
摘要:例題假設(shè)存在如下表的需要付費(fèi)的廣播臺(tái),以及廣播臺(tái)信號(hào)可以覆蓋的地區(qū)。 例題 假設(shè)存在如下表的需要付費(fèi)的廣播臺(tái),以及廣播臺(tái)信號(hào)可以覆蓋的地區(qū)。如何選擇最少的廣播臺(tái),讓...
閱讀 1833·2023-04-26 01:55
閱讀 1096·2021-09-30 09:47
閱讀 1688·2019-08-30 15:54
閱讀 754·2019-08-30 15:53
閱讀 709·2019-08-30 15:52
閱讀 1150·2019-08-30 15:44
閱讀 2426·2019-08-30 14:06
閱讀 1073·2019-08-29 16:39