#P1242. 新汉诺塔
新汉诺塔
Description
There are hollow circular disks of distinct sizes, labeled from to in ascending order by size. These disks are arbitrarily nested on three pegs labeled , , . This configuration is called the initial state.
Your task is to find a shortest sequence of moves that transforms the initial state into the target state.
The move rules are as follows:
- Only one disk may be moved at a time.
- A larger disk may not be placed on top of a smaller disk.
Input Format
The first line contains an integer , the total number of disks present in the states.
The next three lines each contain several integers, describing the disks on pegs , , in the initial state, listed from bottom to top. If a line contains only the single number , that peg has no disk.
The following three lines each contain several integers, describing the disks on pegs , , in the target state, listed from bottom to top. If a line contains only the single number , that peg has no disk.
Output Format
Several lines, each containing a string in the format move I from P to Q, representing a single move.
Then output one line with one integer, the minimum number of moves from the initial state to the target state.
5
3 3 2 1
2 5 4
0
1 2
3 5 4 3
1 1
move 1 from A to B
move 2 from A to C
move 1 from B to C
move 3 from A to B
move 1 from C to B
move 2 from C to A
move 1 from B to C
7
Hint
Constraints: For of the testdata, , and each disk's index .
Each line’s disk description is listed from bottom to top.
Translated by ChatGPT 5
京公网安备 11011102002149号