Tower of Hanoi programming recursion, 3 decomposing step, three minutes Society

If you want to understand what is recursive, then the Tower of Hanoi is a very classic example. Almost all textbooks will be referred to Tower of Hanoi.

Here Insert Picture Description

Tower of Hanoi Legend

Tower of Hanoi has a very interesting legend.

In the center of the world Benares (in northern India) holy temple, a brass plate needle stuck three gems. Hindu God Lord Brahma at the time of creation of the world, in which a needle from bottom to top to put on the descending of 64 gold piece, which is called the Tower of Hanoi. Day or night, there is always a monk in these gold pieces move in accordance with the following rules: only one moving one, no matter on which needles, platelets must be large above. The monks predicted that when all the pieces are put gold from Brahma that needle moved to another time on a pin, it will destroy the world in a clap of thunder, the Vatican and towers, temples and beings will also die.
Source: Baidu one hundred Kehannuota

That is, when the monks the 64 gold pieces from the left to the far right in accordance with the rules of movement, this world would be destroyed.

Do not read this tension, let us analyze it possible to see the monks really put 64 gold pieces are moved to the right.

Definition of needles

First, we can classify to three needles:

  • Needles start
    start position gold sheet.

  • Transition needles
    intermediate transition put a temporary location other gold pieces.

  • Certain needles
    final destination place gold sheet.

Step 3 decomposing step HANOR

First, we can unlock the Tower of Hanoi first step the decomposition steps can help us simplify the problem. Here we are with five Towers of Hanoi example:

Here Insert Picture Description

We all know that if you want to move all the gold piece to the far right (target needles), we first have to put the first 5 gold piece (bottom) to move to the far right , because you can move other pieces of gold than it small, it can not be placed on top of it.

first step

So, to the first five gold pieces to a target needles, we first have to put 1,2,3,4 piece sequin needles are moved on the transition, as shown below:

Here Insert Picture Description

The second step

Then, we can move the first sheet 5 to the target gold needles above:

Here Insert Picture Description

third step

Then we then transition sequin moving target needles needles above the top of it. But note that at this time of transition for the gold piece on needles, the needles starting role with the transition of needles exchanged.

Here Insert Picture Description

总结规律

每次我们要将第n个金片移动到目标银针时,都需要

  1. 先将上方的n-1个金片移动到过渡银针上
  2. 将第n个金片移动到目标银针上
  3. 将过渡银针上的n-1个金片移动到目标银针上

以此类推,当我们准备将上方n-1个金片挪到过渡银针上时,对于第n-1个金片来说,之前的过渡银针就是它的目标银针,而我们得先将上方的n-2个金片移动到另外一个过渡银针上面。在那之前,我们得把上方n-3个金片给…

由此可见,这是一个递归过程,不停地将问题数量缩小,并且每一个阶段的逻辑都是一样的。

因此,我们可以将代码写成递归的方式,只是在每一次递归调用的时候,起始银针,过渡银针以及目标银针的角色总是会产生变化,因为对于不同阶段的不同金片来说,他们的起始,过渡以及目标银针是不一样的。

参考代码 Python

注意每一次调用递归函数的时候,银针每次都被作为不同角色的参数传入,这表示对于每一个金片来说,银针的角色都在变化。

# numDisks: integer 当前起始银针上需要考虑的金片
# source_rod: [] 起始银针
# middle_rod: [] 过渡银针
# target_rod: [] 目标银针
def move_hanoi_tower(numDisks, source_rod, middle_rod, target_rod):
	# 如果当前起始银针上需要考虑的金片只有1个的话,直接将其从起始银针移动到目标银针
    if (numDisks == 1):
        target_rod.insert(0, source_rod.pop(0))
    else:
    	# 否则,先将上方的金片都移动到过渡银针。这里过渡银针被传入当成目标银针
        move_hanoi_tower(numDisks-1, source_rod, target_rod, middle_rod)
        # 然后将当前的金片直接从起始银针移动到目标银针
        target_rod.insert(0, source_rod.pop(0))
        # 接着,再将过渡银针上的金片移动到目标银针。此时,过渡银针作为这些金片的起始银针
        move_hanoi_tower(numDisks-1, middle_rod, source_rod, target_rod)

步数计算

通过将汉诺塔步骤分解为3步之后,我们便可以轻松计算出任意数量金片所需移动的最少步数了。

根据3步分解:

  1. 将n-1个金片移动到过渡银针需要x步
  2. 将第n个金片移动到目标银针需要1步
  3. 将n-1个金片从过渡银针移动到目标银针需要x步(同第一步需要一样的步数)

我们可以得到移动n个金片最少需要步数:
x + 1 + x = 2x + 1

也就是说,每增加一块金片所增加的步数,都是前一片金片步数的两倍加上一步。

我们可以为此制定一张表来看看,我们知道只有一片金片的时候步数是1:

金片数量 1 2 3 4 5 64
步数 1 3 7 15 31 1.84 * 10^19

公式为:
2^n - 1(n为金片数量)

由此可见,若是要把64片金片移动到最右边去,那需要的步数可是个天文数字啊!就是1秒钟移动一片,到宇宙毁灭的那天也没办法完成。

So, we do not have to worry when 64 gold pieces are moved to the right when the world will be destroyed, because until then, the universe has been destroyed first.

Published 14 original articles · won praise 8 · views 2197

Guess you like

Origin blog.csdn.net/vandavidchou/article/details/103650751