Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- using namespace std;
- int S[100];
- int n;
- int top;
- int init(int S[],int top)
- { S[top]=0;}
- int solutie(int S[],int top)
- { return top==n;}
- int tipar(int S[],int top)
- {cout<<endl;
- for(int j=1;j<=n;j++)
- if(S[j]==1) cout<<")";
- else cout<<"(";}
- int succ(int S[],int top)
- {if (S[top]<n) { S[top]++; return 1;}
- return 0;}
- int valid(int S[], int top)
- {int pd=0,pi=0;
- for(int i=1;i<=top;i++)
- if(S[i]==0) pd++;
- else pi++;
- return pd<=n/2 && pi<=pd;}
- int bktr(int S[],int top)
- {top=1;
- while (top>=1)
- {
- int am,este;
- do { am=succ(S,top);
- este=valid(S,top);
- } while(!( (am&&este) || (!am) ) ) ;
- if (am){if (solutie(S,top)) {tipar(S,top);}
- else {top++;init(S,top);}
- }
- else top--;
- }
- return 0;}
- int main()
- {cout<<"n=";cin>>n;
- if(n%2!=0) cout<<"Nu este posibil.";
- bktr(S,n);}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement