SHARE
TWEET

mitu

a guest Sep 23rd, 2019 75 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. ifstream in("shuffle.in");
  6. ofstream out("shuffle.out");
  7.  
  8. int v[10],sol[10],n;
  9.  
  10. int valid(int k)
  11. {
  12.     for(int i=1; i<k; i++)
  13.     {
  14.         if(sol[k]==sol[i])
  15.             return 0;
  16.     }
  17.     int ok=0,i=1;
  18.     while(!ok)
  19.     {
  20.         if(sol[k]==v[i])
  21.             ok=1;
  22.         else i++;
  23.     }
  24.     if(i!=1)
  25.     {
  26.     if(sol[k-1]==v[i-1] || sol[k-1]==v[i+1] || sol[k+1]==v[i-1] || sol[k+1]==v[i+1])
  27.         return 0;
  28.     }
  29.     if(sol[k+1]==v[i-1] || sol[k+1]==v[i+1])
  30.         return 0;
  31.     return 1;
  32. }
  33.  
  34. void back(int k)
  35. {
  36.     for(int i=1; i<=n; i++)
  37.     {
  38.         sol[k]=i;
  39.         if(valid(k))
  40.             if(k==n)
  41.             {
  42.                 for(int j=1; j<=n; j++)
  43.                     cout<<sol[j]<<" ";
  44.                 cout<<'\n';
  45.             }
  46.             else back(k+1);
  47.     }
  48. }
  49.  
  50. int main()
  51. {
  52.     cin>>n;
  53.     for(int i=1; i<=n; i++)
  54.         cin>>v[i];
  55.     back(1);
  56.     return 0;
  57. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top