752. 打开转盘锁

链接:

752. 打开转盘锁

题解:

cpp 复制代码
class Solution {
public:
    int openLock(vector<string>& deadends, string target) {
        std::unordered_set<std::string> table(deadends.begin(), deadends.end());
        if (table.find("0000") != table.end()
                || table.find(target) != table.end()) {
            return -1;
        }
        std::queue<std::string> que;
        std::unordered_map<std::string, int> distance;
        que.push(std::string("0000"));
        distance["0000"] = 0;
        auto get_next = [](std::string& word)->std::vector<std::string> {
            std::vector<std::string> result;
            for (int i = 0; i < word.size(); ++i) {
                char ch = word[i];
                if (word[i] == '9') {
                    word[i] = '0';
                } else {
                    ++word[i];
                }
                result.push_back(word);
                word[i] = ch;
                if (word[i] == '0') {
                    word[i] = '9';
                } else {
                    --word[i];
                }
                result.push_back(word);
                word[i] = ch;
            }
            return result;
        };
        while (!que.empty()) {
            int size = que.size();
            for (int i = 0; i < size; ++i) {
                auto f = que.front();
                que.pop();
                if (f == target) {
                    return distance[f];
                }
                for (auto& next : get_next(f)) {
                    //cout << "next = " << next << endl;
                    if (table.find(next) != table.end()) {
                        continue;
                    }
                    if (distance.find(next) != distance.end()) {
                        continue;
                    }
                    /*if (next == target) {
                        return distance[f] + 1;
                    }*/
                    distance[next] = distance[f] + 1;
                    que.push(next);
                }
            }
        }
        return -1;
    }
};
cpp 复制代码
class Solution {
public:
    int openLock(vector<string>& deadends, string target) {
        if (target == "0000") {
            return 0;
        }

        unordered_set<string> dead(deadends.begin(), deadends.end());
        if (dead.count("0000")) {
            return -1;
        }

        auto num_prev = [](char x) -> char {
            return (x == '0' ? '9' : x - 1);
        };
        auto num_succ = [](char x) -> char {
            return (x == '9' ? '0' : x + 1);
        };

        // 枚举 status 通过一次旋转得到的数字
        auto get = [&](string& status) -> vector<string> {
            vector<string> ret;
            string bak = status;
            for (int i = 0; i < 4; ++i) {
                char num = status[i];
                status[i] = num_prev(num);
                ret.push_back(status);
                status[i] = num_succ(num);
                ret.push_back(status);
                status = bak;
            }
            return ret;
        };

        queue<pair<string, int>> q;
        q.emplace("0000", 0);
        unordered_set<string> seen = {"0000"};
        int step = 0;
        while (!q.empty()) {
            auto [status, step] = q.front();
            q.pop();
            for (auto&& next_status: get(status)) {
                if (!seen.count(next_status) && !dead.count(next_status)) {
                    if (next_status == target) {
                        //return step + 1;
                        return step+1;
                    }
                    q.emplace(next_status, step + 1);
                    seen.insert(move(next_status));
                }
               
            }
            ++step;
        }

        return -1;
    }
};
相关推荐
康谋自动驾驶6 分钟前
康谋分享 | 自动驾驶仿真进入“标准时代”:aiSim全面对接ASAM OpenX
人工智能·科技·算法·机器学习·自动驾驶·汽车
C++ 老炮儿的技术栈1 小时前
什么是函数重载?为什么 C 不支持函数重载,而 C++能支持函数重载?
c语言·开发语言·c++·qt·算法
yychen_java1 小时前
R-tree详解
java·算法·r-tree
MarkHard1232 小时前
Leetcode (力扣)做题记录 hot100(62,64,287,108)
算法·leetcode·职场和发展
王RuaRua2 小时前
[数据结构]5. 栈-Stack
linux·数据结构·数据库·链表
一只鱼^_3 小时前
牛客练习赛138(首篇万字题解???)
数据结构·c++·算法·贪心算法·动态规划·广度优先·图搜索算法
一只码代码的章鱼3 小时前
Spring的 @Validate注解详细分析
前端·spring boot·算法
邹诗钰-电子信息工程3 小时前
嵌入式自学第二十一天(5.14)
java·开发语言·算法
寒小松3 小时前
Problem E: List练习
java·数据结构·list
↣life♚3 小时前
从SAM看交互式分割与可提示分割的区别与联系:Interactive Segmentation & Promptable Segmentation
人工智能·深度学习·算法·sam·分割·交互式分割