前往小程序,Get更优阅读体验!
立即前往
发布
社区首页 >专栏 >【算法刷题指南】BFS-解决最短路问题

【算法刷题指南】BFS-解决最短路问题

作者头像
南桥
发布2024-11-23 11:05:17
发布2024-11-23 11:05:17
7200
代码可运行
举报
文章被收录于专栏:南桥谈编程南桥谈编程
运行总次数:0
代码可运行

1926. 迷宫中离入口最近的出口

代码语言:javascript
代码运行次数:0
复制
class Solution {
    int dx[4] = {0, 0, 1, -1}, dy[4] = {1, -1, 0, 0};
    typedef pair<int, int> PII;
    bool vis[105][105];

public:
    int nearestExit(vector<vector<char>>& maze, vector<int>& entrance) {
        int m = maze.size(), n = maze[0].size();
        queue<PII> q;
        q.push({entrance[0], entrance[1]});
        vis[entrance[0]][entrance[1]] = 1;
        int cnt = 0;
        while (q.size()) {
            cnt++;
            int sz=q.size();
            for (int i = 0; i < sz; i++) {
                auto [a, b] = q.front();
                q.pop();
                for (int j = 0; j < 4; j++) {
                    int x = a + dx[j], y = b + dy[j];
                    if (x >= 0 && x < m && y >= 0 && y < n && !vis[x][y] &&
                            maze[x][y] == '.') {
                        if (x == 0 || x == m - 1 || y == 0 || y == n - 1) {
                            return cnt;
                        }
                        q.push({x, y});
                        vis[x][y] = 1;
                    }
                }
            }
        }
        return -1;
    }
};

433.最小基因变化

433.最小基因变化

代码语言:javascript
代码运行次数:0
复制
class Solution {
public:
    int minMutation(string startGene, string endGene, vector<string>& bank) {
        unordered_set<string> vis;
        unordered_set<string> hash(bank.begin(),bank.end());
        string change="ACGT";

        queue<string> q;
        q.push(startGene);
        vis.insert(startGene);

        if(startGene==endGene) return 0;
        if(!hash.count(endGene)) return -1;
        int ans=0;
        while(q.size())
        {
            ans++;
            int sz=q.size();
            while(sz--)
            {
                string t=q.front();
                q.pop();
                for(int i=0;i<8;i++)
                {
                    string tmp=t;
                    for(int j=0;j<4;j++)
                    {
                        tmp[i]=change[j];
                        if(hash.count(tmp)&&!vis.count(tmp))
                        {
                            if(tmp==endGene) return ans;
                            q.push(tmp);
                            vis.insert(tmp);
                        }
                    }
                }
            }
        }
        return -1;
    }
};

127.单词接龙

127.单词接龙

代码语言:javascript
代码运行次数:0
复制
class Solution {
public:
    int ladderLength(string beginWord, string endWord, vector<string>& wordList) {
        unordered_set<string> hash(wordList.begin(),wordList.end());
        unordered_set<string> vis;
        if(!hash.count(endWord)) return 0;

        queue<string> q;
        q.push(beginWord);
        vis.insert(beginWord);
        int ans=1;
        while(q.size())
        {
            ans++;
            int sz=q.size();
            while(sz--)
            {
                string t=q.front();
                q.pop();
                for(int i=0;i<t.size();i++)
                {
                    string tmp=t;
                    for(char ch='a';ch<='z';ch++)
                    {
                        tmp[i]=ch;
                        if(hash.count(tmp)&&!vis.count(tmp))
                        {
                            if(tmp==endWord) return ans;
                            q.push(tmp);
                            vis.insert(tmp);
                        }
                    }
                }
            }
        } 
        return 0;
    }
};

675.为高尔夫比赛砍树

675.为高尔夫比赛砍树

代码语言:javascript
代码运行次数:0
复制
class Solution {
    typedef pair<int, int> PII;
    int m, n;
    int dx[4] = {0, 0, 1, -1}, dy[4] = {1, -1, 0, 0};
    bool vis[55][55];

public:
    int bfs(vector<vector<int>>& forest, int bx, int by, int ex, int ey) {
        if (bx == ex && by == ey)
            return 0;

        queue<PII> q;
        q.push({bx, by});
        vis[bx][by] = 1;
        memset(vis, 0, sizeof(vis));
        int step = 0;
        while (q.size()) 
        {
            step++;
            int sz = q.size();
            while (sz--) 
            {
                auto [a, b] = q.front();
                q.pop();
                for (int i = 0; i < 4; i++) 
                {
                    int x = a + dx[i], y = b + dy[i];
                    if (x >= 0 && x < m && y >= 0 && y < n && !vis[x][y] &&
                        forest[x][y] != 0) {
                        if (x == ex && y == ey)
                            return step;
                        q.push({x, y});
                        vis[x][y] = 1;
                    }
                }
            }
        }
        return -1;
    }

    int cutOffTree(vector<vector<int>>& forest) 
    {
        m = forest.size(), n = forest[0].size();
        vector<PII> tree;
        for (int i = 0; i < m; i++)
            for (int j = 0; j < n; j++)
                if (forest[i][j] > 1)
                    tree.push_back({i, j});

        sort(tree.begin(), tree.end(), [&](const PII& p1, const PII& p2) {
            return forest[p1.first][p1.second] < forest[p2.first][p2.second];
        });

        int bx = 0, by = 0, ret = 0;
        for (auto& [a, b] : tree)
        {
            int step = bfs(forest, bx, by, a, b);
            if (step == -1)
                return -1;
            ret += step;
            bx = a, by = b;
        }
        return ret;
    }
};
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2024-11-22,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1926. 迷宫中离入口最近的出口
  • 433.最小基因变化
  • 127.单词接龙
  • 675.为高尔夫比赛砍树
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档