Python solves the Tower of Hanoi game

The examples in this article share the specific code of python solving the Tower of Hanoi game for everyone for your reference. The specific content is as follows

1. Problem definition

Baidu Encyclopedia Definition: The Tower of Hanoi (also known as the Tower of Hanoi) is an educational toy derived from an ancient legend in India. It is said that when the Great Brahma created the world, he made three diamond pillars. On one pillar, 64 golden discs were stacked in order from small to large. The Brahma ordered the Brahmin to use one of the pillars to reposition the 64 golden discs on the third pillar. It also stipulates that the golden disc that cannot be enlarged on the small golden disc can only be moved one disc at a time between the three pillars.

For example, if the golden disc has only 3 pieces, in order to meet the rules of the game, it must be completed in 8 steps as shown in the following figure:

Two, code implementation

# Move n plates from x column to z column with the help of y column
def hanoi(n, x, y, z):

 count =0if n ==1: #Recursive exit
 print(x,' --  ', z)return1else:
 # Before n-1 plate is moved from the x column to the y column with the help of the z column
 count +=hanoi(n -1, x, z, y) #Recursive call

 # Move the bottom 1 plate from the x-pillar to the z-pillar
 count +=hanoi(1, x, y, z)

 # Will n-1 plate is moved from the y column to the z column with the help of the x column
 count +=hanoi(n -1, y, x, z) #Recursive call

 return count

def main():

 hanoi_level =input("Please enter the number of floors of the Tower of Hanoi:")print("The total number of moves is%d"%hanoi(int(hanoi_level),'X','Y','Z'))if __name__ =='__main__':main()

When the golden disc has 4 layers, the output result of the code is:

Please enter the number of floors of the Tower of Hanoi: 4
X --  Y
X --  Z
Y --  Z
X --  Y
Z --  X
Z --  Y
X --  Y
X --  Z
Y --  Z
Y --  X
Z --  X
Y --  Z
X --  Y
X --  Z
Y --  Z
The total number of moves is 15

The above is the whole content of this article, I hope it will be helpful to everyone's study.

Recommended Posts

Python solves the Tower of Hanoi game
Python implements the source code of the snake game
Python writes the game implementation of fishing master
Consolidate the foundation of Python (4)
Consolidate the foundation of Python(7)
python guess the word game
Consolidate the foundation of Python(6)
Python realizes the guessing game
Consolidate the foundation of Python(5)
Consolidate the foundation of Python (3)
Python implements the brick-and-mortar game
The usage of wheel in python
Python simply implements the snake game
Python handles the 4 wheels of Chinese
Python simulation of the landlord deal
What is the use of Python
The premise of Python string pooling
Secrets of the new features of Python 3.8
The father of Python joins Microsoft
The operation of python access hdfs
The usage of tuples in python
End the method of running python
Understanding the meaning of rb in python
Can Python implement the structure of the stack?
Learn the basics of python interactive mode
Logistic regression at the bottom of python
Solve the conflict of multiple versions of python
What is the scope of python variables
Python implements the sum of fractional sequences
Two days of learning the basics of Python
What is the id function of python
Python basic actual combat-guess the age game
Where is the pip path of python3
Centos 8.1.1911 solves the problem of yum reinstallation
The essence of Python language: Itertools library
What are the advantages of python language
The specific method of python instantiation object
python3 realizes the function of mask drawing
What is the prospect of python development
What is the function body of python
The specific method of python import library
Solve the conflict of multiple versions of python
What is the function of adb in python
Detailed explanation of the principle of Python super() method
The difference between the syntax of java and python
7 features of Python3.9
Python realizes the development of student management system
Python implements the shuffling of the cards in Doudizhu
The meaning and usage of lists in python
Solve the problem of python running startup error
Can the value of the python dictionary be modified?
Detailed explanation of the usage of Python decimal module
How about learning python at the age of 27?
2.1 The Python Interpreter (python interpreter)
The consequences of uninstalling python in ubuntu, very
Use python to realize the aircraft war game
[898] python get the intersection of two lists | union | difference
Detailed explanation of the principle of Python function parameter classification
What is the advantage of python over corporate language
A brief summary of the difference between Python2 and Python3
Detailed explanation of the principle of Python timer thread pool