給定兩個單詞(beginWord 和 endWord)和一個字典 wordList,找出所有從 beginWord 到 endWord 的最短轉換序列。轉換需遵循如下規則:
每次轉換隻能改變一個字母。
轉換過程中的中間單詞必須是字典中的單詞。
說明:
如果不存在這樣的轉換序列,返回一個空列表。
所有單詞具有相同的長度。
所有單詞只由小寫字母組成。
字典中不存在重複的單詞。
你可以假設 beginWord 和 endWord 是非空的,且二者不相同。
示例 1:
輸入:
beginWord = "hit",
endWord = "cog",
wordList = ["hot","dot","dog","lot","log","cog"]
輸出:
[
["hit","hot","dot","dog","cog"],
["hit","hot","lot","log","cog"]
]
示例 2:
輸入:
beginWord = "hit"
endWord = "cog"
wordList = ["hot","dot","dog","lot","log"]
輸出: []
解釋: endWord "cog" 不在字典中,所以不存在符合要求的轉換序列。
來源:力扣(LeetCode)
鏈接:https://leetcode-cn.com/problems/word-ladder-ii
著作權歸領釦網絡所有。商業轉載請聯繫官方授權,非商業轉載請註明出處。
基本思想
這道題的關鍵是,看出問題的本質,求無向圖中兩個點的最短路徑,將能夠轉化的兩個單詞連線,構成一個無向圖。
當初直接進行的回溯,超時。
下面構造圖進行dfs: 超時 19/39
class Solution {
private:
vector<vector<string>> res;
unordered_map<string, vector<string>> edge;
public:
vector<vector<string>> findLadders(string beginWord, string endWord, vector<string>& wordList) {
vector<string> cur;
unordered_map<string, int> visit;
cur.push_back(beginWord);
for(int i = 0; i < wordList.size(); ++i){
for(int j = i + 1; j < wordList.size(); ++j){
if(diffNum(wordList[i], wordList[j])){
edge[wordList[i]].push_back(wordList[j]);
edge[wordList[j]].push_back(wordList[i]);
}
}
}
if(edge.count(beginWord) == 0){//特別注意:起始點有可能在單詞表內
for(int i = 0; i < wordList.size(); ++i){
if(diffNum(wordList[i], beginWord)){
edge[wordList[i]].push_back(beginWord);
edge[beginWord].push_back(wordList[i]);
}
}
}
dfs(cur, wordList, visit, beginWord, endWord);
return res;
}
void dfs(vector<string> cur, vector<string>& wordList, unordered_map<string, int> visit, string curW, string endW){
if(curW == endW){
// for(auto c : cur)
// cout << c << " " ;
// cout << endl;
if(res.empty() || cur.size() < res[0].size()){
res.clear();
res.push_back(cur);
}
else if(!res.empty() && res[0].size() == cur.size())
res.push_back(cur);
return;
}
visit[curW] = 1;
for(int i = 0; i < edge[curW].size(); ++i){
if(visit[edge[curW][i]] == 0){
//visit[edge[curW][i]] = 1;
cur.push_back(edge[curW][i]);
dfs(cur, wordList, visit, edge[curW][i], endW);
//visit[edge[curW][i]] = 0;
cur.pop_back();
}
}
visit[curW] = 0;
}
bool diffNum(string& word1, string& word2){
int res = 0;
for(int i = 0; i < word1.size() && res < 2; ++i){
if(word1[i] != word2[i]){
++res;
}
}
return res == 1;
}
};
bfs求兩點間最短路徑
這裏維護一個數組,用來統計從源節點到該節點的路徑長度,因爲最後可能有多條最短路徑。
隊列中保存的是從源節點到當前節點的路徑
class Solution {
private:
vector<vector<string>> res;
unordered_map<string, vector<string>> edge;
unordered_map<string,int> dis;
public:
vector<vector<string>> findLadders(string beginWord, string endWord, vector<string>& wordList) {
for(int i = 0; i < wordList.size(); ++i){
for(int j = i + 1; j < wordList.size(); ++j){
if(diffNum(wordList[i], wordList[j])){
edge[wordList[i]].push_back(wordList[j]);
edge[wordList[j]].push_back(wordList[i]);
}
}
dis[wordList[i]] = 0x3f3f3f3f;
}
if(edge.count(beginWord) == 0){//特別注意:起始點有可能在單詞表內
for(int i = 0; i < wordList.size(); ++i){
if(diffNum(wordList[i], beginWord)){
edge[wordList[i]].push_back(beginWord);
edge[beginWord].push_back(wordList[i]);
}
}
}
bfs(beginWord, endWord);
return res;
}
void bfs(string curW, string endW){
queue<vector<string>> q;
q.push({curW});
dis[curW] = 0;
while(!q.empty()){
int n = q.size();
while(n--){
auto temp = q.front();
q.pop();
string backs = temp.back();
if(backs == endW){
res.push_back(temp);
continue;
}
for(int i = 0; i < edge[backs].size(); ++i){
if(dis[backs] + 1 <= dis[edge[backs][i]]){//特別注意這裏
dis[edge[backs][i]] = dis[backs] + 1;
auto copy(temp);
copy.push_back(edge[backs][i]);
q.push(copy);
}
}
}
if(res.size())
break;
}
}
bool diffNum(string& word1, string& word2){
int res = 0;
for(int i = 0; i < word1.size() && res < 2; ++i){
if(word1[i] != word2[i]){
++res;
}
}
return res == 1;
}
};