Advertisement
Guest User

3 problem

a guest
Mar 25th, 2019
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.61 KB | None | 0 0
  1. #include<iostream>
  2. #include<vector>
  3. #include<queue>
  4. #include<string>
  5. #include <set>
  6. using namespace std;
  7. pair<int,string> minway(vector<int>g)
  8. {
  9.     set<int>w;
  10.     int a = 1, str = 0;
  11.     for (int i = 0; i < 9; i++)
  12.     {
  13.         str += g[i] * a;
  14.         a *= 10;
  15.        
  16.     }
  17.     w.insert(str);
  18.     queue<vector<int>>q;
  19.     queue<int>numofswap;
  20.     queue<string>way;
  21.     q.push(g);
  22.     numofswap.push(0);
  23.     way.push("");
  24.     while (!q.empty())
  25.     {
  26.         g = q.front();
  27.         q.pop();
  28.         int swapnum = numofswap.front();
  29.         numofswap.pop();
  30.         string way1 = way.front();
  31.         way.pop();
  32.         int zeroplace = 0;
  33.         int t = 0;
  34.         for (int i = 0; i < 9; ++i)
  35.             {
  36.                 if ((g[i] == i + 1) || (i == 8 && g[i] == 0))
  37.                     ++t;
  38.                 if (g[i] == 0)
  39.                     zeroplace = i;
  40.             }
  41.         if (t == 9)
  42.         {
  43.             pair<int, string> m = make_pair( swapnum,way1 );
  44.             return m;
  45.  
  46.         }
  47.         else {
  48.  
  49.             if ((zeroplace != 0) && (zeroplace != 3) && (zeroplace != 6))
  50.             {
  51.                 vector<int>h1 = g;
  52.                 swap(h1[zeroplace], h1[zeroplace - 1]);
  53.                 str = 0;
  54.                 a = 1;
  55.                 for (int i = 0; i < 9; i++)
  56.                 {
  57.                     str += h1[i] * a;
  58.                     a *= 10;
  59.                 }
  60.                 if (w.find(str) == w.end())
  61.                 {
  62.                     w.insert(str);
  63.                     q.push(h1);
  64.                     way.push(way1 + 'L');
  65.                     numofswap.push(swapnum + 1);
  66.                 }
  67.  
  68.             }
  69.             if ((zeroplace != 2) && (zeroplace != 5) && (zeroplace != 8))
  70.             {
  71.                 vector<int> h2 = g;
  72.                 swap(h2[zeroplace], h2[zeroplace + 1]);
  73.                 str = 0;
  74.                 a = 1;
  75.                 for (int i = 0; i < 9; i++)
  76.                 {
  77.                     str += h2[i] * a;
  78.                     a *= 10;
  79.                 }
  80.                 if (w.find(str) == w.end())
  81.                 {
  82.                     w.insert(str);
  83.                     q.push(h2);
  84.                     way.push(way1 + 'R');
  85.                     numofswap.push(swapnum + 1);
  86.                 }
  87.  
  88.             }
  89.             if (zeroplace > 2)
  90.             {
  91.                 vector<int>h3 = g;
  92.                 str = 0;
  93.                 a = 1;
  94.                 swap(h3[zeroplace], h3[zeroplace - 3]);
  95.                 for (int i = 0; i < 9; i++)
  96.                 {
  97.                     str += h3[i] * a;
  98.                     a *= 10;
  99.                 }
  100.                 if (w.find(str) == w.end())
  101.                 {
  102.                     w.insert(str);
  103.                     q.push(h3);
  104.                     way.push(way1 + 'U');
  105.                     numofswap.push(swapnum + 1);
  106.                 }
  107.  
  108.             }
  109.             if (zeroplace < 6)
  110.             {
  111.                 vector<int>h4 = g;
  112.                 swap(h4[zeroplace], h4[zeroplace + 3]);
  113.                 str = 0;
  114.                 a = 1;
  115.                 for (int i = 0; i < 9; i++)
  116.                 {
  117.                     str += h4[i] * a;
  118.                     a *= 10;
  119.                 }
  120.                 if (w.find(str) == w.end())
  121.                 {
  122.                     w.insert(str);
  123.                     q.push(h4);
  124.                     way.push(way1 + 'D');
  125.                     numofswap.push(swapnum + 1);
  126.                 }
  127.  
  128.             }
  129.         }
  130.        
  131.  
  132.     }
  133.     pair<int, string>m = make_pair(-1, "");
  134.     return m;
  135.  
  136. }
  137. int main()
  138. {
  139.  
  140.     vector<int>g(9);
  141.     for (int i = 0; i < 9; ++i)
  142.     {
  143.         cin >> g[i];
  144.     }
  145.     pair<int,string> m =minway(g);
  146.     cout << m.first << endl << m.second << endl;
  147.    
  148.     system("pause");
  149.     return 0;
  150. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement