Tower of Hanoi
Time: 1 s · Memory: 256 MB
- Recursion
Statement
There are three pegs (, , ). Peg holds disks: the largest at the bottom, smaller and smaller upwards. All disks must be moved to peg . Rules: one move takes a single disk — the top one of some peg — and puts it on another peg; a larger disk may never be placed on a smaller one.
Move the disks with the fewest possible moves and print the moves in order.
Input format
A single line with , , (; , ).
Output format
The first line contains the number of moves. Then each move on its own line: the peg to take the disk from and the peg to put it on.
Examples
Input
2 1 3
Output
3 1 2 1 3 2 3
Input
1 2 1
Output
1 2 1
Note
To move disks from to : move the top disks to the third peg (), put the largest disk on , then move the disks from the third peg to . Base case: — do nothing. There are moves, and the shortest sequence is unique.
First example: small disk , large disk , small disk .
