The quanta problem is actually very simple.

Source: Internet
Author: User

First, go to the code

1 DefHanoi_move (n, source, DEST, intermediate ):2IfN> = 1:#Recursive exit, with only one plate left3Hanoi_move (n-1, Source, intermediate, DEST)4Print("Move % s-> % s"%(Source, DEST ))5Hanoi_move (n-1, intermediate, DEST, source)

 

First, three poles are discharged in sequence, namely source, DEST, and intermediate for the source rod, media rod, and target rod Code. There are n big cakes on the source rod.

We define a function def Hanoi (n, source rod, target rod, and media rod): # It means that the source rod uses the media rod to the target rod.

We assume n-1 layers above the bottom layer have been placed, that is, there are currently only two big cakes on the source rod. The operation we want to perform is:

The n-1 layer on the source rod is equivalent to the n-1 layer on the media Rod Using the target rod to the media rod.

Print the path from the source pole to the target pole

Then, the n-1 layer of the media rod is equivalent to Hanoi (n-1, media rod, target rod, and source rod) by using the source rod to the target rod)

Let's talk about the assumption of N-1 layer, because we used recursion when assuming n-1 layer, in Hanoi (n-1 ...) We assume that the N-2 layer has been placed, and then in Hanoi (n-2 ...) When we assume that the n-3 layer has been placed, continuous backtracking, to the top layer has been placed, this assumption is reasonable, you can use this method. With Recursive Backtracking, it is basically impossible to use recursive ideas to continuously introduce his steps. However, it is easier to get results by making assumptions on the premise.

The quanta problem is actually very simple.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.