Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- using namespace std;
- int n,m,stiva[100];
- void back(int k);
- int valid(int k);
- void tipar(int k);
- int main ()
- {
- ifstream f("fisier.in");
- f>>n;
- back (1);
- f.close();
- return 0;
- }
- void back (int k)
- {
- for (int val=1;val<=n;val++)
- {
- stiva[k]=val; //formeaza stiva cu valorile 1->n
- if(valid(k))
- if(k==n) // a ajuns la final ?
- tipar(k);
- else
- back(k+1); //creste k
- }
- }
- int valid(int k)
- {
- if(k>1){
- for(int i=1;i<k;i++)
- if( stiva[i]==stiva[k]) return 0;
- if(abs(stiva[k]-stiva[k-1])==1) return 0;
- if(abs(stiva[k]-stiva[k-1])==n-1) return 0;
- if((stiva[k]==1&&stiva[k-1]==n)||stiva[k]==n&&stiva[k-1]==1) return 0;
- }
- return 1; //return 1: "de aici nu plec"
- }
- void tipar(int k)
- {
- if(!(abs(stiva[k]-stiva[1])==1 || stiva[n]==n&&stiva[1]==1||stiva[1]==n&&stiva[n]==1)){
- cout<<endl;
- for (int i=1;i<=n;i++)
- cout<<stiva[i]<<" ";
- }
- }
Add Comment
Please, Sign In to add comment