`
zlzyfpqianhao9951078
  • 浏览: 13552 次
最近访客 更多访客>>
文章分类
社区版块
存档分类
最新评论

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

 
阅读更多

详细请点击:http://www.verydemo.com/demo_c92_i2205.html

c++经典书多,但貌似不太新,而python则新书很多,能体现一些新思路新概念。看python的书,写python代码,再用c++实现一遍,这样互补的学习方式貌似也蛮适合自己的。
在《Beginning Python From Novice to Professional》一书的八皇后问题上,python果然精妙而优雅。
在对待

  1. foreachpossibilityatlevel1:
  2. foreachpossibilityatlevel2:
  3. foreachpossibilityatleveln:
  4. ...
  5. isitviable

形式的循环下,它有yield, generator递归。短短20来行代码就搞定了八皇后,如下:

  1. defconflict(state,nextX):
  2. nextY=len(state)
  3. ifnextY==0:print0
  4. foriinrange(nextY):
  5. ifabs(nextX-state[i])in(0,nextY-i):
  6. returnTrue
  7. returnFalse
  8. defqueen(num,state=()):
  9. printstate
  10. forposinrange(num):
  11. ifnotconflict(state,pos):
  12. iflen(state)==num-1:
  13. yield(pos,)
  14. else:
  15. forresultinqueen(num,state+(pos,)):
  16. yield(pos,)+result
  17. defprettyprint(solution):
  18. defline(pos,length=len(solution)):
  19. return'.'*pos+'X'+'.'*(length-pos-1)
  20. forposinsolution:
  21. printline(pos)
  22. if__name__=='__main__':
  23. importrandom
  24. prettyprint(random.choice(list(queen(4))))

其中

  1. forresultinqueen(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:LiWeiJian(mn),lwj1396@163.com
  14. *Company:hunanuniversity
  15. *
  16. *=====================================================================================
  17. */
  18. #include<vector>
  19. #include<iostream>
  20. #include<cmath>
  21. usingnamespacestd;
  22. boolconflict(constvector<int>&state,intnextX)
  23. {
  24. intnextY=state.size();
  25. for(inti=0;i<nextY;i++)
  26. {
  27. if(abs(nextX-state[i])==0||abs(nextX-state[i])==(nextY-i))
  28. returntrue;
  29. }
  30. returnfalse;
  31. }
  32. voidqueen(intnum,vector<int>&state)
  33. {
  34. for(inti=0;i<num;i++)
  35. {
  36. state.clear();
  37. state.push_back(i);
  38. intpos=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. voidprint(intnum,constvector<int>result)
  62. {
  63. for(inti=0;i<result.size();i++)
  64. {
  65. for(intj=0;j<result[i];j++)
  66. cout<<".";
  67. cout<<"X";
  68. for(intj=0;j<num-result[i]-1;j++)
  69. cout<<".";
  70. cout<<endl;
  71. }
  72. }
  73. intmain()
  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了。

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics