Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- class Backtracking
- {
- protected:
- int n,st[20],top;
- int vmin,vmax; /// valorile minime si maxime de pe stiva
- public:
- Backtracking(int _n,int _top,int _vmin,int _vmax)
- : n(_n),top(_top),vmin(_vmin),vmax(_vmax)
- {}
- /// metoda este virtuala pentru a fi polimorfica
- /// (redefinita in clasele derivate)
- virtual bool Valid()
- {
- return true;
- }
- void Back()
- {
- bool cand;
- st[top]=vmin-1;
- while (top>0)
- {
- cand=false;
- while (!cand and st[top]<vmax)
- {
- st[top]++;
- cand=Valid();
- }
- if (!cand) top--;
- else if (top==n) Afisare();
- else st[++top]=vmin-1;
- }
- }
- virtual void Afisare()
- {
- int i;
- for (i=1;i<=n;i++)
- cout<<st[i]<<" ";
- cout<<"\n";
- }
- };
- class Permutari : public Backtracking
- {
- public:
- Permutari(int _n,int _top,int _vmin,int _vmax)
- : Backtracking(_n,_top,_vmin,_vmax)
- {}
- virtual bool Valid()
- {
- for (int i=1;i<top;i++)
- if (st[i]==st[top])
- return false;
- return true;
- }
- };
- int main()
- {
- Permutari P(3,1,1,3);
- P.Back();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement