Guest User

Untitled

a guest
Jun 19th, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.92 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. using namespace std;
  4. int n,m,stiva[100];
  5. void back(int k);
  6. int valid(int k);
  7. void tipar(int k);
  8. int main ()
  9. {
  10.     ifstream f("fisier.in");
  11.     f>>n;
  12.     back (1);
  13.     f.close();
  14.     return 0;
  15. }
  16.  
  17. void back (int k)
  18. {
  19.     for (int val=1;val<=n;val++)
  20.     {
  21.         stiva[k]=val; //formeaza stiva cu valorile 1->n
  22.    
  23.         if(valid(k))
  24.             if(k==n) // a ajuns la final ?
  25.                 tipar(k);
  26.             else
  27.                 back(k+1); //creste k
  28.     }
  29. }
  30. int valid(int k)
  31. {  
  32.     if(k>1){
  33.     for(int i=1;i<k;i++)
  34.         if( stiva[i]==stiva[k]) return 0;
  35.    
  36.     if(abs(stiva[k]-stiva[k-1])==1) return 0;
  37.     if(abs(stiva[k]-stiva[k-1])==n-1) return 0;
  38.     if((stiva[k]==1&&stiva[k-1]==n)||stiva[k]==n&&stiva[k-1]==1) return 0;
  39.     }
  40.     return 1; //return 1: "de aici nu plec"
  41. }
  42. void tipar(int k)
  43. {
  44.     if(!(abs(stiva[k]-stiva[1])==1 || stiva[n]==n&&stiva[1]==1||stiva[1]==n&&stiva[n]==1)){
  45.     cout<<endl;
  46.     for (int i=1;i<=n;i++)
  47.         cout<<stiva[i]<<" ";
  48.     }
  49. }
Add Comment
Please, Sign In to add comment