tanasaradu

Untitled

Nov 23rd, 2017
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.56 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. int n,st[12];
  4. bool viz[12];
  5. void BACK(int top)
  6. {
  7. int i;
  8. if(top==(n+1))
  9. {
  10. for(i=1;i<n && abs(st[i]-st[i+1])>1;i++)
  11. ;
  12. if(i==n)
  13. {
  14. for(int i=1;i<=n;i++)
  15. cout<<st[i]<<" ";
  16. cout<<"\n";
  17. }
  18. }
  19. else for(int i=1;i<=n;i++)
  20. if(!viz[i])
  21. {
  22. st[top]=i;
  23. viz[i]=true;
  24. BACK(top+1);
  25. viz[i]=false;
  26. }
  27. }
  28. int main()
  29. {
  30. cin>>n;
  31. BACK(1);
  32. return 0;
  33. }
Advertisement
Add Comment
Please, Sign In to add comment