ADA 2.3 Hanoi Tower
Tower of Hanoi(河內塔)
- Problem: move n disks from A to C
- Rules
- Move one disk at a time
- Cannot place a larger disk onto a smaller disk
沒玩過得去線上版本玩一下 Play Online
簡易規則:一定要在大盤子上面,一次只能移動一個
Hanoi(1)
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
- Move 1 from A to C
當只有一個盤子時,很直覺的A移動到C
💡 1 move in total
Base case
Hanoi(2)
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
- Move 1 from A to B
- Move 2 from A to C
- Move 1 form B to C
Hanoi(3)
Hanoi(n)
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
Pseudocode for Hanoi
💡 No need to combine the results in this case
較沒有效率,重複做很多事
Algorithm Time Complexity
- T(n) = #moves with n disks
- Base case: T(1) = 1
- Recursive case (n > 1): T(n) = 2T(n - 1) + 1
- We will learn how to derive T(n) later
Further Questions
- Q1: Is tight for Hanoi? Can
- Q2: What about more than 3 pegs?
- Q3: Double-color Hanoi problem
- Input: 2 interleaved-color towers
- Output: 2 same-color towers
Q2 What about more than 3 pegs?
Before the start, try to play Tower of Hanoi 3up pegs, click bellow linked.
Towers of Hanoi
In Chapter ADA 2.2
Recurrence and Divide-and-Conquer :
- Defined the base case. (the logic of smallest)
- Find the stop point.
- Answer maybe is not the tight.
It's maybe a simple way to find an answer is not the best solution.
So, is it suitable of 3up Hanoi ?
Tower of Hanoi 4 pegs
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
In my colleage days, the case of 4 pegs is often called Reves’s problem – the problem being to determine the least number of moves to move all disks was not verified until 2014, by Bousch.
Try to Divide-and-Conquer
- There are 4 pegs
- The other rules are the same of Tower of Hanoi
Step 1.
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
move f(n-2)
Step2.
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
move f(1)
Step3.

move f(1) + f(1)
Step4.

move f(n-2)
Compare the best solutions
Solutions using math functions
n > 1;
f(n) = 2^(g(2) + f(n-1)
g(n) = ROUND(√(2x-1)) - 1
Summary
The complexity has increased to the point where it is not easy to use Divide-and-Conquer to find the best answer.
Reference Paper
Q3. Double-color Hanoi problem:
We maybe can not use the same of solutions to find the best answer for 4 pegs Tower of Hanoi.
But we still try to just find a answer.
AKA Bicolour Towers of Hanoi (用這個可以找到滿多的)
I had not tried to proof this answer.