LeetCode:127 单词接龙 哈希set+BFS 与双端BFS优化
题目描述
给定两个单词(beginWord 和 endWord)和一个字典,找到从 beginWord 到 endWord 的最短转换序列的长度。转换需遵循如下规则:
每次转换只能改变一个字母。
转换过程中的中间单词必须是字典中的单词。
说明:
如果不存在这样的转换序列,返回 0。
所有单词具有相同的长度。
所有单词只由小写字母组成。
字典中不存在重复的单词。
你可以假设 beginWord 和 endWord 是非空的,且二者不相同。
示例 1:
输入:
beginWord = “hit”,
endWord = “cog”,
wordList = [“hot”,“dot”,“dog”,“lot”,“log”,“cog”]
输出: 5
解释: 一个最短转换序列是 “hit” -> “hot” -> “dot” -> “dog” -> “cog”,返回它的长度 5。
示例 2:
输入:
beginWord = “hit”
endWord = “cog”
wordList = [“hot”,“dot”,“dog”,“lot”,“log”]
输出: 0
解释: endWord “cog” 不在字典中,所以无法进行转换。
来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/word-ladder
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
思路
不用邻接矩阵如何存储链接关系呢?这里因为图的边不带权,可以使用查找时间为常数的哈希set(unordered_set)来存储起始点与结束点,如果在某一次遍历中,遍历到结束集合的元素,就说明找到了
1.由起点到终点bfs
因为每次只走一步,我们可以通过维护三个集合来实现bfs
起始点集合 beginSet
未访问过的点集合 reachableSet
这一步到达的节点集合 this_step
- 每次遍历访问起始点集合beginSet中的元素,将他们从未访问集合reachableSet中删除
- 遍历起始点集合beginSet中的元素,对于每一个元素,找到所有与其【相邻的且未被访问的元素】,将他们加入这一步到达的集合this_step中
- 起点集合设置为这一步到达的集合this_step,重复遍历直到起点集合为空,或者遍历过程中遇到终点,就返回步长
代码(未优化,下面有优化版本)
class Solution {
public:
int ladderLength(string beginWord, string endWord, vector<string>& wordList)
{
unordered_set<string> reachable(wordList.begin(), wordList.end());
if(reachable.find(endWord) == reachable.end())
{
return 0;
}
unordered_set beginSet {beginWord};
unordered_set endSet {endWord};
int ans = 1;
while(!beginSet.empty())
{
unordered_set<string>::iterator i;
// 从未被访问集合中删除起始点们,(也就是已经访问过了)
for(i=beginSet.begin(); i!=beginSet.end(); i++)
{
reachable.erase(*i);
}
ans += 1;
// 遍历起始点,找寻和起始点相邻的点
unordered_set<string> this_step;
for(i=beginSet.begin(); i!=beginSet.end(); i++)
{
// 只变某个位置的一个字母,穷举位置与字母来判断是否相邻
for(int j=0; j<(*i).length(); j++)
{
string s = *i;
for(char c='a'; c<='z'; c++)
{
s[j] = c;
// 如果是可达点,或找到答案,或放到这一步走的集合中
if(reachable.find(s) != reachable.end())
{
if(endSet.find(s) != endSet.end())
{
return ans;
}
this_step.insert(s);
}
}
}
}
// 起始集合变为这次走到的点,向前bfs
beginSet = this_step;
}
return 0;
}
};
2.双端BFS 优化
我们不一定严格第从起点走到终点,我们可以从起点向外拓展一步,从终点向外拓展一步,交替进行,只要找到终点集合中的元素,意味着我们找到了最短路径,这表明我们需要维护四个集合了:
起始点集合 beginSet
终点集合 endSet
未访问过的点集合 reachableSet
这一步到达的节点集合 this_step
交替选择集合的准则如下:
每次总是选择this_step, endSet
中点的数目最少的集合作为起始点集合,另一集合作为终点集合
代码
class Solution {
public:
int ladderLength(string beginWord, string endWord, vector<string>& wordList)
{
unordered_set<string> reachable(wordList.begin(), wordList.end());
if(reachable.find(endWord) == reachable.end())
{
return 0;
}
unordered_set beginSet {beginWord};
unordered_set endSet {endWord};
int ans = 1;
while(!beginSet.empty())
{
unordered_set<string>::iterator i;
// 从未被访问集合中删除起始点们,(也就是已经访问过了)
for(i=beginSet.begin(); i!=beginSet.end(); i++)
{
reachable.erase(*i);
}
ans += 1;
// 遍历起始点,找寻和起始点相邻的点
unordered_set<string> this_step;
for(i=beginSet.begin(); i!=beginSet.end(); i++)
{
// 只变某个位置的一个字母,穷举位置与字母来判断是否相邻
for(int j=0; j<(*i).length(); j++)
{
string s = *i;
for(char c='a'; c<='z'; c++)
{
s[j] = c;
// 如果是可达点,或找到答案,或放到这一步走的集合中
if(reachable.find(s) != reachable.end())
{
if(endSet.find(s) != endSet.end())
{
return ans;
}
this_step.insert(s);
}
}
}
}
// 双端bfs:总是选择点少的作为起始集合,能够有效剪枝
if(endSet.size() < this_step.size())
{
beginSet = endSet;
endSet = this_step;
}
else
{
beginSet = this_step;
}
}
return 0;
}
};