Word Ladder II

126. Word Ladder II

Hard

A transformation sequence from word beginWord to word endWord using a dictionary wordList is a sequence of words beginWord -> s1 -> s2 -> ... -> sk such that:

  • Every adjacent pair of words differs by a single letter.
  • Every si for 1 <= i <= k is in wordList. Note that beginWord does not need to be in wordList.
  • sk == endWord

Given two words, beginWord and endWord, and a dictionary wordList, return all the shortest transformation sequences from beginWord to endWord, or an empty list if no such sequence exists. Each sequence should be returned as a list of the words [beginWord, s1, s2, ..., sk].

Example 1:

Input: beginWord = "hit", endWord = "cog", wordList = ["hot","dot","dog","lot","log","cog"]
Output: [["hit","hot","dot","dog","cog"],["hit","hot","lot","log","cog"]]
Explanation: There are 2 shortest transformation sequences:
"hit" -> "hot" -> "dot" -> "dog" -> "cog"
"hit" -> "hot" -> "lot" -> "log" -> "cog"

Example 2:

Input: beginWord = "hit", endWord = "cog", wordList = ["hot","dot","dog","lot","log"]
Output: []
Explanation: The endWord "cog" is not in wordList, therefore there is no valid transformation sequence.

Constraints:

  • 1 <= beginWord.length <= 5
  • endWord.length == beginWord.length
  • 1 <= wordList.length <= 1000
  • wordList[i].length == beginWord.length
  • beginWord, endWord, and wordList[i] consist of lowercase English letters.
  • beginWord != endWord
  • All the words in wordList are unique.

struct GNode {
    GNode(std::string _val): val(_val){}  
    
    void addNeighborhood(GNode* _neighbor){
        neighbors.insert(_neighbor);
    }
    
    string val;
    set<GNode*> neighbors;
    int level = 2000;
};


struct WordGraph{
    WordGraph(){}
    WordGraph(string beginWord, string endWord, vector<string>& wordList){
        if(nodes.find(beginWord) == nodes.end()){
            n_root = new GNode(beginWord);
            n_root->level = 0;
            nodes[beginWord] = new GNode(beginWord);
        }
        
        if(nodes.find(endWord) == nodes.end()){
            n_dest = new GNode(endWord);
            n_dest->level = 2001;
            nodes[endWord] = n_dest;
        }
        
        for(string &w : wordList){
            if(nodes.find(w) == nodes.end()){
                nodes[w] = new GNode(w);
            }
        }
        buildLinks();
        //printGraph();
        
    }
    
    void buildLinks(){
        
        std::vector<GNode*> nodes_l1;
        
        nodes_l1.emplace_back(n_root);
        std::vector<GNode*> nodes_l2;
        
        for(auto &n : nodes_l1){
            nodes.erase(n->val);
        }
        while (!nodes_l1.empty()){
            for(size_t i=0; i < nodes_l1.size(); i++){
                GNode* n_current = nodes_l1[i];
                for(auto it = nodes.begin(); it != nodes.end(); it++){
                    if(distance1(n_current->val, it->second->val)){
                        if(it->second->level > n_current->level)
                        {
                            n_current->addNeighborhood(it->second);
                            it->second->level = n_current->level + 1;
                            nodes_l2.emplace_back(it->second);
                        }
                    }
                }
            }
            
            for(auto &n : nodes_l1){
                nodes.erase(n->val);
            }
            nodes_l1.clear();
            nodes_l1.swap(nodes_l2);
            
            
        }
    }
    
    bool distance1(string &w1, string &w2){
        
        string w1_sub = w1.substr(1, w1.size() - 1);
        string w2_sub = w2.substr(1, w2.size() - 1);
        
        if(w1_sub == w2_sub){
            return true;
        }
        
        w1_sub = w1.substr(0, w1.size() - 1);
        w2_sub = w2.substr(0, w2.size() - 1);
        
        if(w1_sub == w2_sub){
            return true;
        }
        
        for(int i = 1; i< w1.size() - 1; i++){
            w1_sub = w1.substr(0,i) + w1.substr(i+1,w1.size()-i-1);
            w2_sub = w2.substr(0,i) + w2.substr(i+1, w2.size()-i-1);
            
            if(w1_sub == w2_sub){
                return true;
            }
        }
        
        return false;
    }
    
    void printGraph(){
        std::cout<<n_root->val<<" "<<n_root->level<<std::endl;
        set<GNode*> neighbors = n_root->neighbors;
        set<GNode*> neighbors_downlevel;
        while(!neighbors.empty()){
            for(auto &n : neighbors){
                for(auto &nei : n->neighbors){
                    neighbors_downlevel.insert(nei);
                }
                std::cout<<"val:"<<n->val<<" "<<n->level<<" ";
            }
            std::cout<<endl;
            neighbors.clear();
            neighbors.swap(neighbors_downlevel);
        }
        
    }
    
    unordered_map<string, GNode*> nodes;
    GNode* n_root;
    GNode* n_dest;
    
};

class Solution {
public:
    vector<vector<string>> findLadders(string beginWord, string endWord, vector<string>& wordList) {
        bool flag = false;
        for(auto &n: wordList){
            if(n == endWord){
                flag = true;
                break;
            }
        }
        if(!flag)
            return vector<vector<string>>();
        
        wg_ = WordGraph(beginWord, endWord, wordList);   
        vector<string> path;
        backtrace(wg_.n_root, wg_.n_dest, path);
        
        if(!paths_.empty())
            return paths_.begin()->second;
        else
            return vector<vector<string>>();
    }
    
    void backtrace(GNode* _n_cur, GNode* _n_dest, vector<string> &_path){
        
        _path.emplace_back(_n_cur->val);
        
        if(_n_cur->val == _n_dest->val){
            paths_[_path.size()].emplace_back(_path);
            return;
        }
        set<GNode*> neighbors_downlevel = _n_cur->neighbors;
        
        for(auto &n: neighbors_downlevel){
            backtrace(n, _n_dest, _path);
            _path.pop_back();
        }
        //return false;
    }
private:
    WordGraph wg_;
    map<size_t,vector<vector<string>>> paths_;
};

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
基恩士(Kinco)的ladder builder是一款用于PLC(可编程逻辑控制器)编程的软件工具。PLC是一种用于自动化控制的硬件设备,常用于工业领域中的各种控制系统。 ladder builder是基恩士公司为了方便用户进行PLC编程开发的工具软件。它提供了直观、简单的梯形图编程界面,使用户能够以图形化的方式编写和编辑PLC程序。通过该软件,用户可以使用梯形图的元素(如继电器、计数器、定时器等)来构建逻辑控制程序,实现对机器、设备或生产线的自动化控制。 使用ladder builder进行PLC编程的优点是它的易学性和易用性。用户无需具备高级的编程知识,只需掌握梯形图的基本原理即可。软件提供了丰富的模块和函数库,用户可以根据实际需要选择和配置,以满足各种控制需求。此外,ladder builder还支持多种编程语言,如ST(结构化文本)、FBD(功能块图)、SFC(顺序功能图)等,使得用户可以根据自己的喜好和需求选择适合自己的编程方式。 另外,ladder builder还具有调试和仿真功能,用户可以在软件中进行程序的调试和测试,以确保程序的正确性和可靠性。通过仿真功能,用户可以在实际硬件设备之前对程序进行模拟,以检查程序的逻辑是否正确,避免出现错误和故障。这大大节省了用户的时间和成本。 基恩士的ladder builder软件是一款功能强大、易于使用的PLC编程工具,它提供了丰富的功能和模块,使用户能够以图形化的方式轻松编写和编辑PLC程序,实现对机器和设备的自动化控制。无论是初学者还是经验丰富的工程师,都可以通过ladder builder来提高PLC编程的效率和质量。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值