The classic case of recursive algorithm of Hanoi tower implemented by python

@本文来源于公众号:csdn2299,喜欢可以关注公众号 程序员学府
学到递归的时候有个汉诺塔的练习,汉诺塔应该是学习计算机递归算法的经典入门案例了,所以本人觉得可以写篇博客来表达一下自己的见解。这markdown编辑器还不怎么会用,可能写的有点格式有点丑啦,各位看官多多见谅.

Internet to find a picture of the Tower of Hanoi, Tower of Hanoi is to use the disk with the middle column on the left-most column in descending order stack up, it means the same as a c keep the original Insert picture description here
 cut the crap , First light the code

def move(n, a, buffer, c):
  if(n == 1):
    print(a,"->",c)
    return
  move(n-1, a, c, buffer)
  move(1, a, buffer, c)
  move(n-1, buffer, a, c)
move(3, "a", "b", "c")

The first is to define a moving function. The four parameters represent the number of plates on the a column, and the buffer is the b column. The buffer is named buffer for easy understanding. As the name implies, it is a buffer that moves a to c. Then c is the target Columns
Let's read
 the general writing of function code recursion, there must be a condition to stop the recursion cycle, so when judging that the number of plates on the a column is 1, you can stop the recursion and return, and there is only one above the a column I moved a to c. The key point is the following code. Recursion is actually a very abstract algorithm. We need to use abstract thinking to think about the problem of Hanoi Tower. Think of the plate on the a column in two, which is the above. The bottom plate and the bottom plate, if shown Insert picture description here
  We don't care how many plates are on the top, our every operation is to move the bottom plate through the buffer b column buffer to the c column.
 Children's shoes must be thinking why they want to move the sauce. In fact, this is a summary. If you play the Hanota game yourself, you will find the rule. In fact, this game is to keep all the above methods to move to b, and then get the last one of a to c, and then rack your brains to move b to c. At this time, you will find that the original on b must also be stored through the empty one, a. n-1 above b, and then move the maximum final b to c, here the law is reflected, you can also abstract the method of movement, and you can use this to design a program algorithm.
 Let ’s use the abstraction just now Interpretation of the remaining code

move(n-1, a, c, buffer)

This code means to move the n-1 above the a column just mentioned to move to the buffer buffer through c according to the rule from small to large. This function enters recursion.

move(1, a, buffer, c)

When the execution of the above statement is completed, that is, the recursive movement of n-1 plates is completed, the execution of this statement is to move a plate on the a column to c, which is the so-called bottom plate

move(n-1, buffer , a, c)

The last step is to move the n-1 on a to the buffer just now. It must be moved through a to c to complete the movement of the entire Tower of Hanoi, so the last step is to pass the n-1 on a When the buffer moves to the c-pillar.
 I will write down the whole moving process, taking 3 on the a-pillar as an example

/**
我把3个盘子的汉诺塔全部通过代码演示,按缩进原则,每一个缩进即进一个递归函数,每打印一次即中止当前递归,也就是每个print
说明:
  1.n = 3, n = 2, n = 1是每次执行if(n == 1)的结果,这里就不写判断了,相信童鞋们也能看懂,也就是n不等与1时就减1进入递归
  2.请注意a,b,c柱每次进入函数的顺序,不要被形参带错路了,看准每次函数参数的实参 
**/
move(3, "a", "b", "c")
n=3:
  //开始从a上移动n-1即2个盘子通过c移动到b,以腾出c供a最后一个盘子移动
  move(2, "a","c","b")
  n=2:
  //开始进行n=2的一个递归,把当前a('a')柱上的n-1个盘子通过c('b')移动到b('c')
    move(1, "a", "b", "c")
    n=1:
    //n=2的第一个递归完成,打印结果,执行当前子函数剩余代码
      print("a", "->", "c") 
    move(1, "a", "c", "b")
    n=1:
      print("a", "->", "b")
    move(1, "c", "a", "b")
    n=1:
      print("c", "->", "b")
     //到这里完成了a柱上面的n-1即是2个盘子的移动
//开始把a柱上最后一个盘子移动到c柱上
move(1, "a", "b", "c")
n=1:
  print("a", "->", "c")
  //到这里完成移动a柱上的最后一个盘子到c柱上 
move(2, "b", "a", "c")
n=2:
//开始进行n=2的第二个递归,即把当前b('b')的盘子(n-1个)通过a('a')移动到c('c')上
  move(1, "b", "c", "a")
  n=1:
  //n=2 的第二个递归完成,打印结果并执行当前子函数的剩余代码
    print("b", "->", "a")
  move(1, "b", "a", "c")
  n=1:
    print("b", "->", "c")
  move(1, "a", "b", "c")
  n=1:
    print("a", "->", "c")
    //到这里把b上的盘子通过a移动到c,
//整个代码执行完毕,汉诺塔移动完成

The final print result is:
Insert picture description here
after understanding the principle of Hanoi Tower's recursive algorithm, children's shoes can write a program to try. Here is just to learn Python's recursion so using Python, children's shoes can be implemented in other languages, Hanoi Tower It can really help to understand the principle of recursion. The importance of recursion in programming is self-evident!
Thank you very much for reading
. When I chose to study python at university, I found that I ate a bad computer foundation. I did n’t have an academic qualification. This is
nothing to do. I can only make up for it, so I started my own counterattack outside of coding. The road, continue to learn the core knowledge of python, in-depth study of computer basics, sorted out, if you are not willing to be mediocre, then join me in coding, and continue to grow!
In fact, there are not only technology here, but also things beyond those technologies. For example, how to be an exquisite programmer, rather than "cock silk", the programmer itself is a noble existence, isn't it? [Click to join] Want to be yourself, want to be a noble person, come on!

34 original articles published · Liked12 · Visitors 20,000+

Guess you like

Origin blog.csdn.net/chengxun03/article/details/105477240