-
Notifications
You must be signed in to change notification settings - Fork 1
/
test.cpp
44 lines (44 loc) · 1.05 KB
/
test.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
class Solution {
public:
vector<vector<string>> findLadders(string &start, string &end,
unordered_set<string> &dict) {
unordered_map<string, unordered_set<string>> m;
unordered_set<string> s1 { start }, s2, *cur = &s1, *next = &s2;
dict.insert(end);
for (bool flag = 1; flag;) {
for (auto c : *cur) {
for (const auto& d : dict) {
auto diff = 0;
for (auto i = 0u; i < d.length(); i++)
if (d[i] != c[i] && ++diff > 1)
break;
if (diff == 1) {
m[d].insert(c);
next->insert(d);
if (d == end)
flag = 0;
}
}
}
if (flag = flag && !next->empty()) {
swap(cur, next);
next->clear();
for (auto c : *cur)
dict.erase(c);
}
}
vector<vector<string>> result;
function<vector<vector<string>>(string)> dfs;
dfs = [&dfs,&m,&start](string s)->vector<vector<string>> {
vector<vector<string>> ans;
if(s==start)return { {start}};
for(auto i:m[s])
for(auto d:dfs(i)) {
ans.push_back(d);
ans.back().push_back(s);
}
return ans;
};
return dfs(end);
}
};