http://oj.leetcode.com/problems/word-ladder/

图的最短路径问题,可以用最短路径算法,也可以深搜,也可以广搜。

深搜版本:

第一次写的时候,把sum和visited都自然的设置成了传引用,导致递归调用下去之后,再返回来,反而各种参数的值退不回来了。然后把sum和visited改成了传值,这样反而适应了本程序意图。可见,也不是什么时候都需要传引用的。具体在写程序的时候,需要传值还是传引用,要具体分析。传引用和传值的情况分别如下:

void DFS(string currentWord,string endWord,int &sum, unordered_set<string> &dict,map<string,bool>  &visited,int  &minDistance)
void DFS(string currentWord,string endWord,int &sum, unordered_set<string> &dict,map<string,bool>  &visited,int  &minDistance)
另外,还遇到了一个问题,在递归调用层次下去又上来后,对本层循环的,后续影响。所以又添加了两个变量,int tempSum;和map<string,bool> tempVisited;。给它们赋值成参数刚进来的样子,这样就摒除了同层循环间的相互影响。

深搜版本超时了,继续改……
#include <iostream>
#include <vector>
#include <string>
#include <unordered_set>
#include <map>
#include <hash_map>
#include <limits.h>
using namespace std; class Solution {
public: int calcDistance(string a,string b)
{
int sum = ;
for(int i = ;i<a.size();i++)
{
if(a[i]!=b[i])
sum++;
}
return sum;
} void DFS(string currentWord,string endWord,int sum, unordered_set<string> &dict,map<string,bool> visited,int &minDistance)
{
if(calcDistance(currentWord,endWord)==)
{
sum++;
if(minDistance> sum)
minDistance = sum;
return;
}
int tempSum;
map<string,bool> tempVisited;
for(unordered_set<string>::iterator iter = dict.begin();iter!=dict.end();iter++)
{
if(visited[*iter]==false && calcDistance(currentWord,*iter)==)
{
visited[*iter] = true;
tempSum = sum;
tempSum++;
tempVisited = visited;
DFS(*iter,endWord,tempSum,dict,visited,minDistance);
}
}
} int ladderLength(string start, string end, unordered_set<string> &dict) { string currentWord = start; int sum = ; map<string,bool> visited; for(unordered_set<string>::iterator iter = dict.begin();iter!=dict.end();iter++)
visited[*iter] = false; int minDistance = INT_MAX; DFS(currentWord,end,sum,dict,visited,minDistance); return minDistance;
}
}; int main()
{
Solution *mySolution = new Solution(); unordered_set<string> dict;
dict.insert("hot");
dict.insert("dot");
dict.insert("dog");
dict.insert("lot");
dict.insert("log");
string start = "hit";
string end = "cog"; cout<<mySolution->ladderLength(start,end,dict);
return ;
}

然后,写了一个广搜版本的,代码如下:

class Solution {

public:
class node{
public:
int distance;
string word;
bool visited;
public:
node()
{
distance = ;
word.clear();
visited = false;
}
}; int testDistance(string test,string end)
{
int distance = ;
for(int i = ;i<test.size();i++)
if(test[i]!= end[i])
distance++;
return distance;
} int ladderLength(string start, string end, unordered_set<string> &dict) { vector<node*> wordVector; unordered_set<string>::iterator itor=dict.begin(); for(int i = ;i<dict.size();i++)
{
node *myNode = new node();
myNode->word = *itor;
itor++;
wordVector.push_back(myNode);
} node *myNode = new node();
myNode->word = start; queue<node*> wordQueue;
wordQueue.push(myNode); node *topNode= new node(); while(!wordQueue.empty())
{
topNode = wordQueue.front();
wordQueue.pop();
if(testDistance(topNode->word,end) == )
{
return topNode->distance+;
}
else
{
node *pIndexNode = new node();
for(vector<node*>::iterator itor = wordVector.begin();itor!=wordVector.end();itor++)
{
pIndexNode = *itor;
if(pIndexNode->visited == false && testDistance(pIndexNode->word,topNode->word)==)
{
pIndexNode->visited = true;
pIndexNode->distance = topNode->distance+;
wordQueue.push(pIndexNode);
}
}
}
} }
};

在这个过程中,刚开始写的是

return topNode->distance++;就直接这样返回了,写程序过程中,细心不够,这样返回是distance本来的值,然后distance会++,但是那也没用了。

仍然没过。但是相比之下,广搜比深搜,在测试数据上有进步了。继续改进广搜版本,将testDistance函数改了,并且还是用了eraser函数,将已经加入过的元素删除了,代码如下:

#include <iostream>
#include <vector>
#include <string>
#include <unordered_set>
#include <queue>
using namespace std; class Solution {
public:
class node{
public:
int distance;
string word;
public:
node()
{
distance = ;
word.clear();
}
}; bool testDistance(string test,string end)
{
int distance = ;
for(int i = ;i<test.size();i++)
{
if(test[i]!= end[i])
distance++;
if(distance>=)
return false;
}
return true;
} int ladderLength(string start, string end, unordered_set<string> &dict) { vector<node*> wordVector; unordered_set<string>::iterator itor=dict.begin(); for(int i = ;i<dict.size();i++)
{
node *myNode = new node();
myNode->word = *itor;
itor++;
wordVector.push_back(myNode);
} node *myNode = new node();
myNode->word = start; queue<node*> wordQueue;
wordQueue.push(myNode); node *topNode= new node(); while(!wordQueue.empty())
{
topNode = wordQueue.front();
wordQueue.pop();
if(testDistance(topNode->word,end))
{
return topNode->distance+;
}
else
{
node *pIndexNode = new node();
for(vector<node*>::iterator itor = wordVector.begin();itor!=wordVector.end();)
{
pIndexNode = *itor;
if(testDistance(pIndexNode->word,topNode->word))
{
pIndexNode->distance = topNode->distance+;
wordQueue.push(pIndexNode);
itor = wordVector.erase(itor);
}
else
itor++;
}
}
} }
}; int main()
{
Solution *mySolution = new Solution(); unordered_set<string> dict;
dict.insert("hot");
dict.insert("dot");
dict.insert("dog");
dict.insert("lot");
dict.insert("log");
string start = "hit";
string end = "cog"; cout<<mySolution->ladderLength(start,end,dict);
return ;
}

虽然每一步都有学习到新东西,也学会了些深搜和广搜,但是这道题目仍然超时,现在考虑用空间换时间。

#include <iostream>
#include <vector>
#include <string>
#include <unordered_set>
#include <unordered_map>
#include <queue>
using namespace std; class Solution {
public:
bool testDistance(string test,string end)
{
int distance = ;
for(int i = ;i<test.size();i++)
{
if(test[i]!= end[i])
distance++;
if(distance>=)
return false;
}
return true;
} void BuildAdjacentList(string &word, unordered_map< string,unordered_set<string> >&adjacentList,const unordered_set<string> &dict)
{
string original = word;
for(size_t pos = ;pos<word.size();pos++)
{
char beforeChange = ' ';
for(int i = 'a';i<'z';++i)
{
beforeChange = word[pos];
if(beforeChange == i)
{
continue;
}
word[pos] = i;
if(dict.count(word)>)
{
auto iter = adjacentList.find(original);
if(iter!= adjacentList.end())
iter->second.insert(word);
else
{
adjacentList.insert(pair<string,unordered_set<string> >(original,unordered_set<string>()));
adjacentList[original].insert(word);
}
}
word[pos] = beforeChange;
}
}
} int ladderLength(string start, string end, unordered_set<string> &dict) { queue<pair<string,int> > wordQueue;
wordQueue.push(make_pair(start,)); unordered_map< string,unordered_set<string> > adjacentList; string topWord; int ans = ; unordered_set<string> visited;
visited.insert(start); while(!wordQueue.empty())
{
topWord = wordQueue.front().first; if(testDistance(topWord,end))
{
return wordQueue.front().second+;
}
else
{
BuildAdjacentList(topWord,adjacentList,dict); auto iter = adjacentList.find(topWord);
if(iter!= adjacentList.end())
{
for(unordered_set<string>::iterator iterset = iter->second.begin();iterset!= iter->second.end();iterset++)
{
if(visited.find(*iterset)==visited.end())
{
wordQueue.push(make_pair(*iterset,wordQueue.front().second+));
visited.insert(*iterset);
} }
} }
wordQueue.pop();
}
return ;
}
}; int main()
{
Solution *mySolution = new Solution(); unordered_set<string> dict;
dict.insert("hot");
//dict.insert("dot");
dict.insert("dog");
//dict.insert("lot");
//dict.insert("log");
string start = "hot";
string end = "dog"; cout<<mySolution->ladderLength(start,end,dict);
return ;
}

上面的这份代码,终于AC了,果然考察的重点在另一个思路上。其实,也用不着用空间来存储。比如可以使用更精简的代码,如下:

class Solution{
public:
int ladderLength(string start,string end,unordered_set<string> &dict)
{
queue<pair<string ,int > > wordQueue;
unordered_set<string> visited;
wordQueue.push(make_pair(start,));
visited.insert(start); while(!wordQueue.empty())
{
string curStr = wordQueue.front().first;
int curStep = wordQueue.front().second;
wordQueue.pop(); for(int i = ;i<curStr.size();i++)
{
string tmp = curStr;
for(int j = ;j<;++j)
{
tmp[i] = j+'a';
if(tmp == end)
return curStep+;
if(visited.find(tmp) == visited.end() && dict.find(tmp)!=dict.end())
{
wordQueue.push(make_pair(tmp,curStep+));
visited.insert(tmp);
}
}
}
}
return ; }
};

加油!

 其实,这道题的重点不在于深搜、广搜剪枝之类的。换个角度说的话,dict很大,多次遍历它的话,影响时间,然后换个思路……

LeetCode OJ——Word Ladder的更多相关文章

  1. Java for LeetCode 126 Word Ladder II 【HARD】

    Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) from ...

  2. [LeetCode] 126. Word Ladder II 词语阶梯 II

    Given two words (beginWord and endWord), and a dictionary's word list, find all shortest transformat ...

  3. [LeetCode] 127. Word Ladder 单词阶梯

    Given two words (beginWord and endWord), and a dictionary's word list, find the length of shortest t ...

  4. LeetCode 126. Word Ladder II 单词接龙 II(C++/Java)

    题目: Given two words (beginWord and endWord), and a dictionary's word list, find all shortest transfo ...

  5. [Leetcode Week5]Word Ladder II

    Word Ladder II 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/word-ladder-ii/description/ Descripti ...

  6. [Leetcode Week5]Word Ladder

    Word Ladder题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/word-ladder/description/ Description Give ...

  7. [LeetCode] 126. Word Ladder II 词语阶梯之二

    Given two words (beginWord and endWord), and a dictionary's word list, find all shortest transformat ...

  8. 【leetcode】Word Ladder

    Word Ladder Total Accepted: 24823 Total Submissions: 135014My Submissions Given two words (start and ...

  9. 【leetcode】Word Ladder II

      Word Ladder II Given two words (start and end), and a dictionary, find all shortest transformation ...

随机推荐

  1. 16Shell脚本—计划任务服务程序

    计划任务服务程序 尽管我们现在已近有了功能彪悍的脚本程序来执行一些批处理工作,但是,如果仍然需要每天凌晨两点敲击键盘回车键来执行这个脚本程序,这简直太痛苦了.为此,我们需要学习如何设置服务器的计划任务 ...

  2. Scrapy+Chromium+代理+selenium

    上周说到scrapy的基本入门.这周来写写其中遇到的代理和js渲染的坑. js渲染 js是爬虫中毕竟麻烦处理的一块.通常的解决办法是通过抓包,然后查看request信息,接着捕获ajax返回的消息.但 ...

  3. Kubernetes的主要功能

    Kubernetes的主要功能 1.数据卷   Pod中容器之间共享数据,可以使用数据卷. 2.应用程序健康检查   容器内服务可能进程堵塞无法处理请求,可以设置监控检查策略保证应用健壮性. 3.复制 ...

  4. Python基础(六)——面向对象编程

    (1)定义类和实例 这一部分难得和 Java 较为一致,直接写个例子: class Stu: def __init__(self, name, id): # 构造方法 self.name = name ...

  5. Html5_标签

    HTML 1.一套规则,浏览器认识的规则. 2.开发者: 学习Html规则 开发后台程序: - 写Html文件(充当模板的作用) ****** - 数据库获取数据,然后替换到html文件的指定位置(W ...

  6. LeetCode(279)Perfect Squares

    题目 Given a positive integer n, find the least number of perfect square numbers (for example, 1, 4, 9 ...

  7. Solution: 最近公共祖先·一 [hiho一下 第十三周]

    题目1 : 最近公共祖先·一 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 小Ho最近发现了一个神奇的网站!虽然还不够像58同城那样神奇,但这个网站仍然让小Ho乐在其中 ...

  8. Linux学习-分析登录档

    CentOS 预设提供的 logwatch 不过,要了解系统的状态,还是得要分析整个登录档才行~ 事实上, 目前已经有相当多的登录档分析工具,例如 CentOS 7.x 上面预设的 logwatch ...

  9. MySQL-状态Waiting on empty queue引申

    MySQL 事件调度器示例演示 我们大家都知道MySQL 事件调度器是在 MySQL 5.1 中新生的一个较为特殊的功能,其可以作为定时任务调度器,来取代部分原先只能用操作系统任务调度器才能完成的定时 ...

  10. Educational Codeforces Round 2 Edge coloring of bipartite graph

    题意: 输入一个二分图,用最少的颜色数给它的每条边染色,使得同一个顶点连的边中颜色互不相同. 输出至少需要的颜色数和任意一种染色方案. 分析: 证明不会,只说一下(偷瞄巨巨代码学到的)做法. 假设点的 ...