javascript解汉诺塔问题
传说某间寺院有三根柱子,在创世之初,第一根柱子串有64个金盘,盘的尺寸由下到上一个比一个小。寺院里的僧侣依照一个古老的预言,每天移动一个盘;大盘不能叠在小盘上面,预言说盘子全部移动到到第三根柱子时,世界就会灭亡。最少移动步数是随着盘子的个数呈指数增长(2^n-1)的。对指数增长有概念的同学应该能够看出移动64个盘子所需的步数是个天文数字,即使僧侣们每秒可完成一个盘子的移动,也需要5846亿年才能完成,整个宇宙现在也不过137亿年。这个世界灭亡传说的本意大概是在祝福世界不要灭亡吧。在《猩球崛起》故事中就是用同样的问题来给猩猩们测智商,用了4个盘子。四个盘子最少需要挪动2^4-1=15步,这只猩猩最快15步就完成了,难怪科学家们要大吃一惊了。下面是一个有7个盘子的儿童玩具,最少需要2^7-1=127步完成。最后,用javascript编制一个递归算法,来做一做这个儿童游戏吧:var hanoi = function(k,src,tmp,dst){if(k>0){hanoi(k-1,src,dst,tmp);//把上面k-1个盘子从src通过dst搬到tmpiCount+=1;put("iCount = " + iCount + " move disc " + k + " from " + src + " to " + dst);//把最大的盘子从src搬到dsthanoi(k-1,tmp,src,dst);//把k-1个盘子从tmp通过src搬到dst}}var iCount=0;//计数var n = 7; //盘子个数pt("Math.pow(2,"+n+")-1");hanoi(n,'A','B','C');put("~~~~~~~~~game over~~~~~~~~~~~~~~~");调试信息:Math.pow(2,7)-1 127iCount = 1 move disc 1 from A to CiCount = 2 move disc 2 from A to BiCount = 3 move disc 1 from C to BiCount = 4 move disc 3 from A to CiCount = 5 move disc 1 from B to AiCount = 6 move disc 2 from B to CiCount = 7 move disc 1 from A to CiCount = 8 move disc 4 from A to BiCount = 9 move disc 1 from C to BiCount = 10 move disc 2 from C to AiCount = 11 move disc 1 from B to AiCount = 12 move disc 3 from C to BiCount = 13 move disc 1 from A to CiCount = 14 move disc 2 from A to BiCount = 15 move disc 1 from C to BiCount = 16 move disc 5 from A to CiCount = 17 move disc 1 from B to AiCount = 18 move disc 2 from B to CiCount = 19 move disc 1 from A to CiCount = 20 move disc 3 from B to AiCount = 21 move disc 1 from C to BiCount = 22 move disc 2 from C to AiCount = 23 move disc 1 from B to AiCount = 24 move disc 4 from B to CiCount = 25 move disc 1 from A to CiCount = 26 move disc 2 from A to BiCount = 27 move disc 1 from C to BiCount = 28 move disc 3 from A to CiCount = 29 move disc 1 from B to AiCount = 30 move disc 2 from B to CiCount = 31 move disc 1 from A to CiCount = 32 move disc 6 from A to BiCount = 33 move disc 1 from C to BiCount = 34 move disc 2 from C to AiCount = 35 move disc 1 from B to AiCount = 36 move disc 3 from C to BiCount = 37 move disc 1 from A to CiCount = 38 move disc 2 from A to BiCount = 39 move disc 1 from C to BiCount = 40 move disc 4 from C to AiCount = 41 move disc 1 from B to AiCount = 42 move disc 2 from B to CiCount = 43 move disc 1 from A to CiCount = 44 move disc 3 from B to AiCount = 45 move disc 1 from C to BiCount = 46 move disc 2 from C to AiCount = 47 move disc 1 from B to AiCount = 48 move disc 5 from C to BiCount = 49 move disc 1 from A to CiCount = 50 move disc 2 from A to BiCount = 51 move disc 1 from C to BiCount = 52 move disc 3 from A to CiCount = 53 move disc 1 from B to AiCount = 54 move disc 2 from B to CiCount = 55 move disc 1 from A to CiCount = 56 move disc 4 from A to BiCount = 57 move disc 1 from C to BiCount = 58 move disc 2 from C to AiCount = 59 move disc 1 from B to AiCount = 60 move disc 3 from C to BiCount = 61 move disc 1 from A to CiCount = 62 move disc 2 from A to BiCount = 63 move disc 1 from C to BiCount = 64 move disc 7 from A to CiCount = 65 move disc 1 from B to AiCount = 66 move disc 2 from B to CiCount = 67 move disc 1 from A to CiCount = 68 move disc 3 from B to AiCount = 69 move disc 1 from C to BiCount = 70 move disc 2 from C to AiCount = 71 move disc 1 from B to AiCount = 72 move disc 4 from B to CiCount = 73 move disc 1 from A to CiCount = 74 move disc 2 from A to BiCount = 75 move disc 1 from C to BiCount = 76 move disc 3 from A to CiCount = 77 move disc 1 from B to AiCount = 78 move disc 2 from B to CiCount = 79 move disc 1 from A to CiCount = 80 move disc 5 from B to AiCount = 81 move disc 1 from C to BiCount = 82 move disc 2 from C to AiCount = 83 move disc 1 from B to AiCount = 84 move disc 3 from C to BiCount = 85 move disc 1 from A to CiCount = 86 move disc 2 from A to BiCount = 87 move disc 1 from C to BiCount = 88 move disc 4 from C to AiCount = 89 move disc 1 from B to AiCount = 90 move disc 2 from B to CiCount = 91 move disc 1 from A to CiCount = 92 move disc 3 from B to AiCount = 93 move disc 1 from C to BiCount = 94 move disc 2 from C to AiCount = 95 move disc 1 from B to AiCount = 96 move disc 6 from B to CiCount = 97 move disc 1 from A to CiCount = 98 move disc 2 from A to BiCount = 99 move disc 1 from C to BiCount = 100 move disc 3 from A to CiCount = 101 move disc 1 from B to AiCount = 102 move disc 2 from B to CiCount = 103 move disc 1 from A to CiCount = 104 move disc 4 from A to BiCount = 105 move disc 1 from C to BiCount = 106 move disc 2 from C to AiCount = 107 move disc 1 from B to AiCount = 108 move disc 3 from C to BiCount = 109 move disc 1 from A to CiCount = 110 move disc 2 from A to BiCount = 111 move disc 1 from C to BiCount = 112 move disc 5 from A to CiCount = 113 move disc 1 from B to AiCount = 114 move disc 2 from B to CiCount = 115 move disc 1 from A to CiCount = 116 move disc 3 from B to AiCount = 117 move disc 1 from C to BiCount = 118 move disc 2 from C to AiCount = 119 move disc 1 from B to AiCount = 120 move disc 4 from B to CiCount = 121 move disc 1 from A to CiCount = 122 move disc 2 from A to BiCount = 123 move disc 1 from C to BiCount = 124 move disc 3 from A to CiCount = 125 move disc 1 from B to AiCount = 126 move disc 2 from B to CiCount = 127 move补充:web前端 , JavaScript ,
上一个:JS弹出层
下一个:javascript 正则表达式
- 更多JAVA疑问解答:
- java怎么在线读取ftp服务器上的文件内容
- 关于程序员的职业规划
- HTML和JSP矛盾吗?
- java小程序如何打包?
- java怎么split路径文件名?
- jsp+javaBean中Column 'ordersPrice' specified twice的错误
- Java TCP/IP Socket网络编程系列
- 大家来讨论一下我到底该用什么好?Swing 还是 JavaFX
- 关于Hibernate实体自身多对一的抓取问题
- 关于apache2+tomcat群集出现的问题
- spring 获取上下文问题
- SSH 导入导出excel 谁有这块的资料吗?
- Ext TreePanel 刷新问题
- springmvc 加载一个jsp页面执行多个方法 报404
- checkbox数组action怎么向页面传值