#P1242. 新汉诺塔

新汉诺塔

Description

There are nn hollow circular disks of distinct sizes, labeled from 11 to nn in ascending order by size. These nn disks are arbitrarily nested on three pegs labeled AA, BB, CC. 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 nn, the total number of disks present in the states.

The next three lines each contain several integers, describing the disks on pegs AA, BB, CC in the initial state, listed from bottom to top. If a line contains only the single number 00, that peg has no disk.

The following three lines each contain several integers, describing the disks on pegs AA, BB, CC in the target state, listed from bottom to top. If a line contains only the single number 00, 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 100%100\% of the testdata, 1n451 \le n \le 45, and 11 \le each disk's index n\le n.

Each line’s disk description is listed from bottom to top.

Translated by ChatGPT 5