Grandpa's Rubik Cube
Time limit1sMemory limit128 MB
Given a Rubik's cube configuration and a list of face rotations, decide whether applying them all yields a solved cube with each face a single color.
- Level
Medium6 of 10
- Topics
- Simulation, Implementation, Matrix, Array
- Solved
- No attempts yet
Problem
A Rubik's cube is a well-known puzzle: a cube whose six faces are covered with colored stickers. The goal is to rotate the faces until every face shows a single color.

Figure 1: Rubik Cube
When a face is rotated, the stickers along the touching edges of the four neighboring faces move as well. Figure 2 shows one such rotation.

Figure 2: Rotation example
Your grandpa claims that, for any starting configuration, he can produce a sequence of rotations that reaches a winning configuration — one in which every face shows a single color. Given a starting configuration and a list of rotations, decide whether applying that list, in order, actually reaches a winning configuration.
The six colors are Yellow (Y), Red (R), Blue (B), Green (G), White (W) and Magenta (M).

Figure 3: Representation of the cube
Unfolded layout. The cube is written unfolded, as in Figure 3a. The first three lines hold the Top face. The next three lines hold four faces printed side by side, in the order Left, Front, Right, Back. The last three lines hold the Bottom face. The Top and Bottom faces are attached to the Front face.
Face numbering (Figure 3b). The faces are numbered 1 to 6: 1 = Left, 2 = Front, 3 = Right, 4 = Back, 5 = Top, 6 = Bottom.
Rotations. Each rotation is a nonzero integer. Its absolute value selects the face to turn; a positive value is a clockwise quarter turn and a negative value is a counter-clockwise quarter turn, in both cases as seen while looking directly at that face from outside the cube.
Input
The first line contains an integer: the number of test cases.
Each test case consists of ten lines. The first nine lines describe the starting configuration in the unfolded layout above, with stickers separated by single spaces. The tenth line lists the rotations to apply, in order, and is terminated by the value 0.
Output
For each test case, print a single line. Print Yes, grandpa! if applying the rotations reaches a winning configuration (every face a single color), and No, you are wrong! otherwise.