網頁

2026年7月2日 星期四

LeetCode 3286: 別只用 Dijkstra!0-1 BFS (deque) 才是 0ms 關鍵


0ms |C++ Py3| BFS使用deque解Leetcode 3286  Find a Safe Walk Through a Grid
了解以下幾點有助於解決最短路徑問題:
所有權重相同時,只需使用基於佇列queue的廣度優先搜尋(BFS over queue)
權重有兩種非負值(如本題所示),使用基於雙端佇列(deque)的廣度優先搜尋(BFS over deque)
權重為正值時,使用基於優先權佇列(priority queue)的廣度優先搜尋(類似Dijkstra演算法)

2026年7月1日 星期三

Beats 100%|C++ UnionFind BFS解Leetcode 2812 Find the Safest Path in a Grid


Beats 100%|C++ unionFind BFS linkedList解Leetcode 2812  Find the Safest Path in a Grid
本影片詳細講解如何結合 UnionFind與 BFS(廣度優先搜尋)來解決 LeetCode 2812. Find the Safest Path in a Grid。透過 BFS 預處理所有格子到最近小偷的距離,再利用 UnionFind 找出具有最大安全性係數的路徑,最終在 C++ 實作中達到 182ms 並擊敗 100% 的紀錄!