[Leetcode] Breadth-first Search

Posted stary_yan

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[Leetcode] Breadth-first Search相关的知识,希望对你有一定的参考价值。

[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)

以上是关于[Leetcode] Breadth-first Search的主要内容,如果未能解决你的问题,请参考以下文章

Breadth-first search

Breadth-first Search690. Employee Importance(easy)

breadth-first depth-first best-first

广度优先搜索(Breadth-First Search)

宽搜BFS(Breadth-first search)和二叉树

如何递归列出某个位置的所有目录,广度优先?