class Solution {
public:
int ladderLength(string beginWord, string endWord, vector<string>& wordList) {
//优化,双向BFS
//优先遍历两个队列中size较小的
queue<string> open1;
queue<string> open2;
unordered_set<string> close1;
unordered_set<string> close2;
unordered_set<string> worddit(wordList.begin(),wordList.end());
//这里的集合用于更改状态
int depth1=0;
if(!worddit.count(endWord)){
return 0;
}
//双向BFS需要判断起点是否为终点
int test=0;
for(int i=0;i<beginWord.size();i++){
if(beginWord[i]!=endWord[i]){
test++;
}
}
if(test==1){
return 2;
}
open1.push(beginWord);
open2.push(endWord);
string cur;
while(!open1.empty()&&!open2.empty()){
if(open1.size()>open2.size()){
queue<string> temp1=open1;
open1=open2;
open2=temp1;
unordered_set<string> temp2=close1;
close1=close2;
close2=temp2;
}
int n1=open1.size();
//这里必须要逐层遍历,不然深度的增加会出错
while(n1--){
cur=open1.front();
open1.pop();
//改用 逐个字母修改
if(close2.count(cur)){
return depth1;
}
if(close1.count(cur)){
continue;
}
close1.insert(cur);
for(auto& cbyte:cur){
for(char i='a';i<='z';i++){
char temp=cbyte;
cbyte=i;
if(worddit.count(cur)&&temp!=i){
open1.push(cur);
}
cbyte=temp;
}
}
}
depth1++;
}
return 0;
// //双向BFS
// queue<string> open1;
// queue<string> open2;
// unordered_set<string> close1;
// unordered_set<string> close2;
// unordered_set<string> worddit(wordList.begin(),wordList.end());
// //也可以用close表,这里用集合代替,访问过的单词会被擦除
// int depth1=0;
// int depth2=0;
// if(!worddit.count(endWord)){
// return 0;
// }
// //双向BFS需要判断起点是否为终点
// int test=0;
// for(int i=0;i<beginWord.size();i++){
// if(beginWord[i]!=endWord[i]){
// test++;
// }
// }
// if(test==1){
// return 2;
// }
// open1.push(beginWord);
// open2.push(endWord);
// string cur;
// while(!open1.empty()&&!open2.empty()){
// int n1=open1.size();
// //这里必须要逐层遍历,不然深度的增加会出错
// while(n1--){
// cur=open1.front();
// open1.pop();
// //改用 逐个字母修改
// if(close2.count(cur)){
// return depth1+depth2;
// }
// if(close1.count(cur)){
// continue;
// }
// close1.insert(cur);
// for(auto& cbyte:cur){
// for(char i='a';i<='z';i++){
// char temp=cbyte;
// cbyte=i;
// if(worddit.count(cur)&&temp!=i){
// if(close1.count(cur)){
// //排除无效的状态
// //这里不排除会出错
// cbyte=temp;
// continue;
// }
// open1.push(cur);
// }
// cbyte=temp;
// }
// }
// }
// depth1++;
// int n2=open2.size();
// while(n2--){
// cur=open2.front();
// open2.pop();
// if(close1.count(cur)){
// return depth1+depth2;
// }
// if(close2.count(cur)){
// continue;
// }
// close2.insert(cur);
// //改用 逐个字母修改
// for(auto& cbyte:cur){
// for(char i='a';i<='z';i++){
// char temp=cbyte;
// cbyte=i;
// if(worddit.count(cur)&&temp!=i){
// if(close2.count(cur)){
// //排除无效的状态
// //这里不排除会出错
// cbyte=temp;
// continue;
// }
// open2.push(cur);
// }
// cbyte=temp;
// }
// }
// }
// depth2++;
// }
// return 0;
// bfs
// queue<string> open;
// unordered_set<string> worddit(wordList.begin(),wordList.end());
// //也可以用close表,这里用集合代替,访问过的单词会被擦除
// int ans=1;
// if(!worddit.count(endWord)){
// return 0;
// }
// open.push(beginWord);
// string cur;
// while(!open.empty()){
// int n=open.size();
// //这里必须要逐层遍历,不然深度的增加会出错
// while(n--){
// cur=open.front();
// open.pop();
// if(cur==endWord){
// return ans;
// }
// //此方法超时
// // for(auto word:wordList){
// // int p=0;
// // for(int i=0;i<word.size();i++){
// // if(word[i]!=cur[i]){
// // p++;
// // }
// // }
// // if(p==1&&worddit.count(word)){
// // open.push(word);
// // worddit.erase(word);
// // }
// // }
// //改用 逐个字母修改
// for(auto& cbyte:cur){
// for(char i='a';i<='z';i++){
// char temp=cbyte;
// cbyte=i;
// if(worddit.count(cur)&&temp!=i){
// open.push(cur);
// worddit.erase(cur);
// }
// cbyte=temp;
// }
// }
// }
// ans++;
// }
// return 0;
}
};