0% found this document useful (0 votes)
69 views

Permutari Video: Grila Backtrcking

The document provides 7 examples of using backtracking to solve different problems: 1. Permutations - Backtracking code to generate all permutations of numbers from 1 to n. 2. Arrangements - Backtracking code to generate all arrangements of numbers from 1 to n taken p at a time. 3. Combinations - Backtracking code to generate all combinations of numbers from 1 to n taken p at a time. 4. Generating all possible flags using a given set of colors. 5. Arranging n books into packages of m books each. 6. Solving a maze problem using backtracking. 7. Generating all possible subsets of numbers from 1 to n

Uploaded by

beatricec.2004
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
69 views

Permutari Video: Grila Backtrcking

The document provides 7 examples of using backtracking to solve different problems: 1. Permutations - Backtracking code to generate all permutations of numbers from 1 to n. 2. Arrangements - Backtracking code to generate all arrangements of numbers from 1 to n taken p at a time. 3. Combinations - Backtracking code to generate all combinations of numbers from 1 to n taken p at a time. 4. Generating all possible flags using a given set of colors. 5. Arranging n books into packages of m books each. 6. Solving a maze problem using backtracking. 7. Generating all possible subsets of numbers from 1 to n

Uploaded by

beatricec.2004
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

Permutari video https://www.youtube.com/watch?

v=UrOrH-ulA4o

Grila backtrcking

http://hzone.xhost.ro/site_modul_programare_web/exemple_atestat/logofatu/back1.html

http://info.mcip.ro/?cap=Backtracking

1. Permutari - Backtracking
#include<iostream>
using namespace std;
int st[10],n,k;
void Init()
{
st[k]=0;
}
int Am_Succesor()
{
if(st[k]<n)
{
st[k]++;
return 1;
}
else return 0;}
int E_Valid()
{
for(int i=1;i<k;i++)
if(st[i]==st[k])return 0;
return 1;}
int Solutie()
{
return k==n;
}
void Tipar()
{
for(int i=1;i<=n;i++)cout<<st[i];
cout<<endl;}
void back()
{
int As;
k=1;Init();
while(k>0)
{
do{}while((As=Am_Succesor())&&!E_Valid());
if(As)
if(Solutie())Tipar();
else {k++;Init();}
else k--;}}
int main()
{
cout<<"n=";cin>>n;
back();
}

SAU Permutari fara do while


#include<iostream>
using namespace std;
int st[10],n,k,i,As,ev;
void Init()
{
st[k]=0;
}
int Am_Succesor()
{
if(st[k]<n)
{
st[k]++;
return 1;
}
else return 0;}
int E_Valid()
{
for(i=1;i<k;i++)
if(st[i]==st[k])return 0;
return 1;}
int Solutie()
{
return k==n;
}
void Tipar()
{
for(i=1;i<=n;i++)cout<<st[i];
cout<<endl;}
void back()
{
k=1;Init();
while(k>0)
{As=1;ev=0;
while(As&&!ev)
{As=Am_Succesor();
if(As) ev=E_Valid();}
if(As)
if(Solutie())Tipar();
else {k++;Init();}
else k--;}}
int main()
{
cout<<"n=";cin>>n;
back();
}

2. Aranjamente - Backtracking
#include<iostream>
using namespace std;
int st[10],n,k,p;
void Init()
{
st[k]=0;
}
int Am_Succesor()
{
if(st[k]<n)
{
st[k]++;
return 1;
}
else return 0;}
int E_Valid()
{
for(int i=1;i<k;i++)
if(st[i]==st[k])return 0;
return 1;}
int Solutie()
{
return k==p;
}
void Tipar()
{
for(int i=1;i<=p;i++)cout<<st[i];
cout<<endl;}
void back()
{
int As;
k=1;Init();
while(k>0)
{
do{}while((As=Am_Succesor())&&!E_Valid());
if(As)
if(Solutie())Tipar();
else {k++;Init();}
else k--;}}
int main()
{
cout<<"n=";cin>>n;
cout<<"p=";cin>>p;
back();
}

3. Combinari - Backtracking
#include<iostream>
using namespace std;
int st[10],n,k,p;
void Init()
{
if(k==1)
st[k]=0;
else
st[k]=st[k-1];
}
int Am_Succesor()
{
if(st[k]<n-p+k)
{
st[k]++;
return 1;
}
else return 0;}
int E_Valid()
{
for(int i=1;i<k;i++)
if(st[i]==st[k])return 0;
return 1;}
int Solutie()
{
return k==p;
}
void Tipar()
{
for(int i=1;i<=p;i++)cout<<st[i];
cout<<endl;}
void back()
{
int As;
k=1;Init();
while(k>0)
{
do{}while((As=Am_Succesor())&&!E_Valid());
if(As)
if(Solutie())Tipar();
else {k++;Init();}
else k--;}}
int main()
{
cout<<"n=";cin>>n;
cout<<"p=";cin>>p;
back();}

4. Se citesc n culori sa se afiseze steagurile care se pot forma cu ele


#include <iostream>
using namespace std;
int st[10],n,k,i;
char cul[10][15];
void init()
{
st[k]=0;
}
int succesor()
{
if(st[k]<n)
{
st[k]++;
return 1;
}
else return 0;}
int solutie()
{
return k==n;
}
void tipar()
{
for(i=1;i<=n;i++)
cout<<cul[st[i]]<<" ";
cout<<endl;
}
int valid()
{
for(i=1;i<k;i++)
if(st[i]==st[k])return 0;
return 1;
}
void bt()
{
int as;
k=1;init();
while(k>0)
{
while((as=succesor())&&!valid());
if(as)
if(solutie())tipar();
else {k++;init();}
else k--;}}
int main()
{
cout<<"n=";cin>>n;
for(i=1;i<=n;i++)
{cout<<"culoare"<<i<<"=";
cin>>cul[i];}
bt();
}

5.Aranjarea a n carti in pachete de m carti in biblioteca


#include <iostream>
using namespace std;
int n,k,i,st[10],m,x=1;
char carti[10][20];
void init()
{st[k]=0;}
int succesor()
{
if(st[k]<n)
{
st[k]++;
return 1;
}
else return 0;
}
int solutie()
{
return k==m;
}
void tipar()
{cout<<x<<") ";x++;
for(i=1;i<=m;i++)
cout<<carti[st[i]]<<" ";
cout<<endl;
}
int valid()
{
for(i=1;i<k;i++)
if(st[i]==st[k]) return 0;
return 1;
}
void bt()
{int as;
k=1; init();
while(k>0)
{

while((as=succesor()) && !valid());


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

int main()
{cout<<"n=";cin>>n;
cout<<"m=";cin>>m;
for(i=1;i<=n;i++)
{cout<<"cartea "<<i<<"=";
cin>>carti[i];}
bt();

return 0;
}

6. Labirint
#include <iostream>
using namespace std;
ifstream f("date.in");
int l[10][10],d[2][100],i,j,m,n,x,y,k;
void Tipar(int k, int d[2][100])
{int i; cout<<"_ _"<<endl;
for(i=1;i<=k;i++)
cout<<"l="<<d[0][i]<<" "<<"c="<<d[1][i]<<endl;
}
void ies(int x, int y, int&k, int l[10][10], int d[2][100])
{
int i, gasit;
if(l[x][y]==16) Tipar(k,d);
else
{
k++;
d[0][k]=x;
d[1][k]=y;
gasit=0;
for(i=1;i<=k-1;i++)
if(d[0][i]==d[0][k] && d[1][i]==d[1][k]) gasit=1;
if(!gasit)
for(i=1;i<=4;i++)
switch(i)
{
case 1: {if(l[x][y] & (int) 8) ies(x-1,y,k,l,d); break;}
case 2: {if(l[x][y] & (int) 4) ies(x,y+1,k,l,d); break;}
case 3: {if(l[x][y] & (int) 2) ies(x+1,y,k,l,d); break;}
case 4: {if(l[x][y] & (int) 1) ies(x,y-1,k,l,d); break;}
k--;
}
}
}
int main()
{cin>>n; cin>>m;
for(i=1;i<=m;i++)
for(j=1;j<=n;j++)
{
cout<<"l["<<i<<","<<j<<"]=";
cin>>l[i][j];}
cin>>x;
cin>>y;
for(i=1;i<=n;i++)
{
l[0][i]=16; l[m+1][i]=16;
}
for(i=1;i<=n;i++)
{
l[i][0]=16; l[i][n+1]=16;
}
k=0;
ies(x,y,k,l,d);
return 0;
}
7. Problema cu submultimi
#include <iostream>
using namespace std;
int x=1,st[20],i,k,n;
void init()
{
st[k]=-1;
}
int succesor()
{if(st[k]<1) {st[k]++;return 1;}
else return 0;}
int valid()
{return 1;}
int solutie()
{
return k==n;
}
void tipar()
{
cout<<x<<".{ "; x++;
for(i=1;i<=n;i++)
if(st[i]==1) cout<<i<<",";
cout<<"\b"<<" }"<<endl;}
void bt()
{int as;
k=1;init();
while(k>0)
{while((as=succesor()) && !valid ());
if(as) if(solutie()) tipar();
else {k++;init();}
else k--;}}
int main()
{
cout<<"n=";cin>>n;
bt();
return 0;
}

You might also like