[Leetcode]Word Ladder II

2014-11-24 10:52:35 · 作者: · 浏览: 0

题目:

Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) from start to end, such that:

  1. Only one letter can be changed at a time
  2. Each intermediate word must exist in the dictionary

    For example,

    Given:
    start = "hit"
    end = "cog"
    dict = ["hot","dot","dog","lot","log"]

    Return

      [
        ["hit","hot","dot","dog","cog"],
        ["hit","hot","lot","log","cog"]
      ]
    

    Note:

    • All words have the same length.
    • All words contain only lowercase alphabetic characters.

      解题思路:

      这道题居然被网友标注为难度1,而另一道Word Ladder被标注为难度3。事实上在做的过程中我感觉Word Ladder II的难度比Word Ladder要高。解题的关键在于选取适当的数据结构,题目要求输出所有路径,不像Word Ladder里一样只需要输出最少路径节点数。因此不是在该层找到第一个可达节点就能返回,而是要访问所有可达节点,并且记录下节点之间的关系。一个节点可能经过多个下一层节点到达目的节点,也就是说一个节点可能对应多个下一层节点,同时,可能有多个节点经过字符变换以后到达下一层的某一个节点,因此一个节点可能与多个上一层节点相对应起来。因此考虑采用multimap来建立节点之间的关系。从起点开始采用BFS搜索,路径上会经过许多无效路径(不是最短路径的路径或不可达路径),因此如果当前节点与下一层节点的关系,就会有许多干扰关系的存在。注意到与目的节点关联的节点总是有效节点,因此在建立节点关系时,采用将当前节点与上一层节点关联的方法,这样一来我们最后就可以从终点出发,倒推出各条有效最短路径。multimap的键为当前节点,值为与当前节点关联的上一层节点。

      另一方面,由于采用BFS,可以采用队列来存储待访问的节点。然而,在访问节点时需要保证访问的节点不在当前层次和上一层次的已有节点中(前者会导致重复访问,后者会导致反复循环),需要对节点进行剪枝。对于前一个问题,可以设置一个visited标志,如果已经访问过,就不将该节点继续压入待访问队列。对于后一个问题,可以设一个变量来记录当前的层次数,当访问到下一个层次时,将上一层次的所有节点从字典里面清空。

      通过以上的步骤和细节处理,就可以建立起从目的节点到初始节点的一副有向无环图。这时只需要用DFS算法将各条路径记录下来即可,需要注意到从目的节点开始DFS所得到的结果是反向的路径,对于vector容器,可以用reverse函数将其反转。下面给出代码。

      代码:

      class Solution {
      public:
          vector
            
             > findLadders(string start, string end, unordered_set
             
               &dict) { vector
              
               > Path; vector
               
                 CurrPath; unordered_set
                
                  visited; queue
                 
                  > CurrCandidate; multimap
                  
                    father; bool finded=false; int curr_step=0; int WordSize=start.size(); if(start.empty()||end.empty())return Path; if(start==end){ CurrPath.push_back(start); Path.push_back(CurrPath); } if(dict.count(start))dict.erase(start); if(dict.count(end))dict.erase(end); CurrCandidate.push(make_pair(start,1)); visited.insert(start); while(!CurrCandidate.empty()){ pair
                   
                     CurrWord(CurrCandidate.front()); CurrCandidate.pop(); if(curr_step
                    
                     ::iterator iter; for(iter=visited.begin();iter!=visited.end();iter++){ dict.erase(*iter); } curr_step=CurrWord.second; visited.clear(); } for(int i=0;i
                     
                       father,string start,string end,vector
                      
                        &CurrPath,vector
                       
                        > &Path){ CurrPath.push_back(end); if(start==end){ Path.push_back(CurrPath); reverse(Path.back().begin(),Path.back().end()); CurrPath.pop_back(); return; } pair
                        
                         ::iterator,multimap
                         
                          ::iterator> pos=father.equal_range(end); while(pos.first!=pos.second){ build_path(father,start,pos.first->second,CurrPath,Path); pos.first++; } CurrPath.pop_back(); } };