Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cstdio>
- #include <algorithm>
- #include <vector>
- #include <iostream>
- #include <string>
- #include <string.h>
- #include <cmath>
- #include <iomanip>
- #include <set>
- #include <map>
- #include <queue>
- #define X first
- #define Y second
- #define ll long long
- using namespace std;
- int N;
- map< vector<short int> , pair<vector<short int>, short int> >mp;
- vector< pair<vector<short int>,int> >v[2];
- inline int myabs(int x)
- {
- if (x<0)
- x=-x;
- return x;
- }
- inline int mark(vector<short int>x)
- {
- int res=0;
- for (int i=0; i<N*N; i++)
- {
- int xx, yy;
- xx=x[i]/N;
- yy=x[i]%N;
- res+=myabs(xx - i/N) + myabs(yy - i%N);
- }
- return res;
- }
- int zz[6][6];
- inline vector<short int> get()
- {
- vector<short int>res;
- for (int i=0; i<N; i++)
- for (int j=0; j<N; j++)
- res.push_back(zz[i][j]);
- return res;
- }
- inline void gen(vector<short int>x)
- {
- for (int i=0; i<N*N; i++)
- zz[i/N][i%N]=x[i];
- for (int i=0; i<N; i++)
- {
- for (int j=0; j<N; j++)
- {
- if ( zz[i][j]==0 )
- {
- vector<short int>nw;
- if (i<N-1)
- {
- swap(zz[i][j], zz[i+1][j]);
- nw=get();
- if ( mp.find(nw)==mp.end() )
- {
- v[1].push_back( make_pair(nw, mark(nw) ) );
- mp[nw]=make_pair(x, 1);
- }
- swap(zz[i][j], zz[i+1][j]);
- }
- if (j<N-1)
- {
- swap(zz[i][j], zz[i][j+1]);
- nw=get();
- if ( mp.find(nw)==mp.end() )
- {
- v[1].push_back( make_pair(nw, mark(nw) ) );
- mp[nw]=make_pair(x, 3);
- }
- swap(zz[i][j], zz[i][j+1]);
- }
- if (i>0)
- {
- swap(zz[i][j], zz[i-1][j]);
- nw=get();
- if ( mp.find(nw)==mp.end() )
- {
- v[1].push_back( make_pair(nw, mark(nw) ) );
- mp[nw]=make_pair(x, 2);
- }
- swap(zz[i][j], zz[i-1][j]);
- }
- if (j>0)
- {
- swap(zz[i][j], zz[i][j-1]);
- nw=get();
- if ( mp.find(nw)==mp.end() )
- {
- v[1].push_back( make_pair(nw, mark(nw) ) );
- mp[nw]=make_pair(x, 4);
- }
- swap(zz[i][j], zz[i][j-1]);
- }
- }
- }
- }
- }
- bool cmp( pair< vector<short int>, int >A, pair< vector<short int>, int >B )
- {
- if ( A.Y<B.Y )
- return 1;
- return 0;
- }
- vector<short int>vv;
- void build(vector<short int>x)
- {
- vector<string>ans;
- pair<vector<short int>, short int>to;
- while ( x!=vv )
- {
- to=mp[x];
- if ( to.Y==1 )
- {
- ans.push_back("DOWN");
- x=to.X;
- continue;
- }
- if ( to.Y==2 )
- {
- ans.push_back("UP");
- x=to.X;
- continue;
- }
- if ( to.Y==3 )
- {
- ans.push_back("RIGHT");
- x=to.X;
- continue;
- }
- if ( to.Y==4 )
- {
- ans.push_back("LEFT");
- x=to.X;
- continue;
- }
- }
- cout<<ans.size()<<endl;
- for (int i=ans.size()-1; i>=0; i--)
- cout<<ans[i]<<endl;
- //cin>>ans[0];
- }
- int main ()
- {
- cin>>N;
- for (int i=1; i<=N; i++)
- for (int j=1; j<=N; j++)
- {
- int xx;
- cin>>xx;
- vv.push_back(xx);
- }
- mp[vv]=make_pair(vv, 1);
- v[1].push_back(make_pair(vv, mark(vv)));
- while (1==1)
- {
- //cout<<v[0].size()<<endl;
- v[1].swap(v[0]);
- v[1].clear();
- for (int i=0; i<v[0].size(); i++)
- {
- if ( v[0][i].Y==0 )
- {
- build(v[0][i].X);
- return 0;
- }
- gen(v[0][i].X);
- }
- sort(v[1].begin(), v[1].end(), cmp);
- int ff=v[1].size();
- v[1].resize( min(ff, 100) );
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement