LeetCode(127) Word Ladder

题目如下:

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

Only one letter can be changed at a time
Each intermediate word must exist in the dictionary
For example,

Given:
start = "hit"
end = "cog"
dict = ["hot","dot","dog","lot","log"]
As one shortest transformation is "hit" -> "hot" -> "dot" -> "dog" -> "cog",
return its length 5.


分析如下:

因为要找寻最小length,所以需要BFS而不是DFS。


我的代码:

//331ms
class Solution {
public:
    int ladderLength(string start, string end, unordered_set<string> &dict) {
        queue<string> my_queue;
        my_queue.push(start);
        int length = 0;
        map<string, int> length_count;
        //NOTE:
        //1 map记录了历史路径,避免走成回路造成死循环。
        //2 灵活地使用map<string, int>来表示level信息。
        //3 一开始使用map<string, string>表示父子关系,非常麻烦易错。
        length_count[start] = 1;
        string current = "";
        int type_code = 0;
        while(!my_queue.empty()) {
            current = my_queue.front();
            my_queue.pop();
            
            if (current == end) {
                length = length_count[end];
                break;
            }
            
            for (int i = 0; i < current.length(); ++i) {
                string temp_current = current;
                for (char character = 'a'; character <= 'z'; ++character) {
                    temp_current[i] = character;
                    //判断是否当前单词在历史记录map中,避免走成回路造成死循环。
                    if (dict.find(temp_current) != dict.end() && length_count.find(temp_current) == length_count.end() ) {
                        my_queue.push(temp_current);
                        length_count[temp_current] = length_count[current] + 1;
                    }
                }
            }
            
        }
        return length;
    }
};

updated : 2015-04-06

//582ms
class Solution {
public:
    int ladderLength(string start, string end, unordered_set<string> &dict) {
        int length = start.length();
        queue<string> que;
        map<string, int> word_level;
        que.push(start);
        word_level[start] = 1;
        
        while(!que.empty()) {
            string keep_current = que.front();
            que.pop();
            if (end == keep_current) {
                //std::cout<<"3, keep_current = "<<keep_current<<", end = "<<end<<std::endl;
                return word_level[end];
            }
            for (int i = 0; i < length; ++i) {
                string current = keep_current;
                //std::cout<<"1, current ="<<current<<std::endl;
                for(char alpha = 'a'; alpha <= 'z'; ++alpha) {
                    current[i] = alpha;
                    //std::cout<<"2, current ="<<current<<std::endl;
                    if ((dict.find(current) != dict.end() &&
                         word_level.find(current) == word_level.end())||
                        (current==end && word_level.find(current) == word_level.end()))
                    {
                        que.push(current);
                        //std::cout<<"3, current ="<<current<<std::endl;
                        word_level[current] = word_level[keep_current] + 1;
                        //std::cout<<"cur="<<current<<",lvl="<< word_level[current]<<std::endl;
                    }
                }
            }
        }
        return 0;
    }
};



你可能感兴趣的:(LeetCode,bfs)