126. Word Ladder II( Queue; BFS)

Given two words (beginWord and endWord), and a dictionary's word list, find all shortest transformation sequence(s) from beginWord to endWord, such that:

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

For example,

Given:
beginWord = "hit"
endWord = "cog"
wordList = ["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.

思路:仍然使用两个队列做WFS, 不同于I的是

  1. 入队的元素是从根节点至当前节点的单词数组
  2. 不能立即删dict中的元素,因为该元素可能在同级的其他节点中存在
class Solution {
public:
    vector<vector<string>> findLadders(string beginWord, string endWord, unordered_set<string> &wordList) {
        queue<vector<string>> queue_to_push;
        queue<vector<string>> queue_to_pop;
        set<string> flag;
        set<string>::iterator it;
        bool endFlag = false;
        string curStr;
        vector<string> curVec;
        vector<vector<string>> ret;
        char tmp;
        
        curVec.push_back(beginWord);
        queue_to_pop.push(curVec);
        wordList.erase(beginWord); //if beginWord is in the dict, it should be erased to ensure shortest path
        while(!queue_to_pop.empty()){
            curVec = queue_to_pop.front();
            curStr = curVec[curVec.size()-1];
            queue_to_pop.pop();
            
            //find one letter transformation
            for(int i = 0; i < curStr.length(); i++){ //traverse each letter in the word
                for(int j = 'a'; j <= 'z'; j++){ //traverse letters to replace
                    if(curStr[i]==j) continue; //ignore itself
                    tmp = curStr[i];
                    curStr[i]=j;
                    if(curStr == endWord){
                        curVec.push_back(curStr);
                        ret.push_back(curVec);
                        endFlag = true;
                        curVec.pop_back(); //back track
                    }
                    else if(!endFlag && wordList.count(curStr)>0){ //occur in the dict
                        flag.insert(curStr);
                        curVec.push_back(curStr);
                        queue_to_push.push(curVec);
                        curVec.pop_back(); //back track
                    }
                    curStr[i] = tmp; //back track
                }
            }
            
            if(queue_to_pop.empty()){//move to next level
                if(endFlag){ //terminate
                    break; //Maybe there's no such path, so we return uniformaly at the end
                }
                for(it=flag.begin(); it != flag.end();it++){ //erase the word occurred in current level from the dict
                    wordList.erase(*it);
                }
                swap(queue_to_pop, queue_to_push); //maybe there's no such path, then endFlag = false, queue_to_push empty, so we should check if queue_to_pop is empty in the next while
                flag.clear();
            }
        }
        return ret;
    }
};

 

posted on 2015-10-04 17:34  joannae  阅读(268)  评论(0编辑  收藏  举报

导航