Sunteți pe pagina 1din 7

Probleme rezolvate de Backtraking

2. Lui Gigel îi plac nr. formate numai din cifre pare cifre aflate în ordine descrescătoare. Să se
determine şi să se afişeze pe ecran toate nr. de n cifre (0<n<10) care îi plac lui Gigel. Valoarea lui
n este un nr. natural care se citeşte de la tastatură.

Exemplu:

INPUT OUTPUT
200 220 222 400 402 422 440 442 444 600 620 622 640 644 660 662 664
n=3 666 800 820 822 840 842 844 860 862 864 866 880 882 884 886 888

type int stiva[100];


stiva st;
int i,n,k;
bool as,ev;

void init(init k, stiva st)


{ st[k]=-1;
}

void succesor(bool as, stiva st, int k)


{ if st[k]<9 { st[k]=st[k]+1;
as=1;}
else as=0;
}

void valid(bool ev, stiva st, int k)


{ int i;
ev=1;
for(i=1;i<=k-1;i++)
if(st[i]%2!=0) ev=0;
for(i=1;i<=k-1;i++)
if(st[i]<st[i+1]) ev=0;
}

bool solutie(int k)
{ solutie=(k=n);
}

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

int main()
{ cin>>n;
k=1;
init(k,st);
while(k>0)
{ do{ succesor(as,st,k);
if(as) valid(ev,st,k);
}until ((not as) or (as and ev));
if(as)
if(solutie(k)) tipar
else { k=k+1;
init(k,st);}
else k=k-1;
}
}

3. Concurs sportiv
La un concurs sportiv s-au înscris n concurenţi având numerele de concurs 1,2,...,n. Pentru
fiecare sportiv se cunoaşte tara de origine (şir de caractere). In prima zi vor intra in concurs m
concurenţi. Afişaţi toate posibilităţile de a stabili ordinea intrării in concurs a celor m concurenţi
respectând următoarele condiţii:
- 2 sportivi din aceeaşi tara nu pot evolua unul după altul
- trebuie respectata ordinea crescătoare a numerelor de concurs ale sportivilor.

type int stiva[100];


stiva st;
char tara[50];
int m,i,n,k;
bool as,ev;

void init(init k, stiva st)


{ st[k]=0;
}

void succesor(bool as, stiva st, int k)


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

void valid(bool ev, stiva st, int k)


{ int i;
ev=1;
for(i=1;i<=k-1;i++)
if(st[i]==st[k]) ev=0;
if (k>1 and tara[st[k-1]]==tara[st[k]]) ev=0;
if (k>1 and st[k]< st[k-1]) ev=0;
}

bool solutie(int k)
{ solutie=(k=m);
}

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

int main()
{ cin>>n>>m;
for(i=1;i<=n;i++)
{ cout<<”concurentul ”<<i<<”=”;cin>>ţara[i];}
while(k>0)
{ do{ succesor (as,st,k);
if as then valid(ev,st,k);
}until (not as) or (as and ev);

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

4. Descompunere
Sa se afişeze toate modurile posibile de a descompune un număr natural n in suma de k numere
naturale diferite(n si k sunt cunoscute).

type int stiva[100];


stiva st;
int s,n,k,p;
bool as,ev;

void init(init k, stiva st)


{ st[k]=0;
}

void succesor(bool as, stiva st, int k)


{ if st[k]<n-p+k { st[k]=st[k]+1;
as=1;}
else as=0;
}

void valid(bool ev, stiva st, int k)


{ int i;
ev=1;
s=0;
for(i=1;i<=k-1;i++)
if(st[i]==st[k]) ev=0;
if (k>=2 and st[k]<st[k-1]) ev=0;
if (s>n) ev=0;
}

bool solutie(int k)
{ solutie=(k=p);
}

void tipar;
{ int i;
if(s==n)
for(i=1;i<=p;i++)
if (st[i]%2==1) cout<<st[i]<<” ”;
}

int main()
cin>>n>>p;
k=1;init(k,st);
while k>0 do
begin
repeat
succesor (as,st,k);
if as then valid(ev,st,k);
until (not as) or (as and ev);
if as then
if solutie(k) then tipar
else begin
k=k+1;
init(k,st)
end
else k=k-1;
end;

5. POSIBILITATI DE ARANJARE A PARANTEZELOR


Se considera de la tastatura un numar natural n care reprezinta un nr. de paranteze. Scrieti un
program care sa determine toate posibilitatiile de aranjare a celor n paranteze astfel incat sirul de
paranteze rezultat sa fie corect sintactic.

type int stiva[100];


stiva st;
int s,n,k;
bool as,ev;

void init(init k, stiva st)


{ st[k]=0;
}

void succesor(bool as, stiva st, int k)


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

void valid(bool ev, stiva st, int k)


{ int i;
ev=1;
for(i=1;i<=k-1;i++)
if(st[i]==st[k]) ev=0;
if (k==1 and st[k]%2==0) ev=0;
if (k==n and st[k]%2==1) ev=0;
}

bool solutie(int k)
{ solutie=(k=n);
}

void tipar;
{ int i;
for(i=1;i<=k;i++)
if (st[i]%2==1) cout<<”(”;
else cout<<”)”;
}

int main()
{ cin>>n;
k=1;
init(k,st);
while(k>0)
{ do{ succesor(as,st,k);
if(as) valid(ev,st,k);
}until ((not as) or (as and ev));
if(as)
if(solutie(k)) tipar
else { k=k+1;
init(k,st); }
else k--;
}
}
6. Partitiile unui numar natural
Scrieti un program care sa determine toate partitiile unui nr natural n.

Exemplu:

INPUT OUTPUT
n=3 1+1+1=3
1+2=3
2+1=3
3=3

type int stiva[100];


stiva st;
int s,n,k;
bool as,ev;

void init(init k, stiva st)


{ st[k]=0;
}

void succesor(bool as, stiva st, int k)


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

void valid(bool ev, stiva st, int k)


{ int i;
s=0;
for(i=1;i<=k;i++)
s=s+st[i]
if(s<n) ev=1;
else ev=0;
}

bool solutie(int k)
{ solutie=(k=n);
}

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

int main()
{ cin>>n;
k=1;
init(k,st);
while (k>0)
{ do{ succesor(as,st,k);
if as then valid(ev,st,k);
}until (not as) or (as and ev);

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

S-ar putea să vă placă și