6686.Tower of Hanoi

Time Limit: 1s Memory Limit: 512MB

The Tower of Hanoi game consists of three stacks (left, middle and right) and $n$ round disks of different sizes. Initially, the left stack has all the disks, in increasing order of size from top to bottom.

The goal is to move all the disks to the right stack using the middle stack. On each move you can move the uppermost disk from a stack to another stack. In addition, it is not allowed to place a larger disk on a smaller disk.

Your task is to find a solution that minimizes the number of moves.

Input Format(From the terminal/stdin)

The only input line has an integer $n$ : the number of disks.

  • $1 \le n \le 16$

Output Format(To the terminal/stdout)

First print an integer $k$ : the minimum number of moves.

After this, print $k$ lines that describe the moves. Each line has two integers $a$ and $b$ : you move a disk from stack $a$ to stack $b$ .

Sample Input

Copy
2
 \n

Sample Output

Copy
3
1 2
1 3
2 3
 \n
 · \n
 · \n
 · \n
Source: CSES, Introductory Problems, 2165

Submit

请先 登录

© 2025 FAQs