Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cmath>
- using namespace std;
- int n, x[2000], nrsol=0;
- void init(int k)
- {
- x[k]=0;
- }
- int existaSuccesor(int k)
- {
- return x[k]<n;
- }
- int solutie(int k)
- {
- return k==n;
- }
- void tipar()
- {
- /*for(int k=1; k<=n; k++)
- cout<<"Dama "<<k<<" pe coloana "<<x[k]<<endl;
- nrsol++;
- cout<<endl<<endl;*/
- nrsol++;
- for(int i=1; i<=n; i++)
- cout<<x[i]<<" ";
- cout<<endl;
- }
- int cont(int k)
- {
- return 1;
- }
- void backt()
- {
- int k=1;
- init(1);
- while(k>0)
- {
- if(existaSuccesor(k))
- {
- x[k]++;
- if(cont(k))
- if(solutie(k))
- tipar();
- else
- {
- k++;
- init(k);
- }
- }
- else
- k--;
- }
- }
- int main()
- {
- cin>>n;
- backt();
- cout<<"Sunt "<<nrsol<<" solutii."<<endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement