Advertisement
Guest User

Untitled

a guest
Mar 3rd, 2015
209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.87 KB | None | 0 0
  1. #include <cstdio>
  2. #include <algorithm>
  3. #include <vector>
  4. #include <iostream>
  5. #include <string>
  6. #include <string.h>
  7. #include <cmath>
  8. #include <iomanip>
  9. #include <set>
  10. #include <map>
  11. #include <queue>
  12.  
  13. #define X first
  14. #define Y second
  15. #define ll long long
  16.  
  17. using namespace std;
  18.  
  19. int N;
  20.  
  21. map< vector<short int> , pair<vector<short int>, short int> >mp;
  22. vector< pair<vector<short int>,int> >v[2];
  23. inline int myabs(int x)
  24. {
  25.        if (x<0)
  26.           x=-x;
  27.        return x;      
  28. }
  29. inline int mark(vector<short int>x)
  30. {
  31.        int res=0;
  32.        for (int i=0; i<N*N; i++)
  33.        {
  34.            int xx, yy;
  35.            xx=x[i]/N;
  36.            yy=x[i]%N;
  37.            res+=myabs(xx - i/N) + myabs(yy - i%N);
  38.        }      
  39.        return res;
  40. }
  41. int zz[6][6];
  42.  
  43. inline vector<short int> get()
  44. {
  45.        vector<short int>res;
  46.        for (int i=0; i<N; i++)
  47.            for (int j=0; j<N; j++)
  48.                res.push_back(zz[i][j]);
  49.        return res;      
  50. }
  51. inline void gen(vector<short int>x)
  52. {
  53.        
  54.        for (int i=0; i<N*N; i++)      
  55.            zz[i/N][i%N]=x[i];
  56.        for (int i=0; i<N; i++)
  57.        {
  58.            for (int j=0; j<N; j++)
  59.            {
  60.                if ( zz[i][j]==0 )
  61.                {
  62.                   vector<short int>nw;
  63.                   if (i<N-1)
  64.                   {
  65.                      swap(zz[i][j], zz[i+1][j]);        
  66.                      nw=get();
  67.                      if ( mp.find(nw)==mp.end() )
  68.                      {
  69.                         v[1].push_back( make_pair(nw, mark(nw) ) );
  70.                         mp[nw]=make_pair(x, 1);    
  71.                      }
  72.                      swap(zz[i][j], zz[i+1][j]);
  73.                   }          
  74.                  
  75.                   if (j<N-1)
  76.                   {
  77.                      swap(zz[i][j], zz[i][j+1]);        
  78.                      nw=get();
  79.                      if ( mp.find(nw)==mp.end() )
  80.                      {
  81.                         v[1].push_back( make_pair(nw, mark(nw) ) );
  82.                         mp[nw]=make_pair(x, 3);    
  83.                      }
  84.                      swap(zz[i][j], zz[i][j+1]);
  85.                   }
  86.                  
  87.                   if (i>0)
  88.                   {
  89.                      swap(zz[i][j], zz[i-1][j]);        
  90.                      nw=get();
  91.                      if ( mp.find(nw)==mp.end() )
  92.                      {
  93.                         v[1].push_back( make_pair(nw, mark(nw) ) );
  94.                         mp[nw]=make_pair(x, 2);    
  95.                      }
  96.                      swap(zz[i][j], zz[i-1][j]);
  97.                   }
  98.                  
  99.                   if (j>0)
  100.                   {
  101.                      swap(zz[i][j], zz[i][j-1]);        
  102.                      nw=get();
  103.                      if ( mp.find(nw)==mp.end() )
  104.                      {
  105.                         v[1].push_back( make_pair(nw, mark(nw) ) );
  106.                         mp[nw]=make_pair(x, 4);    
  107.                      }
  108.                      swap(zz[i][j], zz[i][j-1]);
  109.                   }
  110.                }    
  111.            }        
  112.        }
  113. }
  114. bool cmp( pair< vector<short int>, int >A, pair< vector<short int>, int >B )
  115. {
  116.      if ( A.Y<B.Y )
  117.         return 1;
  118.      return 0;    
  119. }
  120. vector<short int>vv;
  121. void build(vector<short int>x)
  122. {
  123.      vector<string>ans;
  124.      pair<vector<short int>, short int>to;
  125.      while ( x!=vv )
  126.      {
  127.            to=mp[x];
  128.            if ( to.Y==1 )
  129.            {
  130.               ans.push_back("DOWN");  
  131.               x=to.X;
  132.               continue;
  133.            }
  134.            if ( to.Y==2 )
  135.            {
  136.               ans.push_back("UP");
  137.               x=to.X;
  138.               continue;    
  139.            }
  140.            if ( to.Y==3 )
  141.            {
  142.               ans.push_back("RIGHT");
  143.               x=to.X;
  144.               continue;    
  145.            }
  146.            if ( to.Y==4 )
  147.            {
  148.               ans.push_back("LEFT");
  149.               x=to.X;
  150.               continue;    
  151.            }
  152.      }  
  153.      cout<<ans.size()<<endl;
  154.      for (int i=ans.size()-1; i>=0; i--)
  155.          cout<<ans[i]<<endl;
  156.      //cin>>ans[0];  
  157. }
  158. int main ()
  159. {
  160.     cin>>N;
  161.    
  162.     for (int i=1; i<=N; i++)
  163.         for (int j=1; j<=N; j++)
  164.         {
  165.             int xx;
  166.             cin>>xx;
  167.             vv.push_back(xx);    
  168.         }
  169.     mp[vv]=make_pair(vv, 1);
  170.     v[1].push_back(make_pair(vv, mark(vv)));
  171.     while (1==1)
  172.     {
  173.           //cout<<v[0].size()<<endl;
  174.           v[1].swap(v[0]);
  175.           v[1].clear();      
  176.           for (int i=0; i<v[0].size(); i++)
  177.           {
  178.               if ( v[0][i].Y==0 )
  179.               {
  180.                  build(v[0][i].X);
  181.                  return 0;    
  182.               }
  183.               gen(v[0][i].X);    
  184.           }
  185.           sort(v[1].begin(), v[1].end(), cmp);
  186.           int ff=v[1].size();
  187.           v[1].resize( min(ff, 100) );
  188.     }
  189.  
  190.     return 0;
  191. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement