forked from rishabhgarg25699/Competitive-Programming
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Rubik's_Cube.cpp
146 lines (138 loc) · 2.72 KB
/
Rubik's_Cube.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
// six faces of cube FRONT(F), BACK(B), UP(U), DOWN(U), LEFT(L) and RIGHT(R)
// R: 90 degree clockwise rotation of the right face
// R': 90 degree anticlockwise rotation of the right face
// R2: 180 degree rotation of the right face
// BLF2: rotating the back face by 90 degrees clockwise, then the left face
// by 90 degrees clockwise followed by the front face by 180 degrees.
/* To find out the number of times the sequence should
be applied repeatedly to the cube to get back the original cube*/
#include<bits/stdc++.h>
using namespace std;
int state[55];
void rotate(int a, int b, int c, int d){
int temp = state[d];
state[d] = state[c];
state[c] = state[b];
state[b] = state[a];
state[a] = temp;
}
long long gcd(long long a, long long b)
{
if (a == 0)
return b;
if (b == 0)
return a;
if (a == 1 or b == 1)
return 1;
if (a == b)
return a;
if (a > b)
return gcd(b, a%b);
else
return gcd(a, b%a);
}
void Permute(char code){
switch(code){
case 'U':{
rotate(19, 21, 27, 25);
rotate(20, 24, 26, 22);
rotate(7, 28, 39, 18);
rotate(8, 31, 38, 15);
rotate(9, 34, 37, 12);
break;
}
case 'D':{
rotate(46, 48, 54, 52);
rotate(47, 51, 53, 49);
rotate(43, 36, 3, 10);
rotate(44, 33, 2, 13);
rotate(45, 30, 1, 16);
break;
}
case 'F':{
rotate(37, 39, 45, 43);
rotate(38, 42, 44, 40);
rotate(25, 34, 48, 16);
rotate(26, 35, 47, 17);
rotate(27, 36, 46, 18);
break;
}
case 'B':{
rotate(1, 3, 9, 7);
rotate(2, 6, 8, 4);
rotate(19, 10, 54, 28);
rotate(20, 11, 53, 29);
rotate(21, 12, 52, 30);
break;
}
case 'L':{
rotate(10, 12, 18, 16);
rotate(11, 15, 17, 13);
rotate(1, 19, 37, 46);
rotate(4, 22, 40, 49);
rotate(7, 25, 43, 52);
break;
}
case 'R':{
rotate(28, 30, 36, 34);
rotate(29, 33, 35, 31);
rotate(48, 39, 21, 3);
rotate(51, 42, 24, 6);
rotate(54, 45, 27, 9);
break;
}
}
}
long long lcm(long long a, long long b){
if (a == 1)
return b;
if (b == 1)
return a;
if (a == b)
return a;
return (a/gcd(a, b))*b;
}
int main ()
{
int i, j, temp;
long long moves, cycle_len;
char input[1010], code;
bool seen[55];
for (i=1; i<=54; i++){
state[i] = i;
seen[i] = false;
}
scanf("%s", input);
i = 0;
while (input[i] != '\0'){
code = input[i];
if (input[i+1] != '\'' and input[i+1] != '2'){
Permute(code);
i++;
}
else{
Permute(code);
Permute(code);
if (input[i+1] == '\'')
Permute(code);
i += 2;
}
}
moves = 1;
for (i=1; i<=54; i++){
if (seen[i])
continue;
seen[i] = true;
cycle_len = 1;
temp = i;
j = state[i];
while (temp != j){
j = state[j];
seen[j] = true;
cycle_len++;
}
moves = lcm(moves, cycle_len);
}
printf("%lld\n", moves);
return 0;
}