日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

LeetCode——BFS

發(fā)布時(shí)間:2024/2/28 编程问答 59 豆豆
生活随笔 收集整理的這篇文章主要介紹了 LeetCode——BFS 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

BFS


目錄

  • BFS 介紹
  • 計(jì)算在網(wǎng)格中從原點(diǎn)到特定點(diǎn)的最短路徑長度
  • 組成整數(shù)的最小平方數(shù)數(shù)量
  • 最短單詞路徑

  • 1. BFS 介紹


    廣度優(yōu)先搜索一層一層地進(jìn)行遍歷,每層遍歷都以上一層遍歷的結(jié)果作為起點(diǎn),遍歷一個(gè)距離能訪問到的所有節(jié)點(diǎn)。

    需要注意的是,遍歷過的節(jié)點(diǎn)不能再次被遍歷。

    第一層:
    0 -> {6,2,1,5}
    第二層:
    6 -> {4}
    2 -> {}
    1 -> {}
    5 -> {3}
    第三層:
    4 -> {}
    3 -> {}

    每一層遍歷的節(jié)點(diǎn)都與根節(jié)點(diǎn)距離相同。設(shè) di 表示第 i 個(gè)節(jié)點(diǎn)與根節(jié)點(diǎn)的距離,推導(dǎo)出一個(gè)結(jié)論:對(duì)于先遍歷的節(jié)點(diǎn) i 與后遍歷的節(jié)點(diǎn) j,有 di <= dj。利用這個(gè)結(jié)論,可以求解最短路徑等最優(yōu)解 問題:第一次遍歷到目的節(jié)點(diǎn),其所經(jīng)過的路徑為最短路徑。應(yīng)該注意的是,使用 BFS 只能求解無權(quán)圖的最短路徑,無權(quán)圖是指從一個(gè)節(jié)點(diǎn)到另一個(gè)節(jié)點(diǎn)的代價(jià)都記為 1。
    在程序?qū)崿F(xiàn) BFS 時(shí)需要考慮以下問題:

    • 隊(duì)列:用來存儲(chǔ)每一輪遍歷得到的節(jié)點(diǎn);
    • 標(biāo)記:對(duì)于遍歷過的節(jié)點(diǎn),應(yīng)該將它標(biāo)記,防止重復(fù)遍歷;

    2. 計(jì)算在網(wǎng)格中從原點(diǎn)到特定點(diǎn)的最短路徑長度


    題目描述:1 表示可以經(jīng)過某個(gè)位置,求解從(0,0)位置到(tr,tc)位置的最短路徑長度。

    public int minPathLength(int[][] grids, int tr, int tc) {final int[][] direction = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};final int m = grids.length, n = grids[0].length;Queue<Pair<Integer, Integer>> queue = new LinkedList<>();queue.offer(new Pair<>(0, 0));int pathLength = 0;while (!queue.isEmpty()) {int size = queue.size();pathLength++;while (size-- > 0) {Pair<Integer, Integer> cur = queue.poll();int cr = cur.getKey(), cc = cur.getValue();grids[cr][cc] = 0; //標(biāo)記for (int[] d : direction) {int nr = cr + d[0], nc = cc + d[1];if (nr < 0 || nr >= m || nc < 0 || nc >= n || grids[nr][nc] == 0) {continue;}if (nr == tr && nc == tc) {return pathLength;}queue.offer(new Pair<>(nr, nc));}}}return -1;}

    注:Pair:配對(duì)提供了一種方便方式來處理簡單的鍵值關(guān)聯(lián),Pair類在javafx.util 包中,類構(gòu)造函數(shù)有兩個(gè)參數(shù),鍵及對(duì)應(yīng)值:

    Pair<Integer, String> pair = new Pair<>(1, "One");Integer key = pair.getKey();String value = pair.getValue();

    3. 組成整數(shù)的最小平方數(shù)數(shù)量


    可以將每個(gè)整數(shù)看成圖中的一個(gè)節(jié)點(diǎn),如果兩個(gè)整數(shù)之差為一個(gè)平方數(shù),那么這兩個(gè)整數(shù)所在的節(jié)點(diǎn)就有一條邊。

    要求解最小的平方數(shù)數(shù)量,就是求解從節(jié)點(diǎn) n 到節(jié)點(diǎn) 0 的最短路徑。

    本題也可以用動(dòng)態(tài)規(guī)劃求解,在之后動(dòng)態(tài)規(guī)劃部分中會(huì)再次出現(xiàn)。

    public int numSquares(int n) {List<Integer> squares = generateSquares(n);Queue<Integer> queue = new LinkedList<>();boolean[] marked = new boolean[n + 1];queue.offer(n);marked[n] = true;int level = 0;while (!queue.isEmpty()) {int size = queue.size();level++;while (size-- > 0) {int cur = queue.poll();for (Integer s : squares) {int next = cur - s;if (next < 0) {break;}if (next == 0) {return level;}if (marked[next]) {continue;}marked[next] = true;queue.offer(next);}}}return n;}/*** 生成小于 n 的平方數(shù)序列** @param n* @return*/private List<Integer> generateSquares(int n) {List<Integer> squares = new ArrayList<>();int square = 1;int diff = 3;while (square <= n) {squares.add(square);square += diff;diff += 2;}return squares;}

    3. 最短單詞路徑


    題目描述:找到一條從 beginWord 到 endWord 的最短路徑,每次移動(dòng)規(guī)定為改變一個(gè)字符,并且改變之后的字符串必須在 wordList 中。

    public int ladderLength(String beginWord, String endWord, java.util.List<String> wordList) {wordList.add(beginWord);int N = wordList.size();int start = N - 1;int end = 0;while (end < N && !wordList.get(end).equals(endWord)) {end++;}if (end == N) {return 0;}List<Integer>[] graphic = buildGraphic(wordList);return getShortestPath(graphic, start, end);}private List<Integer>[] buildGraphic(List<String> wordList) {int N = wordList.size();List<Integer>[] graphic = new List[N];for (int i = 0; i < N; i++) {graphic[i] = new ArrayList<>();for (int j = 0; j < N; j++) {if (isConnect(wordList.get(i), wordList.get(j))) {graphic[i].add(j);}}}return graphic;}private boolean isConnect(String s1, String s2) {int diffCnt = 0;for (int i = 0; i < s1.length() && diffCnt <= 1; i++) {if (s1.charAt(i) != s2.charAt(i)) {diffCnt++;}}return diffCnt == 1;}private int getShortestPath(List<Integer>[] graphic, int start, int end) {Queue<Integer> queue = new LinkedList<>();boolean[] marked = new boolean[graphic.length];queue.add(start);marked[start] = true;int path = 1;while (!queue.isEmpty()) {int size = queue.size();path++;while (size-- > 0) {int cur = queue.poll();for (int next : graphic[cur]) {if (next == end) {return path;}if (marked[next]) {continue;}marked[next] = true;queue.add(next);}}}return 0;}

    總結(jié)

    以上是生活随笔為你收集整理的LeetCode——BFS的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。