Advertisement
Guest User

Solver222 for 잌갤

a guest
Oct 2nd, 2014
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.73 KB | None | 0 0
  1. #include <cstdio>
  2. #include <vector>
  3. #include <string>
  4. using namespace std;
  5.  
  6. #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)
  7.  
  8. int oReg[7], pReg[7];
  9. int face_arr[24];
  10.  
  11. const int ORIENTATE_LEN = 2187, PERMUTATE_LEN = 5040;
  12. const string ORIENTATE_STR[9] = {"Ri", "R2", "R", "Fi", "F2", "F", "Ui", "U2", "U"};
  13. const string PERMUTATE_STR[5] = {"R2", "F2", "Ui", "U2", "U"};
  14.  
  15. void R() { ROT(1, 5, 6, 2, 1, 2); }
  16. void F() { ROT(0, 4, 5, 1, 1, 2); }
  17. void U() { ROT(0, 1, 2, 3, 0, 0); }
  18. void Ri() { R();R();R(); }
  19. void Fi() { F();F();F(); }
  20. void Ui() { U();U();U(); }
  21. void R2() { R();R(); }
  22. void F2() { F();F(); }
  23. void U2() { U();U(); }
  24.  
  25. void loadO(int o) {
  26.     int i;
  27.     for (i=6; i>=0; --i) {
  28.         oReg[i] = o % 3;
  29.         o /= 3;
  30.     }
  31. }
  32.  
  33. void loadP(int p) {
  34.     int i, s = 720;
  35.     vector<int> temp;
  36.     for (i=0; i<7; ++i) temp.push_back(i);
  37.     for (i=0; i<7; ++i) {
  38.         pReg[i] = temp[p / s];
  39.         temp.erase(temp.begin() + (p / s));
  40.         p %= s;
  41.         if (i != 6) s /= (6 - i);
  42.     }
  43. }
  44.  
  45. int getO() {
  46.     int i, res = 0;
  47.     for (i=0; i<7; ++i) (res *= 3) += oReg[i];
  48.     return res;
  49. }
  50.  
  51. int getP() {
  52.     int i, res = 0, s = 720;
  53.     vector<int> temp;
  54.     for (i=0; i<7; ++i) temp.push_back(i);
  55.     for (i=0; i<7; ++i) {
  56.         int p;
  57.         res += (p = (int)(find(temp.begin(), temp.end(), pReg[i]) - temp.begin())) * s;
  58.         temp.erase(temp.begin() + p);
  59.         if (i != 6) s /= (6 - i);
  60.     }
  61.     return res;
  62. }
  63.  
  64. void initP() {
  65.     loadP(0);
  66. }
  67.  
  68. void mO(int v) { switch (v) {
  69.     case 0: R(); case 1: R(); case 2: R(); break;
  70.     case 3: F(); case 4: F(); case 5: F(); break;
  71.     case 6: U(); case 7: U(); case 8: U(); break;
  72.     default: break;
  73. }}
  74.  
  75. void mP(int v) { switch (v) {
  76.     case 0: R2(); break;
  77.     case 1: F2(); break;
  78.     case 2: U();
  79.     case 3: U();
  80.     case 4: U(); break;
  81.     default: break;
  82. }}
  83.  
  84. string orientate() {
  85.     int i, s, j, choecho = getO(), choechop = getP();
  86.     string res = "";
  87.     vector<int> sol;
  88.     int arr[ORIENTATE_LEN], arr2[ORIENTATE_LEN], arr3[ORIENTATE_LEN];
  89.     for (i=0; i<ORIENTATE_LEN; ++i) arr[i] = -1;
  90.     arr[choecho] = 0;
  91.     for (s = 0; arr[0] == -1; ++s) {
  92.         for (i=0; i<ORIENTATE_LEN; ++i) {
  93.             if (arr[i] == s) {
  94.                 for (j=0; j<9; ++j) {
  95.                     int res;
  96.                     loadO(i);
  97.                     mO(j);
  98.                     res = getO();
  99.                     if (arr[res] == -1) {
  100.                         arr[res] = s + 1;
  101.                         arr2[res] = j;
  102.                         arr3[res] = i;
  103.                     }
  104.                 }
  105.             }
  106.         }
  107.     }
  108.     i = 0;
  109.     while (i != choecho) {
  110.         sol.push_back(arr2[i]);
  111.         i = arr3[i];
  112.     }
  113.     loadO(choecho);
  114.     loadP(choechop);
  115.     while (!sol.empty()) {
  116.         mO(sol.back());
  117.         res += ORIENTATE_STR[sol.back()] + " ";
  118.         sol.pop_back();
  119.     }
  120.     return res;
  121. }
  122.  
  123. string permutate() {
  124.     int i, s, j, choecho = getP();
  125.     string res = "";
  126.     vector<int> sol;
  127.     int arr[PERMUTATE_LEN], arr2[PERMUTATE_LEN], arr3[PERMUTATE_LEN];
  128.     for (i=0; i<PERMUTATE_LEN; ++i) arr[i] = -1;
  129.     arr[choecho] = 0;
  130.     for (s = 0; arr[0] == -1; ++s) {
  131.         for (i=0; i<PERMUTATE_LEN; ++i) {
  132.             if (arr[i] == s) {
  133.                 for (j=0; j<5; ++j) {
  134.                     int res;
  135.                     loadP(i);
  136.                     mP(j);
  137.                     res = getP();
  138.                     if (arr[res] == -1) {
  139.                         arr[res] = s + 1;
  140.                         arr2[res] = j;
  141.                         arr3[res] = i;
  142.                     }
  143.                 }
  144.             }
  145.         }
  146.     }
  147.     i = 0;
  148.     while (i != choecho) {
  149.         sol.push_back(arr2[i]);
  150.         i = arr3[i];
  151.     }
  152.     while (!sol.empty()) {
  153.         res += PERMUTATE_STR[sol.back()] + " ";
  154.         sol.pop_back();
  155.     }
  156.     return res;
  157. }
  158.  
  159. int opposite(int d) { switch (d) {
  160.     case 0: return 5;
  161.     case 1: return 3;
  162.     case 2: return 4;
  163.     case 3: return 1;
  164.     case 4: return 2;
  165.     case 5: return 0;
  166. } return -1; }
  167.  
  168. int permval(int d) { switch (d) {
  169.     case 0: case 1: case 2: return 0;
  170.     case 3: return 1; case 4: return 2; case 5: return 4;
  171. } return -1; }
  172.  
  173. int make_perm(int d) { switch (d) {
  174.     case 0: return 1;
  175.     case 1: return 2;
  176.     case 2: return 0;
  177.     case 3: return 3;
  178.     case 4: return 5;
  179.     case 5: return 6;
  180.     case 6: return 4;
  181. } return d; }
  182.  
  183. void change() {
  184.     int otomata[24] = {
  185.         2, 17, 4, 3, 5, 8,
  186.         1, 9, 12, 0, 13, 16,
  187.         20, 6, 19, 21, 10, 7,
  188.         23, 14, 11, 22, 18, 15
  189.     };
  190.     int i, faces[6];
  191.     faces[3] = face_arr[15]; faces[4] = face_arr[18]; faces[5] = face_arr[22];
  192.     faces[0] = opposite(faces[5]); faces[1] = opposite(faces[3]); faces[2] = opposite(faces[4]);
  193.     for (i=0; i<21; ++i) {
  194.         if (face_arr[otomata[i]] == faces[0] || face_arr[otomata[i]] == faces[5]) {
  195.             oReg[i / 3] = i % 3;
  196.         }
  197.     }
  198.     for (i=0; i<7; ++i) {
  199.         int j, res = 0;
  200.         for (j=0; j<3; ++j) {
  201.             res |= permval((int)(find(faces, faces+6, face_arr[otomata[i * 3 + j]]) - faces));
  202.         }
  203.         pReg[i] = make_perm(res);
  204.     }
  205. }
  206.  
  207. int main() {
  208.     int i, j;
  209.     string orient, perm, face="UFRBLD";
  210.     for (i=0; i<6; ++i) {
  211.         printf("%c Face: ", face[i]);
  212.         for (j=0; j<4; ++j) {
  213.             scanf("%d", &face_arr[(i << 2) | j]);
  214.         }
  215.     }
  216.     change();
  217.     orient = orientate();
  218.     perm = permutate();
  219.     printf("%s%s\n", orient.c_str(), perm.c_str());
  220.     return 0;
  221. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement