Advertisement
a53

PERMUTARI_ITERATIV

a53
Nov 22nd, 2017
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.86 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3. #define NR 10
  4. int x[NR],n,k;
  5.  
  6. void init(int k)
  7. {
  8. x[k]=0;
  9. }
  10.  
  11. int succesor(int k)
  12. {
  13. if(x[k]<n)
  14. return 1;
  15. else
  16. return 0;
  17. }
  18.  
  19. int continuare(int k)
  20. {
  21. for(int i=1;i<=k-1;++i)
  22. if(x[k]==x[i])
  23. return 0;
  24. return 1;
  25. }
  26.  
  27. int solutie(int k)
  28. {
  29. if(k==n+1)
  30. return 1;
  31. else
  32. return 0;
  33. }
  34.  
  35. void afisare()
  36. {
  37. for(int i=1;i<=n;++i)
  38. cout<<x[i]<<' ';
  39. cout<<'\n';
  40. }
  41.  
  42. void backtracking()
  43. {
  44. int k=1;
  45. init(1);
  46. while(k)
  47. if(solutie(k))
  48. afisare(),--k;
  49. else
  50. if(succesor(k))
  51. {
  52. ++x[k];
  53. if (continuare(k))
  54. ++k;
  55. }
  56. else
  57. init(k),--k;
  58. }
  59.  
  60. int main()
  61. {
  62. cout<<"n= "; cin>>n;
  63. backtracking();
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement