tungggg

Backtrack_Permutation

Feb 7th, 2022
28
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.56 KB | None | 0 0
  1. #include<iostream>
  2. using namespace std;
  3. int n;
  4. int a[100];
  5. bool used[100]={false};
  6. void show(){
  7. for(int i=1;i<=n;i++){
  8. cout<<a[i];
  9. }
  10. }
  11. void backtracking(int pos){
  12. if(pos==n+1){
  13. show();
  14. cout<<endl;
  15. return ;
  16. }
  17. else{
  18. for(int i=1;i<=n;i++){
  19. if (used[i]==false){
  20. used[i]=true;
  21. a[pos]=i;
  22. backtracking(pos+1);
  23. used[i]=false;
  24. }
  25. }
  26. }
  27. }
  28. int main(){
  29. cin>>n;
  30. backtracking(1);
  31. return 0;
  32. }
Advertisement
Add Comment
Please, Sign In to add comment