Magic Square Rotations
Time limit2sMemory limit128 MB
Given four fixed permutations that can be applied to an 8-number arrangement, find the minimum number of operations to reach a target arrangement from the initial one via BFS.
- Level
Medium4 of 10
- Topics
- BFS, Simulation, Array
- Solved
- No attempts yet
Problem
There is a magic square made of eight equal-sized squares. Each square has a distinct color, represented by one of the natural numbers from 1 through 8.
A state is represented by the sequence of eight numbers read clockwise starting from the upper-left square. The position numbers are as follows.
1 2 3 4
8 7 6 5
The initial state is (1, 2, 3, 4, 5, 6, 7, 8). The following four transformations can be applied to any state.
- A: Swap the entire top row with the entire bottom row.
- B: Shift each row one square to the right. The rightmost number of each row moves to the leftmost square of that row.
- C: Rotate the four middle squares counterclockwise once.
- D: Swap the numbers at positions 1 and 5.
Starting from the initial state, find the minimum number of transformations needed to make the given target state. Every target state is reachable.
Input
The first line contains 8 integers representing the target state. The integers are given in the state-sequence order defined above.
Output
Print the minimum required number of transformations L on the first line.