Word Ladder II 相邻字符串接龙 BFS

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.
/*2015.9.1cyq*/
#include <iostream>
#include <vector>
#include <string>
#include <unordered_set>
#include <unordered_map>
using namespace std;

class Solution {
public:
    vector<vector<string>> findLadders(string start, string end, unordered_set<string> &dict) {
        vector<vector<string>> result;
		unordered_set<string> cur,next;//当前层和下一层
		unordered_set<string> visited;
		unordered_map<string,vector<string>> preNode;
		bool found=false;

		cur.insert(start);
		while(!cur.empty()&&!found){
			for(auto it=cur.begin();it!=cur.end();it++)
				visited.insert(*it);//本层结点全设置为访问过
			for(auto it=cur.begin();it!=cur.end();it++){
				vector<string> newWords=nextWords(*it,end,dict,visited);
				for(auto ik=newWords.begin();ik!=newWords.end();++ik){
					if(*ik==end) found=true;
					next.insert(*ik);//通过多种路径到达新结点时会自动去重
					preNode[*ik].push_back(*it);//记录前驱结点
				}
			}
			cur.clear();
			swap(cur,next);
		}
		if(found){
			vector<string> path;
			dfs(preNode,start,end,path,result);
		}
		return result;
    }
private:
	//利用前驱map,从end向start,dfs出所有路径
	void dfs(unordered_map<string,vector<string>> &preNode,string &start,string &word,
			vector<string> &path,vector<vector<string>> &result){
		path.push_back(word);
		if(word==start){
			result.push_back(path);
			reverse(result.back().begin(),result.back().end());
		}else{
			for(auto it=preNode[word].begin();it!=preNode[word].end();++it)
				dfs(preNode,start,*it,path,result);
		}
		path.pop_back();
	}
	//获取与s相差一个字符的字符串,要求该字符串未被访问过
	//注意找到新字符串后,先不标记为访问过,因为可能通过多种路径找到新字符串,所有路径都要输出
	vector<string> nextWords(const string &s,const string &target,unordered_set<string> &dict,
		unordered_set<string> &visited){

		vector<string> result;
		for(int i=0;i<s.size();i++){
			string tmp(s);
			for(char c='a';c<='z';c++){
				if(tmp[i]==c) continue;
				swap(c,tmp[i]);
				if((dict.find(tmp)!=dict.end()||tmp==target)&&visited.find(tmp)==visited.end())
					result.push_back(tmp);
				swap(c,tmp[i]);
			}
		}
		return result;
	}
};

int main(){
	string start="hit";
	string end="cog";
	string a[]={"hot","dot","dog","lot","log"};
	int n=sizeof(a)/sizeof(string);
	unordered_set<string> dict;
	for(int i=0;i<n;i++)
		dict.insert(a[i]);

	Solution solu;
	vector<vector<string>> ret=solu.findLadders(start,end,dict);
	for(auto it=ret.begin();it!=ret.end();++it){
		for(auto ik=(*it).begin();ik!=(*it).end();++ik)
			cout<<*ik<<" ";
		cout<<endl;
	}
	return 0;
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值