Problem Description
Problem Analysis
(1) First, check whether it has already been fully placed or has not started moving yet. In this case we do not need to consider optimality, i.e. all disks are on peg C or all disks are on peg A. This is legal, so directly output true.
(2) [Consider the disk with the largest number, disk n; its movement direction is A–>C. It can only be on peg A or peg C. If it is on peg B, we can directly return false—①];
If the disk with the largest number is on peg A, it means the other disks are currently performing the A–>B operation, so consider the n-1 disks being moved, and then judge in the same way as ①, except that the largest disk number becomes n-1 and the movement direction becomes A–>B;
If the disk with the largest number is on peg C, it means the other disks are currently performing the B–>C operation, so consider the n-1 disks being moved, and then judge in the same way as ①, except that the largest disk number becomes n-1 and the movement direction becomes B–>C;
Use recursion to continuously perform operation ① in this way.
| |