Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cstdio>
- #include <vector>
- #include <string>
- using namespace std;
- #define ROT(a,b,c,d,e,f) do{int xx=oReg[a];oReg[a]=oReg[b]+e;oReg[b]=oReg[c]+f;oReg[c]=oReg[d]+e;oReg[d]=xx+f;xx=pReg[a];pReg[a]=pReg[b];pReg[b]=pReg[c];pReg[c]=pReg[d];pReg[d]=xx;xx=7;while(xx--)oReg[xx]%=3;}while(0)
- int oReg[7], pReg[7];
- int face_arr[24];
- const int ORIENTATE_LEN = 2187, PERMUTATE_LEN = 5040;
- const string ORIENTATE_STR[9] = {"Ri", "R2", "R", "Fi", "F2", "F", "Ui", "U2", "U"};
- const string PERMUTATE_STR[5] = {"R2", "F2", "Ui", "U2", "U"};
- void R() { ROT(1, 5, 6, 2, 1, 2); }
- void F() { ROT(0, 4, 5, 1, 1, 2); }
- void U() { ROT(0, 1, 2, 3, 0, 0); }
- void Ri() { R();R();R(); }
- void Fi() { F();F();F(); }
- void Ui() { U();U();U(); }
- void R2() { R();R(); }
- void F2() { F();F(); }
- void U2() { U();U(); }
- void loadO(int o) {
- int i;
- for (i=6; i>=0; --i) {
- oReg[i] = o % 3;
- o /= 3;
- }
- }
- void loadP(int p) {
- int i, s = 720;
- vector<int> temp;
- for (i=0; i<7; ++i) temp.push_back(i);
- for (i=0; i<7; ++i) {
- pReg[i] = temp[p / s];
- temp.erase(temp.begin() + (p / s));
- p %= s;
- if (i != 6) s /= (6 - i);
- }
- }
- int getO() {
- int i, res = 0;
- for (i=0; i<7; ++i) (res *= 3) += oReg[i];
- return res;
- }
- int getP() {
- int i, res = 0, s = 720;
- vector<int> temp;
- for (i=0; i<7; ++i) temp.push_back(i);
- for (i=0; i<7; ++i) {
- int p;
- res += (p = (int)(find(temp.begin(), temp.end(), pReg[i]) - temp.begin())) * s;
- temp.erase(temp.begin() + p);
- if (i != 6) s /= (6 - i);
- }
- return res;
- }
- void initP() {
- loadP(0);
- }
- void mO(int v) { switch (v) {
- case 0: R(); case 1: R(); case 2: R(); break;
- case 3: F(); case 4: F(); case 5: F(); break;
- case 6: U(); case 7: U(); case 8: U(); break;
- default: break;
- }}
- void mP(int v) { switch (v) {
- case 0: R2(); break;
- case 1: F2(); break;
- case 2: U();
- case 3: U();
- case 4: U(); break;
- default: break;
- }}
- string orientate() {
- int i, s, j, choecho = getO(), choechop = getP();
- string res = "";
- vector<int> sol;
- int arr[ORIENTATE_LEN], arr2[ORIENTATE_LEN], arr3[ORIENTATE_LEN];
- for (i=0; i<ORIENTATE_LEN; ++i) arr[i] = -1;
- arr[choecho] = 0;
- for (s = 0; arr[0] == -1; ++s) {
- for (i=0; i<ORIENTATE_LEN; ++i) {
- if (arr[i] == s) {
- for (j=0; j<9; ++j) {
- int res;
- loadO(i);
- mO(j);
- res = getO();
- if (arr[res] == -1) {
- arr[res] = s + 1;
- arr2[res] = j;
- arr3[res] = i;
- }
- }
- }
- }
- }
- i = 0;
- while (i != choecho) {
- sol.push_back(arr2[i]);
- i = arr3[i];
- }
- loadO(choecho);
- loadP(choechop);
- while (!sol.empty()) {
- mO(sol.back());
- res += ORIENTATE_STR[sol.back()] + " ";
- sol.pop_back();
- }
- return res;
- }
- string permutate() {
- int i, s, j, choecho = getP();
- string res = "";
- vector<int> sol;
- int arr[PERMUTATE_LEN], arr2[PERMUTATE_LEN], arr3[PERMUTATE_LEN];
- for (i=0; i<PERMUTATE_LEN; ++i) arr[i] = -1;
- arr[choecho] = 0;
- for (s = 0; arr[0] == -1; ++s) {
- for (i=0; i<PERMUTATE_LEN; ++i) {
- if (arr[i] == s) {
- for (j=0; j<5; ++j) {
- int res;
- loadP(i);
- mP(j);
- res = getP();
- if (arr[res] == -1) {
- arr[res] = s + 1;
- arr2[res] = j;
- arr3[res] = i;
- }
- }
- }
- }
- }
- i = 0;
- while (i != choecho) {
- sol.push_back(arr2[i]);
- i = arr3[i];
- }
- while (!sol.empty()) {
- res += PERMUTATE_STR[sol.back()] + " ";
- sol.pop_back();
- }
- return res;
- }
- int opposite(int d) { switch (d) {
- case 0: return 5;
- case 1: return 3;
- case 2: return 4;
- case 3: return 1;
- case 4: return 2;
- case 5: return 0;
- } return -1; }
- int permval(int d) { switch (d) {
- case 0: case 1: case 2: return 0;
- case 3: return 1; case 4: return 2; case 5: return 4;
- } return -1; }
- int make_perm(int d) { switch (d) {
- case 0: return 1;
- case 1: return 2;
- case 2: return 0;
- case 3: return 3;
- case 4: return 5;
- case 5: return 6;
- case 6: return 4;
- } return d; }
- void change() {
- int otomata[24] = {
- 2, 17, 4, 3, 5, 8,
- 1, 9, 12, 0, 13, 16,
- 20, 6, 19, 21, 10, 7,
- 23, 14, 11, 22, 18, 15
- };
- int i, faces[6];
- faces[3] = face_arr[15]; faces[4] = face_arr[18]; faces[5] = face_arr[22];
- faces[0] = opposite(faces[5]); faces[1] = opposite(faces[3]); faces[2] = opposite(faces[4]);
- for (i=0; i<21; ++i) {
- if (face_arr[otomata[i]] == faces[0] || face_arr[otomata[i]] == faces[5]) {
- oReg[i / 3] = i % 3;
- }
- }
- for (i=0; i<7; ++i) {
- int j, res = 0;
- for (j=0; j<3; ++j) {
- res |= permval((int)(find(faces, faces+6, face_arr[otomata[i * 3 + j]]) - faces));
- }
- pReg[i] = make_perm(res);
- }
- }
- int main() {
- int i, j;
- string orient, perm, face="UFRBLD";
- for (i=0; i<6; ++i) {
- printf("%c Face: ", face[i]);
- for (j=0; j<4; ++j) {
- scanf("%d", &face_arr[(i << 2) | j]);
- }
- }
- change();
- orient = orientate();
- perm = permutate();
- printf("%s%s\n", orient.c_str(), perm.c_str());
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement