-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path127.cpp
59 lines (57 loc) · 1.94 KB
/
127.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
class Solution {
public:
int ladderLength(string beginWord, string endWord, vector<string>& wordList) {
// 所有单词的集合
unordered_set<string> words;
for(auto it : wordList) words.insert(it);
vector<vector<string> > res;
// 使用队列进行BFS
queue<vector<string> > paths;
paths.push({beginWord});
int level = 1;
int min_level = INT_MAX;
// 在本层中使用过的单词
vector<string> used;
while(!paths.empty())
{
vector<string> path = paths.front();
paths.pop();
// 遍历到新的层上
if(path.size() > level)
{
// 对上一层使用过的单词进行清理
for(string item : used) words.erase(item);
used.clear();
if(path.size() > min_level)
break;
else
level = path.size();
}
// 根据最后一个单词向后扩展
string last = path.back();
for(int i = 0; i < last.size(); i++)
{
string new_s = last;
for(char j = 'a'; j <= 'z'; j++)
{
new_s[i] = j;
// 是路径中的单词
if(words.find(new_s) != words.end())
{
vector<string> new_path = path;
new_path.push_back(new_s);
used.push_back(new_s);
if(new_s == endWord)
{
min_level = level;
res.push_back(new_path);
}
else
paths.push(new_path);
}
}
}
}
return res.size() == 0 ? 0 : res[0].size();
}
};