搜索算法之广度优先

/*
// Employee info
class Employee {
public:
    // It's the unique ID of each node.
    // unique id of this employee
    int id;
    // the importance value of this employee
    int importance;
    // the id of direct subordinates
    vector<int> subordinates;
};
*/
class Solution {
public:
    int getImportance(vector<Employee*> employees, int id) {
        
    }
};

1. N叉树的层序遍历

/*
// Definition for a Node.
class Node {
public:
    int val;
    vector<Node*> children;

    Node() {}

    Node(int _val, vector<Node*> _children) {
        val = _val;
        children = _children;
    }
};
*/
class Solution {
public:
    vector<vector<int>> levelOrder(Node* root) {
        vector<vector<int>> V;
        queue<Node*> Q[2];
        int i = 0;
        Q[i].push(root);
        while(!Q[i].empty())
        {
            vector<int> V2;
            while(!Q[i].empty())
            {
                Node* cur = Q[i].front();
                Q[i].pop();
                if(cur == nullptr)
                    continue;
                V2.push_back(cur->val);
                for(auto e : cur->children)
                    Q[!i].push(e);
            }
            if(V2.size()>0)
                V.push_back(V2);
            i = !i;
        }
        return V;
    }
};

2. 腐烂的橘子

在给定的网格中,每个单元格可以有以下三个值之一:

值 0 代表空单元格;
值 1 代表新鲜橘子;
值 2 代表腐烂的橘子。
每分钟,任何与腐烂的橘子(在 4 个正方向上)相邻的新鲜橘子都会腐烂。

返回直到单元格中没有新鲜橘子为止所必须经过的最小分钟数。如果不可能,返回 -1

来源:力扣

/*
class pair
{
    int first;
    int second;

}
*/
class Solution {
public:
    int orangesRotting(vector<vector<int>>& grid) {
        static int pos[4][2] = {{0,1},{1,0},{-1,0},{0,-1}};
        //队列里面存放坐标
        queue<pair<int,int>> q;
        //1.找到所有坏的值
        int row = grid.size();
        int col = grid[0].size();
        for(int i = 0; i<row; i++)
            for(int j = 0; j<col; j++)
            {
                if(grid[i][j] == 2)
                    q.push(make_pair(i,j));
            }
        //在已坏的橘子周围搜索新鲜橘子
        int minTime = 0;
        while(!q.empty())
        {
            int flag = 0;
            //取出所有坏掉的橘子
            int sz = q.size();
            while(sz--)
            {
                pair<int,int> curPos=q.front();
                q.pop();
                for(int i = 0; i<4;++i)
                {
                    int nx = curPos.first+pos[i][0];
                    int ny = curPos.second+pos[i][1];
                    if(nx>=row || nx<0 || ny>=col || ny<0)
                        continue;
                    if(grid[nx][ny]==1)
                    {
                        flag = 1;
                        grid[nx][ny]=2;
                        q.push(make_pair(nx,ny));
                    } 
                }
            }
            if(flag)
                ++minTime;
        }
        //搜索完,遍历查看是否还有新鲜橘子
        for(int i = 0; i<row; i++)
        {
            for(int j = 0; j< col; j++)
                if(grid[i][j] == 1)
                    return -1;
        }
        return minTime;
    }
};

3. 单词接龙

给定两个单词(beginWord 和 endWord)和一个字典,找到从 beginWord 到 endWord 的最短转换序列的长度。转换需遵循如下规则:

每次转换只能改变一个字母。
转换过程中的中间单词必须是字典中的单词。
说明:

如果不存在这样的转换序列,返回 0。
所有单词具有相同的长度。
所有单词只由小写字母组成。
字典中不存在重复的单词。
你可以假设 beginWord 和 endWord 是非空的,且二者不相同。
示例 1:

输入:
beginWord = “hit”,
endWord = “cog”,
wordList = [“hot”,“dot”,“dog”,“lot”,“log”,“cog”]

输出: 5

解释: 一个最短转换序列是 “hit” -> “hot” -> “dot” -> “dog” -> “cog”,
返回它的长度 5。

来源:力扣(LeetCode)
解析:
BeginWord入队,标记BeginWord;遍历当前队列所有单词
每次取出队头单词,判断单子是否和目标相同,如果相同,返回结果;
变换当前单词:(单词长度*25)
判断新的单词是否在字典中,且没有使用过:入队;
转换长度+1

class Solution {
public:
    int ladderLength(string beginWord, string endWord, vector<string>& wordList) {
        queue<string> q;
        q.push(beginWord);
        //标记,哈希标记
        unordered_set<string> vist;
        vist.insert(beginWord);
        //词典中的单词也保存到哈希表中
        unordered_set<string> dict;
        for(auto& wd : wordList)
        {
            dict.insert(wd);
        }
        int minTrans = 0;
        while(!q.empty())
        {
            //每次取出队列中的所有单词
            int sz = q.size();
            while(sz--)
            {
                string curStr=q.front();
            q.pop();
            if(curStr == endWord)
                return minTrans+1;
            //变换当前单词:25*单词长度
            for(int i = 0; i<curStr.size(); i++)
                {
                 string tmp = curStr;
                 for(char ch = 'a'; ch <='z'; ch++)
                    {
                        tmp[i]=ch; 
                        if(dict.find(tmp) != dict.end() && vist.find(tmp) == vist.end())
                        {
                            q.push(tmp);
                            vist.insert(tmp);
                        }
                        
                    }
                }
            }
            minTrans++;
        }
        return 0;
    }
};

3. 打开锁转盘

你有一个带有四个圆形拨轮的转盘锁。每个拨轮都有10个数字: ‘0’, ‘1’, ‘2’, ‘3’, ‘4’, ‘5’, ‘6’, ‘7’, ‘8’, ‘9’ 。每个拨轮可以自由旋转:例如把 ‘9’ 变为 ‘0’,‘0’ 变为 ‘9’ 。每次旋转都只能旋转一个拨轮的一位数字。

锁的初始数字为 ‘0000’ ,一个代表四个拨轮的数字的字符串。

列表 deadends 包含了一组死亡数字,一旦拨轮的数字和列表里的任何一个元素相同,这个锁将会被永久锁定,无法再被旋转。

字符串 target 代表可以解锁的数字,你需要给出最小的旋转次数,如果无论如何不能解锁,返回 -1。

示例 1:

输入:deadends = [“0201”,“0101”,“0102”,“1212”,“2002”], target = “0202”
输出:6
解释:
可能的移动序列为 “0000” -> “1000” -> “1100” -> “1200” -> “1201” -> “1202” -> “0202”。
注意 “0000” -> “0001” -> “0002” -> “0102” -> “0202” 这样的序列是不能解锁的,
因为当拨动到 “0102” 时这个锁就会被锁定。
示例 2:

输入: deadends = [“8888”], target = “0009”
输出:1
解释:
把最后一位反向旋转一次即可 “0000” -> “0009”。

来源:力扣(LeetCode)

0000入队,标记0000;
遍历队列中的所有密码
判断密码时候和目标相同,如果相同,返回结果
变换当前木马:4*2,得到新的密码
判断新的密码是否在死亡数组中,且没有标记过:不再数组中,没有标记过:则入队;
旋转次数+1

class Solution {
public:
    int openLock(vector<string>& deadends, string target) {
        unordered_set<string> dead;
        for(auto e : deadends)
            dead.insert(e);
        queue<string> q;
        if(dead.find("0000") != dead.end())
            return -1;
        q.push("0000");
        unordered_set<string> visit;
        visit.insert("0000");
        
        int minTrans = 0;
        while(!q.empty())
        {
            int sz = q.size();
            while(sz--)
            {
                string curStr=q.front();
                q.pop();
                if(curStr==target)
                    return minTrans;
                for(int i = 0; i<curStr.size(); i++)
                {
                    string tmp1 = curStr;
                    string tmp2 = curStr;
                    if(tmp1[i]=='9')
                        tmp1[i]='0';
                    else
                        tmp1[i]++;
                    if(tmp2[i]=='0')
                        tmp2[i]='9';
                    else
                        tmp2[i]--;
                    if(dead.find(tmp1) == dead.end() && visit.find(tmp1) == visit.end())
                    {
                        dead.insert(tmp1);
                        visit.insert(tmp1);
                    }
                    if(dead.find(tmp2) == dead.end() && visit.find(tmp2) == visit.end())
                    {
                        dead.insert(tmp2);
                        visit.insert(tmp2);
                    }
                        
                }
            }
            minTrans++;
        }
        return -1;
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值