[Leetcode] Breadth-first Search

本文介绍了一种使用广度优先搜索(BFS)解决LeetCode上特定问题的方法,该问题要求在一个森林网格中找到一系列树木的高度递增路径。通过详细的代码解析,展示了如何寻找从起点到各指定目标点的最短路径。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值