Advertisement
Guest User

Untitled

a guest
May 29th, 2017
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.34 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. class Backtracking
  6. {
  7. protected:
  8. int n,st[20],top;
  9. int vmin,vmax; /// valorile minime si maxime de pe stiva
  10. public:
  11. Backtracking(int _n,int _top,int _vmin,int _vmax)
  12. : n(_n),top(_top),vmin(_vmin),vmax(_vmax)
  13. {}
  14. /// metoda este virtuala pentru a fi polimorfica
  15. /// (redefinita in clasele derivate)
  16. virtual bool Valid()
  17. {
  18. return true;
  19. }
  20. void Back()
  21. {
  22. bool cand;
  23. st[top]=vmin-1;
  24. while (top>0)
  25. {
  26. cand=false;
  27. while (!cand and st[top]<vmax)
  28. {
  29. st[top]++;
  30. cand=Valid();
  31. }
  32. if (!cand) top--;
  33. else if (top==n) Afisare();
  34. else st[++top]=vmin-1;
  35. }
  36. }
  37. virtual void Afisare()
  38. {
  39. int i;
  40. for (i=1;i<=n;i++)
  41. cout<<st[i]<<" ";
  42. cout<<"\n";
  43. }
  44. };
  45.  
  46. class Permutari : public Backtracking
  47. {
  48. public:
  49. Permutari(int _n,int _top,int _vmin,int _vmax)
  50. : Backtracking(_n,_top,_vmin,_vmax)
  51. {}
  52. virtual bool Valid()
  53. {
  54. for (int i=1;i<top;i++)
  55. if (st[i]==st[top])
  56. return false;
  57. return true;
  58. }
  59. };
  60.  
  61. int main()
  62. {
  63. Permutari P(3,1,1,3);
  64. P.Back();
  65. return 0;
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement