成人国产在线小视频_日韩寡妇人妻调教在线播放_色成人www永久在线观看_2018国产精品久久_亚洲欧美高清在线30p_亚洲少妇综合一区_黄色在线播放国产_亚洲另类技巧小说校园_国产主播xx日韩_a级毛片在线免费

資訊專(zhuān)欄INFORMATION COLUMN

【程序員必會(huì)十大算法】之騎士周游問(wèn)題

Baoyuan / 1095人閱讀

摘要:騎士周游問(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)題

1.未優(yōu)化前(沒(méi)有策略)

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)了

2.貪心算法優(yōu)化后

主要是添加了這個(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

相關(guān)文章

  • 序員必會(huì)十大算法分治算法(漢諾塔問(wèn)題

    摘要:應(yīng)用分治法是一種很重要的算法。字面上的解釋是分而治之,就是把一個(gè)復(fù)雜的問(wèn)題分成兩個(gè)或更多的相同或相似的子問(wèn)題,再把子問(wèn)題分成更小的子問(wèn)題直到最后子問(wèn)題可以簡(jiǎn)單的直接求解,原問(wèn)題的解即子問(wèn)題的解的合并。 ...

    codecraft 評(píng)論0 收藏0
  • 序員必會(huì)十大算法Prim算法

    摘要:?jiǎn)栴}勝利鄉(xiāng)有個(gè)村莊現(xiàn)在需要修路把個(gè)村莊連通各個(gè)村莊的距離用邊線(xiàn)表示權(quán)比如距離公里問(wèn)如何修路保證各個(gè)村莊都能連通并且總的修建公路總里程最短代碼重點(diǎn)理解中的三層循環(huán) 問(wèn)...

    番茄西紅柿 評(píng)論0 收藏2637
  • 序員必會(huì)十大算法二分查找算法

    摘要:遞歸實(shí)現(xiàn)不考慮相同數(shù)二分查找,不考慮有相同數(shù)的情況遞歸找到了考慮有相同數(shù)二分查找考慮有相同元素的情況遞歸要查找的值 1.遞歸實(shí)現(xiàn) ①不考慮相同數(shù) /** * 二分查...

    YFan 評(píng)論0 收藏0
  • 序員必會(huì)十大算法弗洛伊德算法

    摘要:學(xué)習(xí)資料迪杰斯特拉計(jì)算的是單源最短路徑,而弗洛伊德計(jì)算的是多源最短路徑代碼不能設(shè)置為,否則兩個(gè)相加會(huì)溢出導(dǎo)致出現(xiàn)負(fù)權(quán)創(chuàng)建頂點(diǎn)和邊 學(xué)習(xí)資料 迪杰斯特拉計(jì)算的是單源最...

    JellyBool 評(píng)論0 收藏0
  • 序員必會(huì)十大算法貪心算法

    摘要:例題假設(shè)存在如下表的需要付費(fèi)的廣播臺(tái),以及廣播臺(tái)信號(hào)可以覆蓋的地區(qū)。 例題 假設(shè)存在如下表的需要付費(fèi)的廣播臺(tái),以及廣播臺(tái)信號(hào)可以覆蓋的地區(qū)。如何選擇最少的廣播臺(tái),讓...

    macg0406 評(píng)論0 收藏0

發(fā)表評(píng)論

0條評(píng)論

最新活動(dòng)
閱讀需要支付1元查看
<