8皇后问题的c++与python实现对比

21 篇文章 0 订阅
21 篇文章 0 订阅
    c++经典书多,但貌似不太新,而python则新书很多,能体现一些新思路新概念。看python的书,写python代码,再用c++实现一遍,这样互补的学习方式貌似也蛮适合自己的。
    在《Beginning Python From Novice to Professional》一书的八皇后问题上,python果然精妙而优雅。
   在对待
  1. for each possibility at level 1:
  2.   for each possibility at level 2:
  3.     for each possibility at level n:
  4.     ... 
  5.     is it viable
形式的循环下,它有yield, generator递归。短短20来行代码就搞定了八皇后,如下:
  1. def conflict(state,nextX):
  2.     nextY=len(state)
  3.     if nextY==0:print 0
  4.     for i in range(nextY):
  5.         if abs(nextX-state[i]) in (0,nextY-i):
  6.             return True
  7.     return False
  8. def queen(num,state=()):
  9.     print state
  10.     for pos in range(num):
  11.         if not conflict(state,pos):
  12.             if len(state) == num-1:
  13.                 yield (pos,)
  14.             else:
  15.                 for result in queen(num,state+(pos,)):
  16.                     yield  (pos,)+result 
  17. def prettyprint(solution):
  18.     def line(pos,length=len(solution)):
  19.         return '. '*pos + 'X' +'. '*(length-pos-1)
  20.     for pos in solution:
  21.         print line(pos)
  22. if __name__=='__main__':
  23.     import random
  24.     prettyprint(random.choice(list(queen(4))))
其中
  1. for result in queen(num,state+(pos,)):
  2.                     yield  (pos,)+result 
2句由为精妙,自己苦思很久,没有能完全理解yield的精髓。。。。。

在用c++实现的时候,一直想模仿实现这句话的功能,却被这句话困扰了2个多小时,看书看久了不休息果然效率奇差。
今天早上醒来,才恍然大悟,其实不就是个栈么。
  1. /*
  2.  * =====================================================================================
  3.  *
  4.  *       Filename:  eightqueen.cpp
  5.  *
  6.  *    Description:  8皇后问题,c++实现
  7.  *
  8.  *        Version:  1.0
  9.  *        Created:  2008年12月30日 19时46分52秒
  10.  *       Revision:  none
  11.  *       Compiler:  gcc
  12.  *
  13.  *         Author:  Li WeiJian (mn), lwj1396@163.com
  14.  *        Company:  hunan university
  15.  *
  16.  * =====================================================================================
  17.  */
  18. #include<vector>
  19. #include<iostream>
  20. #include<cmath>
  21. using namespace std;
  22. bool conflict(const vector<int> &state,int nextX)
  23. {
  24.     int nextY = state.size();
  25.     for(int i=0;i<nextY;i++)
  26.     {
  27.         if(abs(nextX-state[i])==0 || abs(nextX-state[i])==(nextY-i))
  28.             return true;
  29.     }
  30.     return false;
  31. }
  32. void queen(int num,vector<int> &state)
  33. {
  34.     for(int i=0;i<num;i++)
  35.     {
  36.         state.clear();
  37.         state.push_back(i);
  38.         int pos=0;
  39.         while(state.size()!=num)
  40.         {
  41.             if(conflict(state,pos))//冲突了
  42.             {
  43.                 if(pos==num-1)//已经是最后一个位置
  44.                 {
  45.                     state.pop_back();
  46.                     break;
  47.                 }
  48.                 pos++;//尝试下一个pos
  49.                 continue;
  50.             }
  51.             else//没有冲突
  52.             {
  53.                 state.push_back(pos);
  54.                 pos=0;
  55.             }
  56.         }
  57.         if(state.size()==num)
  58.             return;
  59.     }
  60. }
  61. void print(int num,const vector<int> result)
  62. {
  63.     for(int i=0;i<result.size();i++)
  64.     {
  65.         for(int j=0;j<result[i];j++)
  66.             cout<<". ";
  67.         cout<<"X ";
  68.         for(int j=0;j<num-result[i]-1;j++)
  69.             cout<<". ";
  70.         cout<<endl;
  71.     }
  72. }
  73. int main()
  74. {
  75.     vector<int> state;
  76.     queen(16,state);
  77.     print(16,state);
  78. }

在实现之后,对比了一下,效率的差距果然满大,python跑14皇后,等了几分钟没搞定,而c++跑16皇后,都是刷的一下出来了,不过可能c++没有用递归也是一个原因。

最后再总结2句:
 1 一本书,无论薄厚,都不能欺负它,不要想1,2天就看完
 2 c++虽然经典,但书籍过老了,要拿一门有活力的语言为它开路,其实boost的c++已经很python了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值