Problem of Hanoi Tower (Tower of Hanoi)
What is Hanoi Tower (Tower of Hanoi)?
Problem of Hanoi Tower (Tower of Hanoi) is a math game consisting of 3 columns and with more disk numbers 1.
Below is an illustration of the Tower of Hanoi problem with 3 discs.
The disks are of different sizes and stacked in ascending order to the top-down size: smaller disks are on larger disks. With different disk numbers, we have different Tower of Hanoi problems, but the solutions to these problems are similar. The optimal solution for the Tower of Hanoi problem is when the game has only 3 piles. With a larger number of piles, the solution to the problem is not yet confirmed.
Math game rules of Hanoi Tower (Tower of Hanoi)
The task of the game is to move the disks of different sizes to another column so as to ensure the original order of the disks: the small disk is on the large disk. Here are some rules for the Hanoi Tower math game (Tower of Hanoi):
Only one disk from one column to another can be moved at a time.
Only move the disk at the top (do not move the disks between).
Larger disks cannot be placed on smaller sized discs.
Below is an illustration of how to solve the Tower of Hanoi problem with 3 discs.
The problem of Hanoi Tower (Tower of Hanoi) with disk number n can be solved with the minimum number of steps of 2 n −1
. Therefore, in the case of 3 discs, the Tower of Hanoi problem can be solved after 2 3 −1 = 7
steps.
Algorithm for Hanoi Tower problem (Tower of Hanoi)
To write an algorithm for the Tower of Hanoi math game, we first need to learn how to solve the problem with the number of disks of 1 and 2. We assign 3 columns with the following names:
cotNguon : the original column contains the disk
cotDich : column to move the disks to
CentralGian : intermediate columns have the purpose of mediating during disk movement
If there is only 1 disk, we only need to move from cotNguon to cotDich.
If there are 2 discs:
First we move the top disk (the smallest disk) to the MiddleGian.
Then we move the disk at the bottom (larger disk) to cotDich.
And finally move the smallest disk from the Chinese version to cotDich.
From these two algorithms, we will have an algorithm for the Tower of Hanoi problem for 3 or more discs. We divide the disk stack into two parts: the largest (the nth disk) is the first and (n-1) the other is the second.
Our goal is to move the nth disk from cotNguon to cotDich and then put all the remaining (n-1) disks on it. Now we can imagine how to solve the problem based on recursion according to the following steps:
Bước 1 : Di chuyển n-1 đĩa từ cotNguon tới cotTrungGian Bước 2 : Di chuyển đĩa thứ n từ cotNguon tới cotDich Bước 3 : Di chuyển n-1 đĩa từ cotTrungGian về cotDich
The recursive algorithm for the Tower of Hanoi problem is:
B ắ t đầ u gi ả i thu ậ t Th á p H à n ộ i Hanoi ( disk , cotNguon , cotDich , cotTrungGian ) IF disk == 0 , th ì di chuy ể n đĩ a t ừ cotNguon t ớ i cotDich ELSE Hanoi ( disk - 1 , cotNguon , cotTrungGian , cotDich ) // Bước 1 di chuy ể n đĩ a t ừ cotNguon t ớ i cotDich // Bước 2 Hanoi ( disk - 1 , cotTrungGian , cotDich , cotNguon ) // Bước 3 K ế t th ú c IF K ế t th ú c gi ả i thu ậ t
According to Tutorialspoint
Previous article: Basic concepts of recursion
Next lesson: Fibonacci sequence in data structure and algorithm
You should read it
- Watch the ice-cream tower with a fun dress
- Tower of Fantasy: a mobile game that competes with Genshin Impact
- Top 10 best Tower defense game on mobile phones 2020
- What is tower fan? 5 advantages of tower fan compared to traditional tree fan?
- Top 5 luxury 5-star hotels in Hanoi
- TOP best tower defense game on phone
- The difference between a mid tower and a full tower computer case
- Pisa leaning tower - The most unique architecture on the planet
May be interested
- Sample painting of Hanoi in my heart, simple landscapeyou love hanoi and want to keep the beautiful moments of the capital in your heart? the hanoi in me painting is a great choice to highlight the beauty of the city through each delicate stroke.
- 20+ types of imagery taking virtual life with the leaning tower of Pisaplease join us through 20+ types of photos to take virtual life, make clever games with pisa leaning tower below!
- The latest All Star Tower Defense 2021 Codecode all star tower defense will give you more gems, exp and sometimes character skins. some of the latest all star tower defense giftcodes below will give players the above items.
- Tower of Fantasy tips for beginnerstower of fantasy tips for beginners, tower of fantasy tips for newbies, making the overall experience smooth, navigating the world
- List of phone numbers of taxi firms in Hanoithis is a list of hanoi taxi numbers, including some big taxi companies such as mai linh taxi, thanh nga taxi, thang long taxi, capital taxi, ... and some other taxi companies so you can call electric taxi reservations when in need of moving around the capital.
- Top 10 best Tower defense game on mobile phones 2020tower defense game is an interesting game with the goal of protecting your stronghold and soldier units from the onslaught of the enemy.
- TOP best tower defense game on phonetop best tower defense games on mobile, top best tower defense games on mobile phones, with a variety of gameplay, this is one of the most popular game genres on mobile.
- Top 5 PC cases worth buying 2019the difficult thing for many people is how to choose a computer case that suits the needs, quality and price among thousands of options in the market. so this article will introduce you to some of the best computer case from full tower, mid tower to mini-itx.
- Latest Anime World Tower Defense Codes and How to Enter Codesanime world tower defense code is also an attractive game with anime theme with many characters from different anime series. if you want to redeem gifts from anime world tower defense code, please go through the list of latest anime world tower defense codes and how to enter the code.
- Why the foundation of the world's tallest tower Burj Khalifa must be powered 24/7a special foundation with a construction period of up to 2 years helps the 800-meter-high burj khalifa in dubai withstand winds of 240 km/h.