/** * Return the length of the shortest path between root and target node. */ intBFS(Node root, Node target){ queue<Node> queue; // store all nodes which are waiting to be processed step = 0; // number of steps neeeded from root to current node
// initialize add root to queue;
// BFS while (queue is not empty) { step = step + 1;
// iterate the nodes which are already in the queue int size = q.size(); for (int i = 0; i < size; ++i) { Node cur = the first node in queue;
return step if cur is target; for (Node next : the neighbors of cur) { add next to queue; }
remove the first node from queue; } }
return-1; // there is no path from root to target }
/** * Return the length of the shortest path between root and target node. */ intBFS(Node root, Node target){ queue<Node> queue; // store all nodes which are waiting to be processed Set<Node> visited; // store all the nodes that we've visited step = 0; // number of steps neeeded from root to current node
// initialize add root to queue; add root to visited;
// BFS while (queue is not empty) { step = step + 1;
// iterate the nodes which are already in the queue int size = queue.size(); for (int i = 0; i < size; ++i) { Node cur = the first node in queue; return step if cur is target;
for (Node next : the neighbors of cur) { if (next is not in visited) { add next to queue; add next to visited; } }
remove the first node from queue; } }
return-1; // there is no path from root to target }
Breadth-First Search 的第一個範例 - Leetcode #286 - Walls and Gates
classSolution { public: intnumIslands(vector<vector<char>>& grid){ int m = grid.size(), n = m ? grid[0].size() : 0, islands = 0;
// 走過矩陣的所有 element,只要遇到島,就把島都消滅(即 '1' -> '0') for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { if (grid[i][j] == '1') { islands++; dfs(grid, m, n, i, j); } } }
return islands; }
private: voiddfs(vector<vector<char>>& grid, constint& m, constint& n, constint& i, constint& j){ if(grid[i][j] == '0') return;
grid[i][j] = '0';
if(i - 1 >= 0) { dfs(grid, m, n, i-1, j); } if(i + 1 < m) { dfs(grid, m, n, i+1, j); } if(j - 1 >= 0) { dfs(grid, m, n, i, j-1); } if(j + 1 < n) { dfs(grid, m, n, i, j+1); } } };
classSolution { public: intnumIslands(vector<vector<char>>& grid){ int m = grid.size(), n = m ? grid[0].size() : 0, islands = 0, offsets[] = {0, 1, 0, -1, 0};
for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { if (grid[i][j] == '1') { islands++; grid[i][j] = '0';
queue<pair<int, int>> todo; todo.push({i, j});
while (!todo.empty()) { pair<int, int> p = todo.front(); todo.pop();
for (int k = 0; k < 4; k++) { int r = p.first + offsets[k], c = p.second + offsets[k + 1]; if (r >= 0 && r < m && c >= 0 && c < n && grid[r][c] == '1') { grid[r][c] = '0'; todo.push({r, c}); } } } } } }
return islands; } };
Breadth-First Search 的第三個範例 - Leetcode #752 - Open the Lock
classSolution { public: intminFlips(vector<vector<int>>& mat){ // Check corner cases int m = mat.size(); int n = m ? mat[0].size() : 0; if(m < 1or n < 1) { return-1; }
// Prepare for BFS set<vector<vector<int>>> visited; queue<vector<vector<int>>> q; q.push(mat); visited.insert(mat);
留言討論