[Leetcode] Breadth-first Search
来源:互联网 发布:linux 文件引用计数 编辑:程序博客网 时间:2024/05/21 08:00
[Leetcode] Breadth-first Search
Description
Analysis
It is easy to understand the question. To be simple, we just need to reach the position in the graph in a specific order and calculate the minimal necessary steps. So, the difficult point is just to calculate the minimal steps between two points by using BFS. That is, we need to BFS many times, but don’t worry, it is OK.
Code
I have to mention that, whenever push a point into the queue, we have to set the visited of that point to true, which can save time.
class Solution {public: int BFS(int n, int m, pair<int, int> &start, pair<int, int> &end, vector<vector<int>> &forest) { vector<vector<bool>> visited(n, vector<bool>(m, false)); queue<pair<int, int>> node_queue; visited[start.first][start.second] = true; int step = 0; node_queue.push(start); int size = 0; while (!node_queue.empty()) { size = node_queue.size(); while (size--) { start = node_queue.front(); node_queue.pop(); if (start == end) { return step; } if (start.first > 0 && !visited[start.first - 1][start.second] && forest[start.first - 1][start.second] != 0) { node_queue.push(pair<int, int>(start.first - 1, start.second)); visited[start.first - 1][start.second] = true; } if (start.first < n - 1 && !visited[start.first + 1][start.second] && forest[start.first + 1][start.second] != 0) { node_queue.push(pair<int, int>(start.first + 1, start.second)); visited[start.first + 1][start.second] = true; } if (start.second > 0 && !visited[start.first][start.second - 1] && forest[start.first][start.second - 1] != 0) { node_queue.push(pair<int, int>(start.first, start.second - 1)); visited[start.first][start.second - 1] = true; } if (start.second < m - 1 && !visited[start.first][start.second + 1] && forest[start.first][start.second + 1] != 0) { node_queue.push(pair<int, int>(start.first, start.second + 1)); visited[start.first][start.second + 1] = true; } } step++; } return -1; } int cutOffTree(vector<vector<int>>& forest) { int n = forest.size(); int m = forest[0].size(); vector<int> orders; map<int, pair<int, int>> records; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { if (forest[i][j] != 0 && forest[i][j] != 1) { orders.push_back(forest[i][j]); records[forest[i][j]] = pair<int, int>(i, j); } } } sort(orders.begin(), orders.end()); pair<int, int> start(0, 0); int steps = 0; int tmp = 0; for (int i = 0; i < orders.size(); i++) { // cout << orders[i] << endl; tmp = BFS(n, m, start, records[orders[i]], forest); // cout << tmp << endl; if (tmp == -1) { return -1; } else { steps += tmp; start = records[orders[i]]; } } return steps; }};
Time Complexity: O(m^2 n^2)
阅读全文
0 0
- [Leetcode] Breadth-first Search
- LeetCode之Breadth-first Search题目汇总
- Breadth-first Search -- Leetcode problem690. Employee Importance
- BFS(Breadth First Search)
- Breadth-first Search -- Leetcode problem513. Find Bottom Left Tree Value
- Breadth-first Search -- Leetcode problem102. Binary Tree Level Order Traversal
- Breadth-First-Search(伪代码)
- Tree-BFS(Breadth-First-Search)
- 广度优先搜索(Breadth first search)
- HackerRank Breadth First Search: Shortest Reach
- 暑期训练Breadth-First Search by Foxpower
- Breadth First Search (BFS)入门
- Breadth First Search (BFS) | Iterative & Recursive Implementation
- LeetCode上Tag为广度优先搜索BFS(Breadth-first Search)的题目整理
- Breadth-first Search -- Leetcode problem515. Find Largest Value in Each Tree Row
- Breadth-first Search -- Leetcode problem107. Binary Tree Level Order Traversal II
- SQL:SEARCH DEPTH FIRST和 SEARCH BREADTH FIRST
- 广度优先搜索算法(Breadth-First-Search,BFS)
- 登录注册+webview
- ios替换字符串包含" "
- git使用
- 11.18
- 29岁电工自学编程行骗 上万人被骗1.6亿
- [Leetcode] Breadth-first Search
- JAVA 8 In Action 读书笔记 (二) : Stream
- 入门到放弃node系列之网络模块(一)
- 2003
- 注解
- 单例封装retrofit+拦截器
- Maven实战
- webview运用和传值
- 多线程下载