2014年2月18日星期二

Week 5

Week 5
In this week’s lab, we traced some recursive functions. I think this tracing process helps me understand how the recursive function actually works.

About the tower of Hanoi recursion:
In a 3-stool Hanoi model, the basic recursion process is:
    def threemove(n, source, temp, destination):
        if n == 1:
            model.move(source, destination)
        elif n > 1:
            threemove(n - 1, source, destination, temp)
            threemove(1, source, temp, destination)
            threemove(n - 1, temp, source, destination)

This is not too hard to understand. I traced this code in a 2-cheese and 3-cheese case and it’s all clear. And in a1, we have a 4-stool Hanoi model, the best approach to move all the cheeses from one stool to another stool also uses recursion. To move 10 cheeses, our code takes 49 steps; 15 cheeses, 129 steps. It’s considerably less than , so I think we got that right!

没有评论:

发表评论