Chinaunix首页 | 论坛 | 博客
  • 博客访问: 121160
  • 博文数量: 53
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 620
  • 用 户 组: 普通用户
  • 注册时间: 2014-08-24 16:22
文章存档

2014年(53)

我的朋友

分类: C/C++

2014-09-22 18:48:33

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.

这道题是要求:给一个起点S和终点E,输出所有最短路径。
BFS时,当你给每个节点加了前驱,你也无法输出所有最短路径,因为有这样的节点存在:

在上图中,B有两个前驱节点A和C,因此存储前驱时,必然存在其中一个覆盖掉另一个的情况。
所以我们需要存储所有的前驱,这里有几个情况需要考虑:
1. 第k+1层需要避免把第k层的节点再添加到队列中
2. 当一个节点A需要把和它相邻的另一个节点B加入队列时,需要"前驱为A的路径上B的深度值<=已经被加入队列的B的深度值“, 这是因为BFS总是先发现最短路径,所以先加入队列的B的深度值一定是最小深度值,但是有可能有同样是最小深度值,但是不同的前驱需要存储。可以看上面那个图帮助理解。
这里面还有个小trick:当需要vector存储两个以上的item时,写一个struct帮助存储。
code如下,但是。。。TLS!!!

  1. public:
  2.     struct S{
  3.         string me;
  4.         int pre;
  5.         int depth;
  6.         S(string& _me,int _pre,int _depth ){
  7.             me=_me;
  8.             pre=_pre;
  9.             depth=_depth;
  10.         }
  11.     };
  12.     int lookslike(string s1, string s2){
  13.         int diff = 0;
  14.         for (int i = 0; i < s1.length(); i++) {
  15.             if (s1[i] != s2[i]) {
  16.                 diff++;
  17.                 if (diff >= 2)
  18.                     return 2;
  19.             }
  20.         }
  21.         return diff;
  22.     }
  23.     vector<vector<string>> findLadders(string start, string end, unordered_set<string> &dict) {
  24.         vector<vector<string>> result;
  25.         if(0==lookslike(start,end)){
  26.                 return result;
  27.         }
  28.         if(1==lookslike(start,end)){
  29.                 vector<string> str;
  30.                 str.push_back(start);
  31.                 str.push_back(end);
  32.                 result.push_back(str);
  33.                 return result;
  34.         }
  35.         dict.erase(start);
  36.         dict.erase(end);
  37.         deque<S> Q;
  38.         vector<S> V;
  39.         unordered_map<string,bool> map;
  40.         unordered_map<string,int> map2;
  41.         Q.push_back(S(start,-1,1));
  42.         map2.insert({start,1});
  43.         bool flag=false;
  44.         while(!Q.empty()){
  45.             S a = Q.front();
  46.             Q.pop_front();
  47.             map2.erase(a.me);
  48.             if(flag==false){
  49.                 V.push_back(a);
  50.                 map.insert({a.me,true});
  51.             }
  52.             string me=a.me;
  53.             int pre=a.pre;
  54.             int depth=a.depth;
  55.             if(1==lookslike(me,end)){
  56.                 //cout<<Q.size()<<" "<<V.size()<<endl;
  57.                 flag=true;
  58.                 vector<string> str;
  59.                 str.push_back(end);
  60.                 str.push_back(me);
  61.                 int x=pre;
  62.                 while(x!=-1){
  63.                     str.push_back(V[x].me);
  64.                     x=V[x].pre;
  65.                 }
  66.                 reverse(str.begin(),str.end());
  67.                 result.push_back(str);
  68.                 continue;
  69.             }
  70.             else if(flag==false){
  71.                 string tmp=me;
  72.                 for(int i=0;i<tmp.length();i++){
  73.                     for(char c='a';c<='z';c++){
  74.                         tmp.replace(i,1,1,c);
  75.                         if(dict.find(tmp)!=dict.end()){
  76.                             if(map.find(tmp)==map.end()){
  77.                                     //if(depth+1<=getdepth(Q,tmp))
  78.                                     if((map2.find(tmp)!=map2.end() && depth+1<=map2[tmp]) || map2.find(tmp)==map2.end()){
  79.                                         Q.push_back(S(tmp,V.size()-1,depth+1));
  80.                                         map2.insert({tmp,depth+1});
  81.                                     }
  82.                             }
  83.                         }
  84.                     }
  85.                     tmp=me;
  86.                 }
  87.             }
  88.         }
  89.         return result;
  90.     }


阅读(1017) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~