003 双向 BFS 实现解开密码锁的最少次数[C++]

//双向 BFS
class Solution
{
public:
    int openLock(vector<string>& deadends, string target)
    {
        // BFS 模板,打印所有可能密码
        unordered_set <string> q1, q2;
        unordered_set <string> dead;
        unordered_set <string> version;

        for (string str : deadends)  dead.insert(str);

        //把初始密码添加到队列中
        q1.insert("0000");
        q2.insert(target);

        int depth = 0;

        while (!q1.empty() && !q2.empty())
        {
            //该条件语句是双向 BFS 的优化
            if (q1.size() > q2.size())
            {
                unordered_set<string> temp1 = q1;
                q1 = q2;
                q2 = temp1;
            }

            unordered_set <string> temp;

            for ( string cur : q1 )
            {

                //判断是否是正确密码
                if (dead.find(cur) != dead.end()) continue;

                //判断两个容器是否有交集,若有交集则结束——关键之处
                if (q2.find(cur) != q2.end()) return depth;

                version.insert(cur);


                for (int j = 0; j < 4; j++)
                {
                    //转动一次密码并添加到队列中
                    string up = plusone(cur, j);

                    //判断是否是回头路
                    if (version.find(up) == version.end())
                        temp.insert(up);

                    string down = minnusone(cur, j);
                    if (version.find(down) == version.end())
                        temp.insert(down);
                }
            }
            depth++;

            // temp 相当于 q1
            // 在这里交换 q1 和 q2 ,下一轮 while 会扩散 q2
            q1 = q2;
            q2 = temp;
        }
        return -1;
    }

private:

    string plusone(string s, int i)
    {
        string ch = s;

        if (ch[i] == '9')   ch[i] = '0';
        else ch[i] += 1;
        return ch;
    }

    string minnusone(string s, int i)
    {
        string ch = s;

        if (ch[i] == '0')   ch[i] = '9';
        else ch[i] -= 1;
        return ch;
    }
};

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值