BFS解决单源最短路相关leetcode算法题

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

迷宫中离入口最近的出口
在这里插入图片描述

class Solution {
    int dx[4] = {0,0,1,-1};
    int dy[4] = {1,-1,0,0};
    bool vis[101][101];
public:
    int nearestExit(vector<vector<char>>& maze, vector<int>& e) {
        //可简化为边权为1的最短路问题
        int m = maze.size(), n = maze[0].size();
        queue<pair<int,int>> q;
        q.push({e[0],e[1]});
        vis[e[0]][e[1]] = true;
        int step = 0;
        while(q.size())
        {
            step++;
            int sz = q.size();
            while(sz--)
            {
                auto [a,b] = q.front();
                q.pop();
                for(int k=0;k<4;k++)
                {
                    int x = a+dx[k],y = b+dy[k];
                    if(x>=0 && x<m && y>=0 && y<n && maze[x][y]=='.'&& !vis[x][y])
                    {
                        if(x==0 || x==m-1 || y==0 || y==n-1) return step;
                        q.push({x,y});
                        vis[x][y] = true;
                    }
                }
            }
        }
        return -1;
    }
};

2.最小基因变化

最小基因变化
在这里插入图片描述

class Solution {
public:
    int minMutation(string startGene, string endGene, vector<string>& bank) {
        //可转化为边权为1的最短路图论问题
        unordered_set<string> vis;//用于标记基因是否已经访问过
        unordered_set<string> hash(bank.begin(),bank.end());
        string change = "ACGT";
        if(startGene == endGene) return 0;
        if(!hash.count(endGene)) return -1;
        int ret = 0;
        queue<string> q;
        q.push(startGene);
        vis.insert(startGene);
        while(q.size())
        {
            ret++;
            int sz = q.size();
            while(sz--)
            {
                auto 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 ret;
                            q.push(tmp);
                            vis.insert(tmp);
                        }
                    }
                }
            }
        }
        return -1;
    }
};

3.单词接龙

单词接龙
在这里插入图片描述

class Solution {
public:
    int ladderLength(string beginWord, string endWord, vector<string>& wordList) {
        unordered_set<string> vis;
        unordered_set<string> hash(wordList.begin(),wordList.end());
        if(!hash.count(endWord)) return 0;
        queue<string> q;
        q.push(beginWord);
        vis.insert(beginWord);
        int ret = 1;
        while(q.size())
        {
            ret++;
            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 ret;
                            q.push(tmp);
                            vis.insert(tmp);
                        }
                    }
                }
            }
        }
        return 0;
    }
};

4.为高尔夫比赛砍树

为高尔夫比赛砍树
在这里插入图片描述

class Solution {
    int dx[4] = {0,0,1,-1};
    int dy[4] = {1,-1,0,0};
    bool vis[51][51];
    int m,n;
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(),[&](const pair<int,int>& p1,const pair<int,int>& p2)
        {
            return f[p1.first][p1.second] < f[p2.first][p2.second];
        });

        int ret = 0;
        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 x1,int y1)
     {
         if(bx == x1 && by == y1) return 0;
         queue<pair<int,int>> q;
         memset(vis,0,sizeof vis);//每次需对vis进行初始化,因为vis是全局的
         q.push({bx,by});
         vis[bx][by] = true;
         int step = 0;
         while(q.size())
         {
             step++;
             int sz = q.size();
             while(sz--)
             {
                 auto [a,b] = q.front();
                 q.pop();
                 for(int k =0;k<4;k++)
                 {
                     int x = a+dx[k] , y = b+dy[k];
                     if(x>=0 && x<m && y>=0 && y<n && f[x][y] && !vis[x][y])
                     {
                         if(x == x1&& y == y1 ) return step;
                         q.push({x,y});
                         vis[x][y] = true;
                     }
                 }
             }
         }
         return -1;
     }
};
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值