Home > Backend Development > Python Tutorial > python益智游戏计算汉诺塔问题示例

python益智游戏计算汉诺塔问题示例

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-06-06 11:29:47
Original
1177 people have browsed it

汉诺塔(又称河内塔)问题是源于印度一个古老传说的益智玩具。大梵天创造世界的时候做了三根金刚石柱子,在一根柱子上从下往上按照大小顺序摞着64片黄金圆盘。大梵天命令婆罗门把圆盘从下面开始按大小顺序重新摆放在另一根柱子上。并且规定,在小圆盘上不能放大圆盘,在三根柱子之间一次只能移动一个圆盘。

代码如下:


times = 0
def test(num,a,b,c):
    globaltimes
    ifnum==1:
       print (a,b)
       times+=1

    else:
       test(num-1,a,c,b)
       test(1,a,b,c)
       test(num-1,c,b,a)
      

  

test(12,"a","b","c")
print "经过的步数passing:%d"%times

Related labels:
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template