#include #include #define MAX_N (3123) typedef char Face[9]; Face cube[6], cube_0[6]; char *letters = "FLBRUDflbrud"; int char2index(char c) { return strchr(letters, c) - letters; } void initialize_cubes() { int i, f; for (f = 0; f < 6; ++f) { for (i = 0; i < 9; ++i) { cube [f][i] = letters[f]; cube_0[f][i] = letters[f]; } } } void copy_face_complement(char *to, char *from) { int i; for (i = 0; i < 9; ++i) { to[i] = from[8-i]; } } void rotate_face(char *f, int repeat) { int temp; while (repeat-- > 0) { temp = f[0]; f[0] = f[6]; f[6] = f[8]; f[8] = f[2]; f[2] = temp; temp = f[1]; f[1] = f[3]; f[3] = f[7]; f[7] = f[5]; f[5] = temp; } } char temp_line[3]; Face temp_face; void movimento_FLBR(int repeat) { while (repeat-- > 0) { rotate_face(cube[char2index('U')], 1); rotate_face(cube[char2index('D')], 3); strncpy(temp_face, cube[char2index('F')], 9); strncpy(cube[char2index('F')], cube[char2index('R')], 9); strncpy(cube[char2index('R')], cube[char2index('B')], 9); strncpy(cube[char2index('B')], cube[char2index('L')], 9); strncpy(cube[char2index('L')], temp_face, 9); } } void movimento_FUBD(int repeat) { while (repeat-- > 0) { rotate_face(cube[char2index('R')], 1); rotate_face(cube[char2index('L')], 3); strncpy(temp_face, cube[char2index('F')], 9); strncpy(cube[char2index('F')], cube[char2index('D')], 9); copy_face_complement(cube[char2index('D')], cube[char2index('B')]); copy_face_complement(cube[char2index('B')], cube[char2index('U')]); strncpy(cube[char2index('U')], temp_face, 9); } } void rotate(char f) { int i, i_f, repeat; i_f = char2index(f); if (i_f < 6) { repeat = 1; } else { repeat = 3; i_f -= 6; f = letters[i_f]; } for (i = 0; i < repeat; ++i) { switch (f) { case 'U': rotate_face(cube[char2index('U')], 1); strncpy(temp_line, cube[char2index('R')], 3); strncpy(cube[char2index('R')], cube[char2index('B')], 3); strncpy(cube[char2index('B')], cube[char2index('L')], 3); strncpy(cube[char2index('L')], cube[char2index('F')], 3); strncpy(cube[char2index('F')], temp_line, 3); break; case 'F': movimento_FUBD(1); rotate('U'); movimento_FUBD(3); break; case 'D': movimento_FUBD(2); rotate('U'); movimento_FUBD(2); break; case 'B': movimento_FUBD(3); rotate('U'); movimento_FUBD(1); break; case 'R': movimento_FLBR(1); rotate('F'); movimento_FLBR(3); break; case 'L': movimento_FLBR(3); rotate('F'); movimento_FLBR(1); break; } } } void movimenta(int n, char *mov) { int i; for (i = 0; i < n; ++i) { rotate(mov[i]); } } void translate(char *to, char *from) { int i_to, i_from; i_to = i_from = 0; while (from[i_from] != '\0') { switch (from[i_from]) { case '*': to[i_to-1] =(to[i_to-1] | 0x20); //make lower case break; case '2': to[i_to]=to[i_to-1]; ++i_to; break; default: to[i_to++] = from[i_from]; break; } ++i_from; } to[i_to] = '\0'; } char mov[MAX_N+1]; char s[MAX_N+1]; int main(int argc, char *noargs[]) { int n, conta; for (;;) { if (scanf("%s", s) == EOF) break; initialize_cubes(); translate(mov, s); n = strlen(mov); conta = 0; do { ++conta; movimenta(n, mov); } while (strncmp((char *) cube, (char *) cube_0, 6 * 3 * 3) != 0); printf("%d\n", conta); } return 0; }