A classic Rubik\'s Cube of size 3 times 3 times 3 is a three dimensional combina
ID: 3583887 • Letter: A
Question
A classic Rubik's Cube of size 3 times 3 times 3 is a three dimensional combination puzzle that has six faces. Each of the six faces is covered by nine stickers, each of one of six solid colors: white, red, blue, orange, green, and yellow White is opposite yellow, blue is opposite green, and orange is opposite red, and the red, white and blue are arranged in that order in a clockwise arrangement. An internal pivot mechanism enables each face to turn independently, thus mixing up the colors. For the puzzle to be solved, each face must be returned to have only one color. The figures below are examples of Rubik's Cube of size 3 times 3 times 3. Figure 1 represents the solved cube. Figure 2 shows the shuffled cube, and Figure 3 illustrates the six faces of the cube. Learn more about this puzzle by Googling it. Which data structure do you recommend to implement a cube of size n times n times n? And why? (Justify your answer). Write a program (for a cube of size 3 times 3 times 3) that does the followings. a. Rotate Up: rotates the up face. b. Rotate Bottom: rotates the bottom face. c. Rotate Left: rotates the left face d. Rotate Right: rotates the right face. e. Rotate Front; rotates the front face. f. Rotate Back: rotates the back face. g. Shuffles the faces of the cube. h. Solves the cube and shows the solution step by step. Compute the time and space complexities of the program you wrote in part (3).Explanation / Answer
we will use a 1D array to represent the cude here, since the question does not ask for a 3D representation. We could have also used a tree but array indexing in easier and faster. code for all movements is given below for clockwise rotation, we can do for anticlockwise similarly! also,because of characeter limit, i'm eliminating some modules. but these will be solved similarly!
------------------------------------------
public class movement { // ---------------------------------------------------------------------------- // General Moves (clockWise) // ---------------------------------------------------------------------------- public static void up(Cube[] C) { int[] rotate = { 9, 10, 11 }; Cube temper; for (int i = 0; i < 3; i++) { int spot = rotate[i]; temper = C[spot]; C[spot] = C[spot + 9]; C[spot + 9] = C[spot + 18]; C[spot + 18] = C[spot + 27]; C[spot + 27] = temper; } int [] leftR = {0, 1}; for(int place: leftR){ temper = C[place]; if(place == 0){ C[place] = C[place + 6]; C[place + 6] = C[place + 8]; C[place + 8] = C[place + 2]; C[place + 2] = temper; } if(place == 1){ C[place] = C[place + 2]; C[place + 2] = C[place + 6]; C[place + 6] = C[place + 4]; C[place + 4] = temper; } } } public static void bottom(Cube[] C) { int[] rotate = { 42, 43, 44 }; Cube temper; for (int i = 0; i < 3; i++) { int spot = rotate[i]; temper = C[spot]; C[spot] = C[spot - 9]; C[spot - 9] = C[spot - 18]; C[spot - 18] = C[spot - 27]; C[spot - 27] = temper; } int [] leftR = {45, 46}; for(int place: leftR){ temper = C[place]; if(place == 45){ C[place] = C[place + 6]; C[place + 6] = C[place + 8]; C[place + 8] = C[place + 2]; C[place + 2] = temper; } // ---------------------------------------------------------------------------- public static void right(Cube[] C) { int[] rotate = { 2, 5, 8 }; Cube temper; for (int i = 0; i < 3; i++) { int spot = rotate[i]; temper = C[spot]; C[spot] = C[spot + 18]; C[spot + 18] = C[spot + 45]; C[spot + 45] = C[spot + 36]; C[spot + 36] = temper; } int [] leftR = {27, 28}; for(int place: leftR){ temper = C[place]; if(place == 27){ C[place] = C[place + 6]; C[place + 6] = C[place + 8]; C[place + 8] = C[place + 2]; C[place + 2] = temper; } if(place == 28){ C[place] = C[place + 2]; C[place + 2] = C[place + 6]; C[place + 6] = C[place + 4]; C[place + 4] = temper; } } } public static void left(Cube[] C) { int[] rotate = { 51, 48, 45 }; Cube temper; for (int i = 0; i < 3; i++) { int spot = rotate[i]; temper = C[spot]; C[spot] = C[spot - 27]; C[spot - 27] = C[spot - 45]; C[spot - 45] = C[spot - 9]; C[spot - 9] = temper; } int [] leftR = {17, 16}; for(int place: leftR){ temper = C[place]; } } // ---------------------------------------------------------------------------- public static void face(Cube[] C) { int[] rotate = { 6, 7, 8, 24, 25 }; Cube temper; for (int spot : rotate) { if(spot == 6){ temper = C[spot]; C[spot] = C[spot + 21]; C[spot + 21] = C[spot + 41]; C[spot + 41] = C[spot + 11]; C[spot + 11] = temper; } if(spot == 7){ temper = C[spot]; C[spot] = C[spot + 23]; C[spot + 23] = C[spot + 39]; C[spot + 39] = C[spot + 7]; C[spot + 7] = temper; } if(spot == 8){ temper = C[spot]; C[spot] = C[spot + 25]; C[spot + 25] = C[spot + 37]; C[spot + 37] = C[spot + 3]; C[spot + 3] = temper; } if (spot == 24) { temper = C[spot]; C[spot] = C[spot + 2]; C[spot + 2] = C[spot - 4]; C[spot - 4] = C[spot - 6]; C[spot - 6] = temper; } if(spot == 25){ temper = C[spot]; C[spot] = C[spot - 2]; C[spot - 2] = C[spot - 6]; C[spot - 6] = C[spot - 4]; C[spot - 4] = temper; } } } public static void back(Cube[] C) { int[] rotate = { 0, 1, 2, 42, 43 }; Cube temper; for (int spot : rotate) { if(spot == 1){ temper = C[spot]; C[spot] = C[spot + 11]; C[spot + 11] = C[spot + 51]; C[spot + 51] = C[spot + 31]; C[spot + 31] = temper; } if(spot == 2){ temper = C[spot]; C[spot] = C[spot + 7]; C[spot + 7] = C[spot + 49]; C[spot + 49] = C[spot + 33]; C[spot + 33] = temper; } if(spot == 43){ temper = C[spot]; C[spot] = C[spot - 2]; C[spot - 2] = C[spot - 6]; C[spot - 6] = C[spot - 4]; C[spot - 4] = temper; } } }
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.