#P2730. [IOI 1996 / USACO3.2] 魔板 Magic Squares

[IOI 1996 / USACO3.2] 魔板 Magic Squares

Description

This is a magic board with 88 squares of equal size: | 11 | 22 | 33 | 44 | |:-:|:-:|:-:|:-:| | 88 | 77 | 66 | 55 |

For the board state shown above, we use the sequence {1,2,3,4,5,6,7,8}\{1,2,3,4,5,6,7,8\} to represent it. This is the initial state.

There are three basic operations, denoted by the uppercase letters A\text A, B\text B, and C\text C (you can change the board state using these operations). Below is a demonstration of applying them to the initial state:

A\text A: Swap the top and bottom rows. | 88 | 77 | 66 | 55 | |:-:|:-:|:-:|:-:| | 11 | 22 | 33 | 44 |

B\text B: Move the rightmost column to the leftmost position. | 44 | 11 | 22 | 33 | |:-:|:-:|:-:|:-:| | 55 | 88 | 77 | 66 |

C\text C: Rotate the four central cells clockwise. | 11 | 77 | 22 | 44 | |:-:|:-:|:-:|:-:| | 88 | 66 | 33 | 55 |

At any state during the transformation, all three basic operations can be applied.

Write a program to compute the shortest sequence of basic operations that transforms the initial state into the target state.

Input Format

A single line containing 88 integers a1,a2,,a8a_1, a_2, \dots, a_8 with 1a1,a2,,a881 \leq a_1, a_2, \dots, a_8 \leq 8, separated by spaces, representing the target state.

Output Format

The first line contains an integer, the length of the shortest operation sequence.

Starting from the second line, output the shortest sequence of operations as a string, wrapping every 6060 characters per line. If there are multiple solutions, output the lexicographically smallest one.

2 6 8 4 5 7 3 1 
7 
BCABCCB

Hint

Translation source: NOCOW.

Translated by ChatGPT 5