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

原创粉丝点击