0% found this document useful (0 votes)
31 views2 pages

Back Principal

This C++ program: 1) Defines functions to initialize an array, find the successor of an element, check if an element is valid, and print a solution. 2) The backtracking function recursively tries all possibilities of a solution array, checking for valid successors and solutions. 3) It calls the initialization, successor, validity, solution, and printing functions at each step to recursively generate all possible solutions.

Uploaded by

Andrei Băluță
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views2 pages

Back Principal

This C++ program: 1) Defines functions to initialize an array, find the successor of an element, check if an element is valid, and print a solution. 2) The backtracking function recursively tries all possibilities of a solution array, checking for valid successors and solutions. 3) It calls the initialization, successor, validity, solution, and printing functions at each step to recursively generate all possible solutions.

Uploaded by

Andrei Băluță
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

# include <iostream>

# include <stdlib.h>
using namespace std;
int as,ev,st[100],k,n;

void init (int k, int st[100])


{
st[k]=vi-rel;
}

void succesor(int k, int st[100], int &as)


{
if(st[k]<n) { as=1;
st[k]=st[k]+rel;
}
else as=0;
}

void valid(int k, int st[100], int &ev)


{
int i;
ev=1;

//distincte
for(i=1;i<k&&ev;i++)
if(st[i]==st[k]) ev=0;

//suma partiala
s=0;
for(i=1;i<=k;i++)
s=s+st[k];
if(s>..conditie..) ev=0;

//daca sunt crescatoare


if(k>1 && st[k]<st[k-1]) ev=0;

int solutie (int k)


{

if(k==n) return 1;
else return 0;
}

void tipar(int k)
{
int i;
for(i=1;i<=k;i++)
cout<<st[i]<<" ";
cout<<endl;
}

void back()
{
k=1;
init(k,st);
while(k>0)
{
do {
succesor(k,st,as);
if (as) valid(k,st,ev);

}while(as && !ev);

if(as)
if(solutie(k)) tipar(k);
else {
k++;
init(k,st);
}
else k--;
}
}

int main ()
{
cout<<"n="; cin>>n;
back();
return 0;
}

You might also like