首页 > 搜索:广搜 词语阶梯

搜索:广搜 词语阶梯

问题描述以及解决过程如下导图

在这里插入图片描述

广搜实现如下

#include 
#include 
#include 
#include 
#include 
#include 
#include using namespace std;/*判断两个单词是否有连接状态*/
bool connect(string &word1, string &word2) { if (word1.length() == 0 || word2.length() == 0) { return false;}int cnt = 0;for (int i = 0; i < word1.length(); ++i) { if (word1[i] != word2[i]) { cnt ++;}}if (cnt == 1) { return true;}return false;
}/*构建图*/
void get_graph(string &begin_word, std::vector<string> &wordList,std::map<string, std::vector<string> > &graph) { wordList.push_back(begin_word);for (int i = 0;i < wordList.size(); ++i ) { graph[wordList[i]]= std::vector<string> ();}for (int i = 0;i < wordList.size(); ++i) { for (int j = i + 1; j < wordList.size(); ++j) { if (connect(wordList[i], wordList[j])) { graph[wordList[i]].push_back(wordList[j]);graph[wordList[j]].push_back(wordList[i]);}}}
}/*广搜获取最短长度*/
int get_length(string &begin_word, string &end_word,std::map<string, std::vector<string> > graph) { queue<pair<string,int>> Q;set<string> visit;Q.push(make_pair(begin_word,1));visit.insert(begin_word);while(!Q.empty()) { /* code */string node = Q.front().first;int step = Q.front().second;Q.pop();if (node == end_word) { return step;}std::vector<string> node_v = graph[node];for (int i = 0;i < node_v.size(); ++i) { if (visit.find(node_v[i]) == visit.end()) { Q.push(make_pair(node_v[i], step + 1));visit.insert(node_v[i]);}}	}return 0;
}int get_result(string &begin_word, string &end_word, std::vector<string> &wordList) { std::map<string, std::vector<string> > graph;get_graph(begin_word,wordList,graph);return get_length(begin_word,end_word,graph);
}int main(int argc, char const *argv[])
{ string begin_word = "hit";string end_word = "cog";std::vector<string> word_list;for (int i = 0;i < 6; ++i) { string tmp;cin >> tmp;word_list.push_back(tmp);}cout << get_result(begin_word, end_word, word_list);return 0;
}

输出如下:

#输入
hot 
dot
dog
log
lot
cog#输出
5

更多相关:

  • importjava.security.SecureRandom;importjavax.crypto.Cipher;importjavax.crypto.SecretKey;importjavax.crypto.SecretKeyFactory;importjavax.crypto.spec.DESKeySpec;//结果与DES算...

  • 题目:替换空格 请实现一个函数,把字符串 s 中的每个空格替换成"%20"。 输入:s = "We are happy." 输出:"We%20are%20happy." 限制: 0 <= s 的长度 <= 10000 解题: 时间复杂度:O(n) 空间复杂度:O(n) class Solution { public:s...

  • 在C++11标准库中,string.h已经添加了to_string方法,方便从其他类型(如整形)快速转换成字面值。 例如: for (size_t i = 0; i < texArrSize; i++)RTX_Shader.SetInt(string("TexArr[") + to_string(i) + "]", 7 + i);...

  • Ubuntu 14.04安装并升级之后,变成楷体字体非常难看,我昨天搞了一晚上,终于理了个头绪,这里整理一下。 经过网上调研,大家的一致看法是,使用开源字体库文泉驿的微黑字体效果比较理想,甚至效果不输windows平台的雅黑字体。下面我打算微黑来美化Ubuntu 14.04. 1.安装文泉驿微黑字体库 sudo aptitude...

  • 使用string时发现了一些坑。 我们知道stl 容器并不是线程安全的,所以在使用它们的过程中往往需要一些同步机制来保证并发场景下的同步更新。 应该踩的坑还是一个不拉的踩了进去,所以还是记录一下吧。 string作为一个容器,随着我们的append 或者 针对string的+ 操作都会让string内部的数据域动态增加,而动态增加的...

  • pip install pywin32from win32com.client import gencachefrom win32com.client import constants, gencachedef doctupdf(wordPath, pdfPath):"""word转pdf:param wordPath: word文件...

  • important_dic = {'预付卡'}weight = 3 #可以修改,也可以再字典里添加权值k=0for word in set(sent): if word in important_dic: k = weight else: k = 0 if wor...

  •         Apache POI是一个开源的利用Java读写Excel,WORD等微软OLE2组件文档的项目。        我的需求是对Excel的数据进行导入或将数据以Excel的形式导出。先上简单的测试代码:package com.xing.studyTest.poi;import java.io.FileInputSt...

  • 要取得[a,b)的随机整数,使用(rand() % (b-a))+ a; 要取得[a,b]的随机整数,使用(rand() % (b-a+1))+ a; 要取得(a,b]的随机整数,使用(rand() % (b-a))+ a + 1; 通用公式:a + rand() % n;其中的a是起始值,n是整数的范围。 要取得a到b之间的...

  • 利用本征图像分解(Intrinsic Image Decomposition)算法,将图像分解为shading(illumination) image 和 reflectance(albedo) image,计算图像的reflectance image。 Reflectance Image 是指在变化的光照条件下能够维持不变的图像部分...

  • 题目:面试题39. 数组中出现次数超过一半的数字 数组中有一个数字出现的次数超过数组长度的一半,请找出这个数字。 你可以假设数组是非空的,并且给定的数组总是存在多数元素。 示例 1: 输入: [1, 2, 3, 2, 2, 2, 5, 4, 2] 输出: 2 限制: 1 <= 数组长度 <= 50000 解题: cl...

  • 题目:二叉搜索树的后序遍历序列 输入一个整数数组,判断该数组是不是某二叉搜索树的后序遍历结果。如果是则返回 true,否则返回 false。假设输入的数组的任意两个数字都互不相同。 参考以下这颗二叉搜索树:      5     /    2   6   /  1   3示例 1: 输入: [1,6,3,2,5] 输出...