首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >【BFS最短路】迷宫中离入口最近的出口 / 最小基因变化 / 单词接龙 / 为高尔夫比赛砍树

【BFS最短路】迷宫中离入口最近的出口 / 最小基因变化 / 单词接龙 / 为高尔夫比赛砍树

作者头像
_小羊_
发布2025-04-09 08:43:33
发布2025-04-09 08:43:33
1270
举报
文章被收录于专栏:C++C++

迷宫中离入口最近的出口

代码语言:javascript
复制
class Solution {
public:
    int nearestExit(vector<vector<char>>& maze, vector<int>& entrance) {
        int dx[4] = {1, -1, 0, 0}, dy[4] = {0, 0, 1, -1};
        bool used[101][101] = {};
        int m = maze.size(), n = maze[0].size(), step = 0;
        queue<pair<int, int>> q;
        int row = entrance[0], col = entrance[1];
        q.push({row, col});
        used[row][col] = true;
        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 && maze[x][y] == '.' 
                        && !used[x][y])
                    {
                        if (x == 0 || x == m - 1 || y == 0 || y == n - 1) return step;
                        used[x][y] = true;
                        q.push({x, y});
                    }
                }
            }
        }
        return -1;
    }
};

最小基因变化

代码语言:javascript
复制
class Solution {
public:
    int minMutation(string startGene, string endGene, vector<string>& bank) {
        unordered_set<string> hash(bank.begin(), bank.end());
        unordered_set<string> used;
        const string str = "ACGT";

        if (startGene == endGene) return 0;
        if (!hash.count(endGene)) return -1;

        queue<string> q;
        q.push(startGene);
        used.insert(startGene);
        int step = 0;
        while (q.size())
        {
            step++;
            int sz = q.size();
            while (sz--)
            {
                string s = q.front();
                q.pop();
                for (int i = 0; i < 8; i++)
                {
                    string tmp = s;
                    for (int j = 0; j < 4; j++)
                    {
                        tmp[i] = str[j];
                        if (tmp == endGene) return step;
                        if (hash.count(tmp) && !used.count(tmp))
                        {
                            used.insert(tmp);
                            q.push(tmp);
                        }
                    }
                }
            }
        }
        return -1;
    }
};

单词接龙

代码语言:javascript
复制
class Solution {
public:
    int ladderLength(string beginWord, string endWord, vector<string>& wordList) {
        unordered_set<string> hash(wordList.begin(), wordList.end());
        unordered_set<string> used;
        queue<string> q;
        if (!hash.count(endWord)) return 0;
        q.push(beginWord);
        used.insert(beginWord);
        int ret = 1; // 最初beginWord也算一个单词
        while (q.size())
        {
            ret++;
            int sz = q.size();
            while (sz--)
            {
                string str = q.front();
                q.pop();
                for (int i = 0; i < beginWord.size(); i++)
                {
                    string tmp(str);
                    for (char ch = 'a'; ch <= 'z'; ch++)
                    {
                        tmp[i] = ch;
                        if (tmp == endWord) return ret;
                        if (hash.count(tmp) && !used.count(tmp))
                        {
                            used.insert(tmp);
                            q.push(tmp);
                        }
                    }
                }
            }
        }
        return 0;
    }
};

为高尔夫比赛砍树

代码语言:javascript
复制
class Solution {
    int dx[4] = {1, -1, 0, 0}, dy[4] = {0, 0, 1, -1};
    int m, n, ret = 0;
public:
    int cutOffTree(vector<vector<int>>& forest) {
        m = forest.size(), n = forest[0].size();
        map<int, pair<int, int>> hash;
        for (int i = 0; i < m; i++)
            for (int j = 0; j < n; j++)
                if (forest[i][j] > 1)
                    hash[forest[i][j]] = {i, j};
        int bx = 0, by = 0;
        for (auto &[a, b] : hash)
        {
            int step = bfs(forest, bx, by, b.first, b.second);
            if (step == -1) return -1;
            ret += step;
            bx = b.first;
            by = b.second;
        }
        return ret;
    }
    int bfs(vector<vector<int>>& forest, int bx, int by, int ex, int ey)
    {
        if (bx == ex && by == ey) return 0;
        int ret = 0;
        queue<pair<int, int>> q;
        bool used[51][51] = {};
        q.push({bx, by});
        used[bx][by] = true;
        while (q.size())
        {
            ret++;
            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 == ex && y == ey) return ret;
                    if (x >= 0 && x < m && y >= 0 && y < n && forest[x][y] && !used[x][y])
                    {
                        used[x][y] = true;
                        q.push({x, y});
                    }
                }
            }
        }
        return -1;
    }
};
代码语言:javascript
复制
class Solution {
    int dx[4] = {1, -1, 0, 0}, dy[4] = {0, 0, 1, -1};
    int m, n, ret = 0;
public:
    int cutOffTree(vector<vector<int>>& f) {
        m = f.size(), n = f[0].size();
        vector<pair<int, int>> trees;
        for (int i = 0; i < m; i++)
            for (int j = 0; j < n; j++)
                if (f[i][j] > 1) 
                    trees.push_back({i, j});
        sort(trees.begin(), trees.end(), 
            [&f](const pair<int, int>& p1, const pair<int, int>& p2)
            { return f[p1.first][p1.second] < f[p2.first][p2.second];});
        
        int bx = 0, by = 0;
        for (auto [a, b] : trees)
        {
            int step = bfs(f, bx, by, a, b);
            if (step == -1) return -1;
            ret += step;
            bx = a, by = b;
        }
        return ret;
    }
    int bfs(vector<vector<int>>& f, int bx, int by, int ex, int ey)
    {
        // 处理边界情况
        if (bx == ex && by == ey) return 0;
        queue<pair<int, int>> q;
        bool used[51][51] = {};
        q.push({bx, by});
        used[bx][by] = true;
        int ret = 0;
        while (q.size())
        {
            ret++;
            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 == ex && y == ey) return ret;
                    if (x < 0 || x >= m || y < 0 || y >= n || !f[x][y] || used[x][y])
                        continue;
                    used[x][y] = true;
                    q.push({x, y});
                }
            }
        }
        return -1;
    }
};

本篇文章的分享就到这里了,如果您觉得在本文有所收获,还请留下您的三连支持哦~

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2025-04-08,如有侵权请联系 cloudcommunity@tencent.com 删除

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

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

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

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