Python puzzle game Calculation Hanoi Tower problem example _python

Source: Internet
Author: User

Hanoi (also known as Heneta) is a puzzle toy from an ancient Indian legend. When the great Brahma created the world, he made three diamond pillars and stacked 64 gold discs on a pillar from the bottom up to the top in order of size. Brahma ordered the Brahman to rearrange the disc from below to another pillar in order of size. It is also stipulated that the disc cannot be enlarged on a small disc and only one disc can be moved between the three pillars at a time.

Copy Code code as follows:

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 ("A", "B", "C")
Print "The number of steps passing:%d"%times

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.