#P2730. Magic Board Transformation

    ID: 15993 Type: Default 1000ms 256MiB

Magic Board Transformation

Magic Board Transformation

You are given a magic board whose 8 squares are colored with 8 distinct colors represented by the numbers 1 through 8. The board is arranged in 2 rows and 4 columns. A board state is represented by a sequence of 8 numbers obtained by reading the board in clockwise order starting from the top‐left corner. In the basic state the sequence is \(\{1,2,3,4,5,6,7,8\}\).

Three basic operations are defined to transform the board. They are labeled as follows:

  • A: Swap the upper and lower rows.
  • B: Remove the rightmost column and insert it as the leftmost column (shifting the other columns to the right).
  • C: Rotate the four central squares clockwise. The central squares are the ones at positions (row 0, column 1), (row 0, column 2), (row 1, column 2) and (row 1, column 1).

For example, applying the operations on the basic state gives:

  • A: The board becomes
    Row 0: 8 7 6 5
    Row 1: 1 2 3 4
    (Border sequence: \(\{8,7,6,5,4,3,2,1\}\)).
  • B: The board becomes
    Row 0: 4 1 2 3
    Row 1: 5 8 7 6
    (Border sequence: \(\{4,1,2,3,6,7,8,5\}\)).
  • C: The board becomes
    Row 0: 1 7 2 4
    Row 1: 8 6 3 5
    (Border sequence: \(\{1,7,2,4,5,3,6,8\}\)).

Your task is to compute the minimum sequence of operations (using letters A, B, and C) required to transform the basic state into a given target state. It is guaranteed that the target state is reachable using these operations.

Note: All formulas are given in LaTeX format.

inputFormat

The input contains a single line with 8 space-separated integers representing the target board configuration. The configuration is given by reading the board in clockwise order starting from the top-left corner.

outputFormat

Output the minimal sequence of operations (a string composed of letters A, B, and C) that transforms the basic state \(\{1,2,3,4,5,6,7,8\}\) into the target state. If the board is already in the target state, output an empty line.

sample

1 2 3 4 5 6 7 8