Chinaunix首页 | 论坛 | 博客
  • 博客访问: 488086
  • 博文数量: 25
  • 博客积分: 111
  • 博客等级: 民兵
  • 技术积分: 1279
  • 用 户 组: 普通用户
  • 注册时间: 2012-10-26 20:51
文章分类

全部博文(25)

文章存档

2014年(17)

2013年(8)

分类: C/C++

2013-07-25 14:20:05

最近在网上看到了一个关于最小操作数的问题,感觉挺有意思的,就做了一下。

    题目大概是这样的:字符数相同的一些列单词,例如“hit”、“hot”、“dot”、“dog”、“lot”、“log”、“cog”,从任意一个单词开始(比如hit),每次只能经过一次变换(只改变一个单词及,例如hit到hot),并最终达到目的单词(例如cog),使此过程中经历的变换次数最少,如果存在多条最短变换,则同时输出。

    看到这个问题时,脑袋里立即想到的就是图的搜索算法,深度优先和广度优先,但是我们知道深度优先和广度优先算法都是比较盲目的算,不具有智能想,也就是启发性。于是,自然想到了A Star算法,但标准的A Star算法不能符合要求,需要进行些扩展。下面先简要的介绍A Star算法。

    A Star算法是一种启发式算法,其效果相当于Dijkstra算法(找到最佳路径),在简单的情况下,其速度相当于BFS算法,它具有Dijkstra算法的准确性,也具有BFS的效率,它是两者的结合。这是因为,Dijkstra算法是按着靠近初始点的结点进行贪婪搜索的(设到初始点的距离为f)这样会产生大量的盲目搜索,而BFS 算法是按着靠近目标点结点进行贪婪搜索的(设到目的节点的距离里为g)这样在遇到障碍物时不能及早的躲避,A Star算法是将两者结合在了一起(h=f+g)按着到初始点和目的地之和最短进行搜索,也即按着h值最小进行路径选择的。在这个过程中,一个重要的概念就是距离的定义,例如在游戏中,为NPC的移动选择最短路径时,我们可以按着欧氏距离也可以按着曼哈顿距离(横纵坐标差的绝对值之和),而在上面单词问题中,我选择的是相同位置处的单词是否相同,不同时距离加一(如hit和hot的距离为1,hot与dog的距离为2)。

    上面就是A Star的主要内容啦,下面开始介绍我是如何利用A Star算法去解决最小操作数问题的。首先按着上面所说的方法及距离的定义我们可以很容易的找出一条最短的匹配路径,现在的问题是如果存在不止一条匹配路径时,该如何去做呢?为了解决这个问题,我为每个节点加上了一个指向其“父节点”的指针(例如节点A是节点B的父节点,也就是在进行搜索时,如果现在搜到了A那么下一个所选择的节点为B)这里可能不同节点存在同一个父节点,即多对一的关系。这样我们就可以从目的节点的父节点回溯出整条路径啦。下面给出主要代码

点击(此处)折叠或打开

  1. class node_property{
  2. public:
  3.     double f;
  4.     double g;
  5.     double h;
  6. public:
  7.     node_property(const double v_f,const double v_g,const double v_h):f(v_f),g(v_g),h(v_h){}
  8.     node_property():f(0),g(0),h(0){}
  9. };

  10. class node{
  11. public:
  12.     node():parent(NULL){}
  13.     node(const std::string &str,node *p=NULL):node_value(str),parent(p){}
  14. public:
  15.     friend class a_star;
  16. public:
  17.     void set_node_property(const node_property& p){
  18.         memcpy(&property,&p,sizeof(node_property));
  19.     }
  20.     void set_node_property(const double v_f,const double v_g,const double v_h){
  21.         node_property n(v_f,v_g,v_h);
  22.         set_node_property(n);
  23.     }
  24.     void set_node(const node& n){
  25.         node_value = n.node_value;
  26.         parent = n.parent;
  27.         set_node_property(n.property);
  28.     }
  29. public:
  30.         friend size_t calc_distance(const node& lhs,const node& rhs){
  31.             typedef std::string::const_iterator c_iter_type;

  32.             size_t rst = 0;
  33.             c_iter_type lhs_it_b = lhs.node_value.begin();
  34.             c_iter_type lhs_it_e = lhs.node_value.end();
  35.             c_iter_type rhs_it_b = rhs.node_value.begin();
  36.             c_iter_type rhs_it_e = rhs.node_value.end();

  37.             for(;lhs_it_b != lhs_it_e && rhs_it_b != rhs_it_e;)
  38.                 if(*lhs_it_b++ != *rhs_it_b++)
  39.                     ++rst;
  40.             return rst;
  41.         }

  42.         node& operator = (const node& rhs){
  43.             if(this != &rhs)
  44.                 set_node(rhs);
  45.             return *this;
  46.         }

  47.         bool operator == (const node& n){
  48.             return node_value == n.node_value;
  49.         }

  50.         bool operator != (const node& n){
  51.             return ! this->operator==(n);
  52.         }

  53.         bool operator < (const node &rhs){
  54.             return property.h< rhs.property.h;
  55.         }
  56. private:
  57.     node_property property;
  58.     std::string node_value;
  59.     node *parent;
  60. };

  61. class a_star{
  62. public:
  63.     a_star(const std::list<std::string>&dic,const std::string src,const std::string dstn){
  64.         source.node_value = src;
  65.         destination.node_value = dstn;
  66.         fomat_dictionary(dic);
  67.     };
  68.     ~a_star(){
  69.         destroy(dictionary);
  70.         destroy(open_list);
  71.         destroy(close_list);
  72.     }
  73. private:
  74.     void destroy(std::list<node>nlist){
  75.         nlist.clear();
  76.     }
  77. private:
  78.     void fomat_dictionary(const std::list<std::string>& str){
  79.         for(std::list<std::string>::const_iterator iter = str.begin();
  80.             iter != str.end();)
  81.             dictionary.push_back(node(*iter++));
  82.     };
  83. public:
  84.     //参数为返回的最小匹配集合,返回值为这样最小匹配集合有几个
  85.     size_t begin_a_star(std::list<std::list<std::string> > &rst){
  86.         rst.clear();
  87.         if(source == destination){
  88.             return 0;
  89.         }
  90.         open_list.push_front(source);
  91.         std::multimap<double,node>rst_vector;
  92.         node *cur_node = 0;
  93.         do{
  94.             open_list.sort();
  95.             close_list.push_front(open_list.front());
  96.             open_list.pop_front();
  97.             cur_node = &(close_list.front());
  98.             if(1 == calc_distance(close_list.front(),destination)){
  99.                 if(!rst_vector.empty())
  100.                     if(rst_vector.begin()->first < cur_node->property.h+1)
  101.                         break;
  102.                 rst_vector.insert(std::make_pair(
  103.                     cur_node->property.h+1,*cur_node));
  104.                 continue;
  105.             }
  106.             insert_open_list(*cur_node);
  107.         }while(!open_list.empty());
  108.         if(0 == rst_vector.size())
  109.             std::cout<<"不存在最小操作数"<<std::endl;
  110.         else{
  111.             std::multimap<double,node>::iterator begin = rst_vector.begin();
  112.             std::multimap<double,node>::iterator end = rst_vector.end();
  113.             std::list<std::string>tmp_list;
  114.             for(;begin != end;){
  115.                 node *tmp_node = &((begin++)->second);
  116.                 for(; NULL && * source;){
  117.                     tmp_list.push_front(tmp_node->node_value);
  118.                     tmp_node = tmp_node->parent;
  119.                 }
  120.                 tmp_list.push_front(source.node_value);
  121.                 tmp_list.push_back(destination.node_value);
  122.                 rst.push_back(tmp_list);
  123.                 tmp_list.clear();
  124.             }
  125.         }
  126.         return rst_vector.size();
  127.     }
  128. private:
  129.     void insert_open_list(const node& n){
  130.         std::list<node>::iterator traver = dictionary.begin();
  131.         std::list<node>::iterator end = dictionary.end();
  132.         for(;traver != end;){
  133.             if(1 == calc_distance(n,*traver)){
  134.                 double f = n.property.f+1;
  135.                 double g = calc_distance(*traver,destination);
  136.                 traver->set_node_property(f,g,f+g);
  137.                 traver->parent = const_cast<node*>(&n);
  138.                 open_list.push_front(*traver);
  139.                 traver = dictionary.erase(traver);
  140.             }
  141.             else
  142.                 ++traver;
  143.         }
  144.     }
  145. private:
  146.     node source,destination;
  147.     std::list<node>     open_list;
  148.     std::list<node>     close_list;
  149.     std::list<node> dictionary;
  150. };

  151. 参考文献:


阅读(6111) | 评论(0) | 转发(2) |
0

上一篇:没有了

下一篇:红黑树总结和两种实现的比较

给主人留下些什么吧!~~