There are three pegs A, B, and C. Peg A has N disks of different sizes, with larger disks at the bottom and smaller disks at the top. The goal is to move all disks from peg A to peg C, maintaining the rule that larger disks are below smaller disks (peg B can be used as auxiliary). Each move can only move the topmost disk of one peg to the top of another peg. Please output the moving process.
Answer
This is a type of dynamic programming problem, and it is relatively simple and convenient to implement with recursion.
For the problem of "moving moveSum disks from the from peg to the to peg (using the by peg)", we can accomplish it in the following three steps:
- Move the top moveSum-1 disks on the from peg to the by peg (using the to peg)
- Move the remaining 1 disk on the from peg directly to the to peg
- Move the moveSum-1 disks on the by peg to the to peg (using the from peg)




The execution process is as follows:


Original link: https://blog.51cto.com/myunix/2399892