【LeetCode】 Surrounded Regions (BFS && DFS)
廣搜和深搜都能解決,但是LeetCode上使用深搜時會棧溢位
DFS:
<span style="font-size:18px;">/*LeetCode Surrounded Regions
* 題目:給定一個字元陣列,由'X'和'O'組成,找到所有被x包圍的o並將其替換為x
* 思路:只要替換被包圍的o就行,如果有一個o是邊界或者上下左右中有一個是o且這個o不會被替換,則該點也不會被替換
* 從四條邊開始,因為在這4周的一定不是被包圍的所以用他們開始找到廣搜的佇列,如果佇列為空,那麼就是所有的o都被包圍
*/
package javaTrain;
public class Train25 {
public static void solve(char[][] board) {
long n = board.length;
if(n==0) return;
long m = board[0].length;
for(long i = 0;i < m;i++){ //對第一行和最後一行的字元進行廣搜
bfs(board,0,i);
bfs(board,n-1,i);
}
for(long j = 1;j < n-1;j++){ //對第一列和最後一列的字元進行廣搜,去除4條邊重複的字元
bfs(board,j,0);
bfs(board,j,m-1);
}
for(int i = 0;i < n;i++){
for(int j = 0;j < m;j++){
if(board[i][j] == 'O') board[i][j] = 'X'; //被包圍的o需取代
else if(board[i][j] == '$') board[i][j] = 'O'; //標記的不被包圍的o保持原樣
}
}
}
private static void bfs(char[][] board,int i,int j){
long n = board.length;
long m = board[0].length;
if(i < 0 || i>=n||j<0||j>=m||board[i][j] != 'O') return; //邊界的點都不被包圍
board[i][j] = '$';
bfs(board,i-1,j);
bfs(board,i,j-1);
bfs(board,i+1,j);
bfs(board,i,j+1);
}
public static void main(String args[]){
char board[][] = {{'O','X','O'},{'X','O','X'},{'O','X','O'}};
solve(board);
for(int i = 0;i < board.length;i++){
for(int j = 0;j < board[0].length;j++){
System.out.print(board[i][j]);
}
System.out.println();
}
}
}
</span>
BFS:
<span style="font-size:18px;">// LeetCode, Surrounded Regions
// BFS,時間複雜度O(n),空間複雜度O(n)
class Solution {
public:
void solve(vector<vector<char>> &board) {
if (board.empty()) return;
const int m = board.size();
const int n = board[0].size();
for (int i = 0; i < n; i++) {
bfs(board, 0, i);
bfs(board, m - 1, i);
}
for (int j = 1; j < m - 1; j++) {
bfs(board, j, 0);
bfs(board, j, n - 1);
}
for (int i = 0; i < m; i++)
for (int j = 0; j < n; j++)
if (board[i][j] == 'O')
board[i][j] = 'X';
else if (board[i][j] == '+')
board[i][j] = 'O';
}
private:
void bfs(vector<vector<char>> &board, int i, int j) {
typedef pair<int, int> state_t;
queue<state_t> q;
const int m = board.size();
const int n = board[0].size();
auto is_valid = [&](const state_t &s) {
const int x = s.first;
const int y = s.second;
if (x < 0 || x >= m || y < 0 || y >= n || board[x][y] != 'O')
return false;
return true;
};
auto state_extend = [&](const state_t &s) {
vector<state_t> result;
const int x = s.first;
const int y = s.second;
// 上下左右
const state_t new_states[4] = {{x-1,y}, {x+1,y},
{x,y-1}, {x,y+1}};
for (int k = 0; k < 4; ++k) {
if (is_valid(new_states[k])) {
// 既有標記功能又有去重功能
board[new_states[k].first][new_states[k].second] = '+';
result.push_back(new_states[k]);
}
}
return result;
};
state_t start = { i, j };
if (is_valid(start)) {
board[i][j] = '+';
q.push(start);
}
while (!q.empty()) {
auto cur = q.front();
q.pop();
auto new_states = state_extend(cur);
for (auto s : new_states) q.push(s);
}
}
};
</span>
相關文章
- Leetcode Surrounded RegionsLeetCode
- Leetcode: Surrounded regionsLeetCode
- Surrounded Regions leetcode javaLeetCodeJava
- LeetCode130:Surrounded RegionsLeetCode
- leetcode_question_130 Surrounded RegionsLeetCode
- 200. Number of Islands+130. Surrounded Regions(並查集/DFS)並查集
- Shaping Regions(dfs)API
- Clone Graph leetcode java(DFS and BFS 基礎)LeetCodeJava
- leetcode刷題記錄:演算法(六)BFS&DFSLeetCode演算法
- 聊聊演算法——BFS和DFS演算法
- LeetCode C++ 1302. Deepest Leaves Sum【Tree/BFS/DFS】中等LeetCodeC++
- 藍橋杯-迷宮(BFS+DFS)
- JAVA圖搜尋演算法之DFS-BFSJava演算法
- NOIP2010引水入城[BFS DFS 貪心]
- 深度DFS 和 廣度BFS搜尋演算法學習演算法
- DFS與BFS——理解簡單搜尋(中文虛擬碼+例題)
- 迷宮系列(三)利用BFS/DFS的資料得到最短路/通路
- 資料結構學習(C++)——圖【2】(DFS和BFS) (轉)資料結構C++
- [LeetCode] 最短的橋 雙BFS JavaLeetCodeJava
- LeetCode BFS題目以及要注意的點LeetCode
- 基本演算法——深度優先搜尋(DFS)和廣度優先搜尋(BFS)演算法
- 【力扣】島嶼數量(體會一下dfs和bfs思路的實質)力扣
- 程式碼隨想錄演算法訓練營 | 200.島嶼的數量(dfs/bfs)演算法
- 【資料結構與演算法】自己動手實現圖的BFS和DFS(附完整原始碼)資料結構演算法原始碼
- DFS
- dfs題目這樣去接題,秒殺leetcode題目LeetCode
- LeetCode演算法練習——深度優先搜尋 DFSLeetCode演算法
- dfs 驗證搜尋二叉樹——leetcode98二叉樹LeetCode
- BFS廣度優先搜尋(5)(亦可以用DFS)--hdu1241(poj1562)(基礎題)
- Count BFS Graph
- 01BFS
- DFS樹
- dfs序
- leetcode 每日一題 543 二叉樹的直徑 dfs方法LeetCode每日一題二叉樹
- LeetCode C++ 968. Binary Tree Cameras【Tree/DFS】困難LeetCodeC++
- LeetCode#110.Balanced Binary Tree(Tree/Height/DFS/Recursion)LeetCode
- 【資料結構與演算法筆記04】對圖搜尋策略的一些思考(包括DFS和BFS)資料結構演算法筆記
- python實現圖(基於圖的不同儲存方式)的深度優先(DFS)和廣度(BFS)優先遍歷Python