backtracking cifre pare crescatoare

Backtracking cifre pare crescatoare

Folosind metoda backtracking, sa se scrie un program care genereaza toate nr din 3 cifre pare,cifrele strict in ordine crescatoare

#include<iostream.h>
#include<math.h>
int st[20],i,p,v[]={0,2,4,6,8};

int valid(int p)
{
for(i=1;i<p;i++)
if(st[p]<=st[i])return 0;
if(p==1 && st[p]==0)return 0;
return 1;
}

int sol(int p)
{return (p==3);}

void tipar(int p)
{              
int i;
for(i=1;i<=p;i++)
cout<<st[i]<<” “;
cout<<endl;
}

void bkt(int p)
{int val;
for(val=0;val<=4;val++)
{
st[p]=v[val];
if(valid(p))
if (sol(p))
tipar(p);
else bkt(p+1);
}}

void main()
{
bkt(1);
}

Leave a Comment