Python algorithm-Hanoi tower problem

Source: Internet
Author: User

Hanoi Tower Problem

Initial state:

thinking: When the number of plates is 3 , we write the moving order

Steps to move:

3 plates, from a to c

1, front two plates, from a to b

1) put the front plate from a to c

A->c

2) Take the second plate, from a to b

A->b

3) put The plates on C, from C to b .

C->b

2, the last plate, from a to c

A->c

3, put Two plates on B, from b to c

1) put the front plate from b to a

B->a

2) put the second plate, from b to c

B->c

3) put a plate on a, from a to c

A->c

Summarize:

n plates, from a to c

1,n-1 a plate from a to b

2, the last plate, from a to c

3.n-1 a plate from b to c

Code implementation:

#Encoding=utf-8 defHanoi (n,a,b,c):ifn = = 1:        PrintA' -', CElse: Hanoi (n-1, A,c,b) Hanoi (1, A,b,c) Hanoi (n-1, B,a,c)if __name__=='__main__': Hanoi (3,'a','b','C')

Python algorithm-Hanoi tower problem

Related Article

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.