Advertisement
Saleh127

UVA 10422 / Backtrack

Jul 15th, 2022
1,139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.20 KB | None | 0 0
  1. /***
  2.  created: 2022-07-15-19.20.12
  3. ***/
  4.  
  5. #include <bits/stdc++.h>
  6. #include <ext/pb_ds/assoc_container.hpp>
  7. #include <ext/pb_ds/tree_policy.hpp>
  8. using namespace std;
  9. using namespace __gnu_pbds;
  10. template<typename U> using ordered_set=tree<U, null_type,less<U>,rb_tree_tag,tree_order_statistics_node_update>;
  11. #define ll long long
  12. #define get_lost_idiot return 0
  13. #define nl '\n'
  14.  
  15.  
  16. ll dx[8] = {-2, -2, -1, -1, 1, 1, 2, 2};
  17. ll dy[8] = {-1, 1, -2, 2, 2, -2, 1, -1};
  18. char cc[5][6]= {"11111","01111","00 11","00001","00000"};
  19. char a[15][15];
  20. ll cnt=0,exist=0;
  21.  
  22. void dfs(ll i,ll j,ll ans)
  23. {
  24.     if(ans==cnt)
  25.     {
  26.         ll ee=0;
  27.  
  28.         for(ll i1=0; i1<5 && !ee; i1++)
  29.         {
  30.             for(ll j1=0; j1<5 && !ee; j1++)
  31.             {
  32.                 if(a[i1][j1]!=cc[i1][j1])
  33.                 {
  34.                     ee=1;
  35.                     break;
  36.                 }
  37.             }
  38.         }
  39.  
  40.         if(ee==0) exist=1;
  41.  
  42.         if(exist) return ;
  43.         return;
  44.  
  45.     }
  46.  
  47.     if(exist) return ;
  48.  
  49.     for(ll ii=0; ii<8; ii++)
  50.     {
  51.  
  52.         ll xx=i+dx[ii];
  53.         ll yy=j+dy[ii];
  54.  
  55.         if(xx>=0 && xx<5 && yy>=0 && yy<5)
  56.         {
  57.             swap(a[i][j],a[xx][yy]);
  58.             dfs(xx,yy,ans+1);
  59.             swap(a[i][j],a[xx][yy]);
  60.         }
  61.     }
  62. }
  63.  
  64.  
  65.  
  66. int main()
  67. {
  68.     ios_base::sync_with_stdio(0);
  69.     cin.tie(0);
  70.     cout.tie(0);
  71.  
  72.     int tt;
  73.  
  74.     cin>>tt;
  75.  
  76.     cin.ignore();
  77.  
  78.     for(int cs=1; cs<=tt; cs++)
  79.     {
  80.         int i,j,k,l,x,y;
  81.  
  82.         vector<string>in;
  83.         string b;
  84.  
  85.         for(i=0;i<5;i++)
  86.         {
  87.             getline(cin,b);
  88.             in.push_back(b);
  89.         }
  90.  
  91.         for(i=0; i<5; i++)
  92.         {
  93.             for(j=0; j<5; j++)
  94.             {
  95.                 a[i][j]=in[i][j];
  96.  
  97.                 if(in[i][j]==' ')
  98.                 {
  99.                     x=i,y=j;
  100.                 }
  101.             }
  102.         }
  103.  
  104.         exist=0;
  105.  
  106.         for(i=0; i<11; i++)
  107.         {
  108.             cnt=i;
  109.             dfs(x,y,0);
  110.             if(exist) break;
  111.         }
  112.  
  113.         if (exist==0) cout<<"Unsolvable in less than 11 move(s)."<<nl;
  114.         else cout<<"Solvable in "<<cnt<<" move(s)."<<nl;
  115.  
  116.     }
  117.  
  118.  
  119.  
  120.     get_lost_idiot;
  121. }
  122.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement