C,1715 1709 1686 1336 1328字节
感谢@KevinCruijssen,节省了25个字节!
到目前为止,还没有答案,所以我决定提出自己的解决方案。
#define m(a,b,c,d)t[c][d]=r[a][b];
#define n(a)m(a,0,a,2)m(a,1,a,5)m(a,2,a,8)m(a,3,a,1)m(a,5,a,7)m(a,6,a,0)m(a,7,a,3)m(a,8,a,6)
#define y memcpy
typedef char R[6][9];R t;F(R r){y(t,r,54);n(0)m(4,6,1,0)m(4,7,1,3)m(4,8,1,6)m(1,0,5,0)m(1,3,5,1)m(1,6,5,2)m(5,0,3,2)m(5,1,3,5)m(5,2,3,8)m(3,2,4,6)m(3,5,4,7)m(3,8,4,8)y(r,t,54);}B(R r){y(t,r,54);n(2)m(1,2,4,0)m(1,5,4,1)m(1,8,4,2)m(3,0,5,6)m(3,3,5,7)m(3,6,5,8)m(4,0,3,6)m(4,1,3,3)m(4,2,3,0)m(5,6,1,8)m(5,7,1,5)m(5,8,1,2)y(r,t,54);}L(R r){y(t,r,54);n(3)m(0,0,5,0)m(0,3,5,3)m(0,6,5,6)m(2,2,4,6)m(2,5,4,3)m(2,8,4,0)m(4,0,0,0)m(4,3,0,3)m(4,6,0,6)m(5,0,2,8)m(5,3,2,5)m(5,6,2,2)y(r,t,54);}E(R r){y(t,r,54);n(1)m(0,2,4,2)m(0,5,4,5)m(0,8,4,8)m(5,2,0,2)m(5,5,0,5)m(5,8,0,8)m(4,2,2,6)m(4,5,2,3)m(4,8,2,0)m(2,0,5,8)m(2,3,5,5)m(2,6,5,2)y(r,t,54);}U(R r){y(t,r,54);n(4)m(0,0,3,0)m(0,1,3,1)m(0,2,3,2)m(1,0,0,0)m(1,1,0,1)m(1,2,0,2)m(2,0,1,0)m(2,1,1,1)m(2,2,1,2)m(3,0,2,0)m(3,1,2,1)m(3,2,2,2)y(r,t,54);}D(R r){y(t,r,54);n(5)m(0,6,1,6)m(0,7,1,7)m(0,8,1,8)m(1,6,2,6)m(1,7,2,7)m(1,8,2,8)m(2,6,3,6)m(2,7,3,7)m(2,8,3,8)m(3,6,0,6)m(3,7,0,7)m(3,8,0,8)y(r,t,54);}a,b,c,d,e,o,p,l;f(char*z,R s){char c[6]="RGOBYW";for(;b<7;b++)for(a=0;a<10;)s[b][a++]=c[b];for(l=strlen(z);l-->0;){d=*z++;if(d-32){e=*z++;if(*z++-32)*z++;o=e-50?e-39?1:3:2;for(p=0;p++<o;)d-70?d-66?d-76?d-82?d-85?D(s):U(s):E(s):L(s):B(s):F(s);}}}
在线尝试!
非高尔夫旧版本:
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <ctype.h>
typedef char Tile;
typedef Tile Face[10];
typedef Face Rubik[7];
void main_loop(Rubik);
void rubik_init(Rubik);
void rubik_print(Rubik);
void rotate(Rubik, char, char);
void print_tile(Rubik, int, int);
void F(Rubik);
void B(Rubik);
void L(Rubik);
void R(Rubik);
void U(Rubik);
void D(Rubik);
#define move(a, b, c, d) \
temp[c][d] = rubik[a][b]
#define move_f(a) \
move(a, 1, a, 3); \
move(a, 2, a, 6); \
move(a, 3, a, 9); \
move(a, 4, a, 2); \
move(a, 6, a, 8); \
move(a, 7, a, 1); \
move(a, 8, a, 4); \
move(a, 9, a, 7)
void F(Rubik rubik) {
Rubik temp;
memcpy(temp, rubik, sizeof(Rubik));
move_f(1);
move(5, 7, 2, 1);
move(5, 8, 2, 4);
move(5, 9, 2, 7);
move(2, 1, 6, 1);
move(2, 4, 6, 2);
move(2, 7, 6, 3);
move(6, 1, 4, 3);
move(6, 2, 4, 6);
move(6, 3, 4, 9);
move(4, 3, 5, 7);
move(4, 6, 5, 8);
move(4, 9, 5, 9);
memcpy(rubik, temp, sizeof(Rubik));
}
void B(Rubik rubik) {
Rubik temp;
memcpy(temp, rubik, sizeof(Rubik));
move_f(3);
move(2, 3, 5, 1);
move(2, 6, 5, 2);
move(2, 9, 5, 3);
move(4, 1, 6, 7);
move(4, 4, 6, 8);
move(4, 7, 6, 9);
move(5, 1, 4, 7);
move(5, 2, 4, 4);
move(5, 3, 4, 1);
move(6, 7, 2, 9);
move(6, 8, 2, 6);
move(6, 9, 2, 3);
memcpy(rubik, temp, sizeof(Rubik));
}
void L(Rubik rubik) {
Rubik temp;
memcpy(temp, rubik, sizeof(Rubik));
move_f(4);
move(1, 1, 6, 1);
move(1, 4, 6, 4);
move(1, 7, 6, 7);
move(3, 3, 5, 7);
move(3, 6, 5, 4);
move(3, 9, 5, 1);
move(5, 1, 1, 1);
move(5, 4, 1, 4);
move(5, 7, 1, 7);
move(6, 1, 3, 9);
move(6, 4, 3, 6);
move(6, 7, 3, 3);
memcpy(rubik, temp, sizeof(Rubik));
}
void R(Rubik rubik) {
Rubik temp;
memcpy(temp, rubik, sizeof(Rubik));
move_f(2);
move(1, 3, 5, 3);
move(1, 6, 5, 6);
move(1, 9, 5, 9);
move(6, 3, 1, 3);
move(6, 6, 1, 6);
move(6, 9, 1, 9);
move(5, 3, 3, 7);
move(5, 6, 3, 4);
move(5, 9, 3, 1);
move(3, 1, 6, 9);
move(3, 4, 6, 6);
move(3, 7, 6, 3);
memcpy(rubik, temp, sizeof(Rubik));
}
void U(Rubik rubik) {
Rubik temp;
memcpy(temp, rubik, sizeof(Rubik));
move_f(5);
move(1, 1, 4, 1);
move(1, 2, 4, 2);
move(1, 3, 4, 3);
move(2, 1, 1, 1);
move(2, 2, 1, 2);
move(2, 3, 1, 3);
move(3, 1, 2, 1);
move(3, 2, 2, 2);
move(3, 3, 2, 3);
move(4, 1, 3, 1);
move(4, 2, 3, 2);
move(4, 3, 3, 3);
memcpy(rubik, temp, sizeof(Rubik));
}
void D(Rubik rubik) {
Rubik temp;
memcpy(temp, rubik, sizeof(Rubik));
move_f(6);
move(1, 7, 2, 7);
move(1, 8, 2, 8);
move(1, 9, 2, 9);
move(2, 7, 3, 7);
move(2, 8, 3, 8);
move(2, 9, 3, 9);
move(3, 7, 4, 7);
move(3, 8, 4, 8);
move(3, 9, 4, 9);
move(4, 7, 1, 7);
move(4, 8, 1, 8);
move(4, 9, 1, 9);
memcpy(rubik, temp, sizeof(Rubik));
}
int main(int argc, char *argv[]) {
Rubik rubik;
rubik_init(rubik);
main_loop(rubik);
return 0;
}
void main_loop(Rubik rubik) {
char a, b;
for (;;) {
a=toupper(getchar());
if (a == 'Q') break;
if (a != 10) {
b=toupper(getchar());
if (b != 10) getchar();
rotate(rubik, a, b);
}
rubik_print(rubik);
}
}
void rubik_init(Rubik rubik) {
int i,n;
char c[7] = " RGOBYW";
for (n=1; n<=7; n++)
for (i=1; i<=10; i++)
rubik[n][i] = c[n];
}
void rotate(Rubik rubik, char a, char b){
int i = b == '2' ? 2 : b == '\'' || b == toupper('i') ? 3 : 1;
int j;
for (j=0; j<i; j++)
if (a == 'F') F(rubik);
else if (a == 'B') B(rubik);
else if (a == 'L') L(rubik);
else if (a == 'R') R(rubik);
else if (a == 'U') U(rubik);
else if (a == 'D') D(rubik);
else;
}
void rubik_print(Rubik rubik) {
int i,j,k;
for (i=1; i<=9; i++)
if (i%3==0) {
print_tile(rubik,5,i);
printf("\n");
}
else if (i%3==1) {
printf(" ");
print_tile(rubik,5,i);
}
else
print_tile(rubik,5,i);
printf("\n");
for (k=1; k<=3; k++) {
for (i=3; i<=6; i++) {
for (j=k*3-2; j<=k*3; j++)
print_tile(rubik, i%4+1, j);
printf(" ");
}
printf("\n");
}
printf("\n");
for (i=1; i<=9; i++)
if (i%3==0) {
print_tile(rubik, 6, i);
printf("\n");
}
else if (i%3==1) {
printf(" ");
print_tile(rubik, 6, i);
}
else
print_tile(rubik, 6, i);
}
void print_tile(Rubik rubik, int a, int b) {
switch (rubik[a][b]) {
case 'R':
printf("R");
break;
case 'O':
printf("O");
break;
case 'B':
printf("B");
break;
case 'G':
printf("G");
break;
case 'Y':
printf("Y");
break;
case 'W':
printf("W");
break;
default:
exit(1);
}
}