Doraemon enjoys solving the Rubik's Cube.
Today, he received a scrambled 2×2×2 Rubik's Cube and wants to know if he can solve it within n moves.
Can you help him out?
To represent the state of a 2x2x2 Rubik's Cube, we use a string consisting of W, O, B, G, Y and R, representing white, orange, blue, green, yellow, and red colors, respectively, and the string has a length of 24. For more details, you can refer to the above image.
We define a move performed on a Rubik's Cube as a clockwise rotation of one of its six faces (Up, Left, Front, Right, Back, Down). To simplify this problem, the counterclockwise rotation is defined as equivalent to performing three moves.
In this problem, the code for preprocessing the Rubik's Cube state string, I/O and some useful functions are already provided.
---> Please follow the link to download the code <---
Your only task is to implement solve(int n) function:
return 1 if the Rubik's Cube can be solved within n moves, otherwise return 0.
In your implementation, you may need to utilize the following functions:
You can trace the code for more detailed information.
Sample Input 1 | Sample Input 2 |
![]() |
![]() |
move(DOWN); move(FRONT); move(LEFT);
move(UP); move(DOWN); move(RIGHT); move(BACK); move(FRONT); move(LEFT);
You can use recursion to implement the function and explore all possible ways to solve the Rubik's Cube.
To prevent unnecessary searches that could lead to a TLE result, make sure to terminate the function when the number of moves exceeds.
Output T lines. For each line, if the given Rubik's Cube can be solved within n moves, print "Yes", Otherwise, print "No".