Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- int n,st[12];
- bool viz[12];
- void BACK(int top)
- {
- int i;
- if(top==(n+1))
- {
- for(i=1;i<n && abs(st[i]-st[i+1])>1;i++)
- ;
- if(i==n)
- {
- for(int i=1;i<=n;i++)
- cout<<st[i]<<" ";
- cout<<"\n";
- }
- }
- else for(int i=1;i<=n;i++)
- if(!viz[i])
- {
- st[top]=i;
- viz[i]=true;
- BACK(top+1);
- viz[i]=false;
- }
- }
- int main()
- {
- cin>>n;
- BACK(1);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment