博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Word Ladder II
阅读量:4314 次
发布时间:2019-06-06

本文共 3165 字,大约阅读时间需要 10 分钟。

Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) from start to end, such that:

  1. Only one letter can be changed at a time
  2. Each intermediate word must exist in the dictionary

For example,

Given:

start = "hit"
end = "cog"
dict = ["hot","dot","dog","lot","log"]

Return

[    ["hit","hot","dot","dog","cog"],    ["hit","hot","lot","log","cog"]  ]

Note:

  • All words have the same length.
  • All words contain only lowercase alphabetic characters.

思路:先一遍BFS获取最小距离,然后DFS打印所有符合条件的路径。注意,visit数组的置位问题,在pop当前元素之后,要把visit置为可访问。

       可惜,目前的版本在 Judge Large上没有AC !!!

class Solution {public:    int min_path;    vector
> all_path; unordered_set
m_visit; void dfs(string start, string end, unordered_set
&dict, vector
& path){ if (start == end && path.size() == min_path){ all_path.push_back(path); return; } if (path.size() >= min_path){ //当前的方向已经不对,不可能得到end return; } m_visit.insert(start); //这里visit会导致一些结点作为中间结点被访问过之后,其他路径再无法利用 // red->ted->tex->tax red->ted->tad->tax red->rex->tex->tax //pop 栈的时候,到了rex这里,这时候要访问tex到达tax但是发现tex已经被visit了 //不能再访问,所以就缺少了一条路劲 string current = start; for(size_t i = 0; i < current.size(); i++){ string ex = current; for(int j = 'a'; j <= 'z'; j++){ if (ex[i] == j){ continue; } ex[i] = j; if (dict.find(ex) != dict.end() && m_visit.find(ex) == m_visit.end()){ path.push_back(ex); dfs(ex,end,dict,path); path.resize(path.size() -1); //可否加在这里呢?pop栈之后,把ex置为可访问,极端情况下end肯定需要置为可访问 m_visit.erase(ex); //因为i++,所以并不会死循环 } } } } int bfs(string start, string end, unordered_set
&dict){ if (start == end){ return 0; } m_visit.insert(start); queue
> qu; qu.push(make_pair(start,1)); while(!qu.empty()){ pair
current = qu.front(); qu.pop(); for(size_t i = 0; i < current.first.size(); i++){ string ex = current.first; for(int j = 'a'; j <= 'z'; j++){ if (ex[i] == j){ continue; } ex[i] = j; if (dict.find(ex) != dict.end() && m_visit.find(ex) == m_visit.end()){ if (ex == end){ //have found return current.second + 1; } m_visit.insert(ex); qu.push(make_pair(ex,current.second + 1)); } } } } if (dict.find(start) != dict.end() && dict.find(end) != dict.end()){ return 0; } return -1; } vector
> findLadders(string start, string end, unordered_set
&dict) { // Start typing your C/C++ solution below // DO NOT write int main() function vector
path; all_path.clear(); m_visit.clear(); //bfs find shortest path min_path = bfs(start,end,dict); if (min_path == 0 || min_path == -1){ return all_path; } m_visit.clear(); path.push_back(start); //dfs output all path dfs(start,end,dict,path); return all_path; } };

 

 

转载于:https://www.cnblogs.com/kwill/archive/2013/06/13/3134110.html

你可能感兴趣的文章
手机自带功能调用
查看>>
百度搜索引擎取真实地址-python代码
查看>>
java 多线程 Future callable
查看>>
字符串操作练习:星座、凯撒密码、99乘法表
查看>>
Java实现字符串转换十六进制MD5值
查看>>
MySQL数据库8(十七)数据库的备份还原
查看>>
tensorflow 梯度下降以及summary
查看>>
9、接口和抽象类
查看>>
timeStamp和GMT时间的转换
查看>>
探索J2ME应用:如何用GCF通信
查看>>
jquery ajaxform上传文件返回不提示信息的问题
查看>>
实现一个2008serve的IIS的虚拟目录(通过网络路径(UNC)的形式,共享在另外一个2008服务器上...
查看>>
适配器
查看>>
c#截取字符串
查看>>
VS2005中配置 ScriptManager,UpdatePanel,UpdateProgress 等AJAX控件 .
查看>>
使用logback实现http请求日志导入mongodb
查看>>
【 2017 Multi-University Training Contest - Team 9 && hdu 6162】Ch’s gift
查看>>
redis在php中的应用(Hash篇)
查看>>
Docker系列之Docker镜像(读书笔记)
查看>>
Scrapy 多url爬取、爬取post请求、更换代理ip、指定日志等级
查看>>