1002: Tower of Hanoi
Memory Limit:128 MB
Time Limit:5.000 S
Judge Style:Text Compare
Creator:
Submit:24
Solved:8
Description
Consider a mathematical puzzle consisting of three rods and a number of disks of various diameters, which can slide onto any rod. The puzzle begins with the disks stacked on one rod in order of decreasing size, the smallest at the top, thus approximating a conical shape. The objective of the puzzle is to move the entire stack to one of the other rods, obeying the following rules:
Only one disk may be moved at a time.
Each move consists of taking the upper disk from one of the stacks and placing it on top of another stack or on an empty rod.
No disk may be placed on top of a disk that is smaller than it.
With three disks, the puzzle can be solved in seven moves. The minimal number of moves required to solve a Tower of Hanoi puzzle is 2N − 1, where N is the number of disks.
In this problem the input is N, the number of disk. The output is the complete trace (movement) of the disks following "EXACTLY" the format shown below.
Input
2
Output
Step 1: Move disk 1 from peg A to peg B
Step 2: Move disk 2 from peg A to peg C
Step 3: Move disk 1 from peg B to peg C
Sample Input Copy
3
Sample Output Copy
Step 1: Move disk 1 from peg A to peg C
Step 2: Move disk 2 from peg A to peg B
Step 3: Move disk 1 from peg C to peg B
Step 4: Move disk 3 from peg A to peg C
Step 5: Move disk 1 from peg B to peg A
Step 6: Move disk 2 from peg B to peg C
Step 7: Move disk 1 from peg A to peg C